net.tcp,套接字连接已中止

本文关键字:连接 套接字 tcp net | 更新日期: 2023-09-27 18:33:38

我创建了包含Windows Service(WindowsServices.cs)的项目,并在该类中添加了WCF协定和服务文件(WindowsServices.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"。远程主机强行关闭了现有连接如果服务是在当前解决方案中定义的,请尝试生成解决方案并再次添加服务引用。

如何解决这个问题?

net.tcp,套接字连接已中止

看看这篇文章:如果我设置 HttpGetEnabled = false 会发生什么

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

smb.HttpGetEnabled = true;

希望会有所帮助。

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

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

如果没有:尝试在 WCF 服务实现 (AgentSvcImpl) 类作用域内创建新的计时器,具有一些非无限间隔(例如 1 秒)和空的 OnTimer 处理程序。和配置跟踪以探索更多