簡體   English   中英

WPF-自托管WCF

[英]WPF - Self hosting WCF

我正在使用此示例來創建一個自托管的WCF應用程序(.NET 4.5): https : //msdn.microsoft.com/zh-cn/library/ms731758%28v=vs.110%29.aspx

我已經成功地使用它來創建控制台應用程序。

但是我現在正嘗試在全新的WPF應用程序中使用相同的代碼。

我唯一更改的是刪除:

host.Close();

因此,應用程序會在連接打開的狀態下正常運行。

但是,當我使用WCF測試客戶端測試WPF應用程序時,它返回錯誤:

Error: Cannot obtain Metadata from http://localhost:8080/hello If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:8080/hello Metadata contains a reference that cannot be resolved: 'http://localhost:8080/hello'. There was no endpoint listening at http://localhost:8080/hello that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. Unable to connect to the remote server No connection could be made because the target machine actively refused it 127.0.0.1:8080HTTP GET Error URI: http://localhost:8080/hello There was an error downloading 'http://localhost:8080/hello'. Unable to connect to the remote server No connection could be made because the target machine actively refused it 127.0.0.1:8080

如果我在以下位置設置斷點:

host.Open(); 

它正在運行,所以應該運行服務好嗎?

我的WPF應用程序與控制台應用程序引起的區別是什么? 在這兩種情況下,Visual Studio均由管理員運行。

主要代碼:

 using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
        {
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            host.Description.Behaviors.Add(smb);

            host.Open();

            //Console.WriteLine("The service is ready at {0}", baseAddress);
            //Console.WriteLine("Press <Enter> to stop the service.");
            //Console.ReadLine();

            // Close the ServiceHost.
            //host.Close();
        }

MSDN示例中的代碼包含對Console.ReadLine()的調用,該調用可防止應用程序退出在其中實例化和打開ServiceHost實例的using塊。

當您將代碼移到WPF應用程序中時,通過保留using塊,但不阻止代碼執行退出該塊(您不想這樣做,因為它是GUI)就可以有效地創建一個然后立即處置服務主機。

您將需要將ServiceHost引導到using塊之外,並在適合您的應用程序時手動進行處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM