net.tcp,套接字连接中止

时间:2013-10-03 09:16:27

标签: c# wcf service windows-services

我创建了包含windows服务(windowsservices.cs)的项目,并在该类中添加了wcf合同和服务文件(windowservices.cs)。它的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'。 远程主机强制关闭现有连接 如果在当前解决方案中定义了服务,请尝试构建解决方案并再次添加服务引用。

如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

可能的问题 - 没有命名空间的服务名称和合同:

<service ... name="WindowsService1.AgentSvcImpl" ... contract="WindowsService1.IAgentWinService">

如果没有: 尝试在WCF服务实现(AgentSvcImpl)类范围内创建新的Timer,其中包含一些非无限间隔(例如1秒)和空OnTimer处理程序。 并Configure Tracing探索更多

答案 1 :(得分:0)

看一下这篇文章:What happens if I set HttpGetEnabled = false

我想你需要激活行为的HttpGetEnabled属性:

smb.HttpGetEnabled = true;

希望会有所帮助。