WCF分布式開發常見錯誤解決(6)Service 'WcfServiceApp.WCFService' has zero application
調試WCF服務應用程序的時候,會出現如下錯誤:
“/”應用程序中的服務器錯誤。Service 'WcfServiceApp.WCFService' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.
說明: 執行當前 Web 請求期間,出現未處理的異常。請檢查堆棧跟蹤信息,以了解有 關該錯誤以及代碼中導致錯誤的出處的詳細信息。
異常詳細信息:
System.InvalidOperationException: Service 'WcfServiceApp.WCFService' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.
源錯誤:
執行當前 Web 請求期間生成了未處理的異常。可以使用下面的異常堆棧跟蹤信 息確定有關異常原因和發生位置的信息。
此錯誤由於沒有服務終結點所致。
解決辦法1:在配置文件添加代碼,配置服務節點:
<endpoint address="" binding="wsHttpBinding" contract="WcfServiceApp.IWCFService">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
解決辦法2:編程方式添加服務節點
using (ServiceHost host = new ServiceHost(typeof (WCFService.WCFService)))
{
Uri tcpAddress = new Uri("net.tcp://localhost:8001/WCFService");
host.AddServiceEndpoint(typeof(WCFService.IWCFService), new NetTcpBinding() , tcpAddress);
if (host.State ! =CommunicationState.Opening)
host.Open();
//顯示運行狀態
Console.WriteLine("Host is runing! and state is {0}",host.State);
//等待輸入即停止服務
Console.Read();
}