我创建了包含 Windows 服务 (windowsservices.cs) 的项目,并在该类 (windowservices.cs) 中添加了 wcf 合同和服务文件。它的 app.config 文件包含
<service behaviorConfiguration="WindowsService1.Service1Behavior"
name="AgentSvcImpl">
<endpoint address="" binding="netTcpBinding" bindingConfiguration=""
name="nethttpendpoint" contract="IAgentWinService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
name="nethttpmetadataendpoint" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8011/AgentSvcImpl" />
</baseAddresses>
</host>
</service>
在 Windows 服务 onstart 方法中,我输入了以下代码:
ServiceHost serviceHost = null;
if (serviceHost != null)
{
serviceHost.Close();
}
//Create a URI to serve as the base address
Uri httpUrl = new Uri("net.tcp://localhost:8011/AgentSvcImpl");
//Create ServiceHost
serviceHost = new ServiceHost(typeof(WindowsService1.AgentSvcImpl), httpUrl);
//Add a service endpoint
serviceHost.AddServiceEndpoint(typeof(WindowsService1.IAgentWinService), new NetTcpBinding(), "");
//Enable metadata exchange
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = false;
serviceHost.Description.Behaviors.Add(smb);
//Start the Service
serviceHost.Open();
当我在 services.msc 中启动此服务时,它启动良好。但是当我尝试添加服务参考时,它会出现以下错误。
元数据包含无法解析的引用:“net.tcp://localhost:8011/AgentSvcImpl”。套接字连接被中止。这可能是由于处理您的消息时出错或远程主机超出接收超时,或者是潜在的网络资源问题造成的。本地套接字超时为“00:04:59.0054016”。远程主机强行关闭现有连接如果在当前解决方案中定义了服务,请尝试构建解决方案并再次添加服务引用。
如何解决这个问题?