【问题标题】:net.tcp service works when hosted by Net.Tcp Listener Adapter, but not Windows Servicenet.tcp 服务在由 Net.Tcp 侦听器适配器托管时工作,但不是 Windows 服务
【发布时间】:2011-12-29 17:47:05
【问题描述】:

我创建了一个 WCF net.tcp 服务并使用 Net.Tcp 侦听器适配器托管它,它运行良好 - 我在回调中设置了一些消息传递,因此该服务会使用状态更新客户端。现在,我正试图通过 Windows 服务托管它来使其工作,而我所做的只是使用原始使用的相同类创建一个 ServiceHost:

using System.Diagnostics;
using System.ServiceModel;
using System.ServiceProcess;
using BuilderService;

namespace BuilderWindowsService
{
    public class BuilderWindowsService : ServiceBase
    {
        public ServiceHost ServiceHost = null;
        public BuilderWindowsService()
        {
            ServiceName = ServiceNames.Builder;
        }

        public static void Main()
        {
            Run(new BuilderWindowsService());
        }

        protected override void OnStart(string[] args)
        {
            if (ServiceHost != null)
                ServiceHost.Close();
            ServiceHost = new ServiceHost(typeof(Builder));
            ServiceHost.Open();
        }

        protected override void OnStop()
        {
            if(ServiceHost != null)
            {
                ServiceHost.Close();
                ServiceHost = null;
            }
        }
    }
}

我可以连接到该服务并发送一个请求,但它永远不会响应也不会超时。我知道我正在访问 Windows 服务,因为我在另一个端口 (8002) 上有它,我可以使用它作为参考添加它。

Windows 服务的我的 App.config 也与原始的 Web.config 几乎相同。我正在使用的客户端也是如此,除了它指向 8002 端点而不是 808。另外,我已经为另一个服务工作,进行完全相同的设置,但由于某种原因,这个端点永远不会响应。

更新

我创建了一个小客户端应用程序来测试直接点击 Windows 服务以排除任何干扰,它生成了以下 app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_IBuilder" 
                         closeTimeout="00:01:00"
                         openTimeout="00:01:00"
                         receiveTimeout="00:10:00"
                         sendTimeout="00:01:00"
                         transactionFlow="false" 
                         transferMode="Buffered"
                         transactionProtocol="OleTransactions"
                         hostNameComparisonMode="StrongWildcard" 
                         listenBacklog="10"
                         maxBufferPoolSize="2147483647" 
                         maxBufferSize="2147483647"
                         maxConnections="10"
                         maxReceivedMessageSize="2147483647">
                    <readerQuotas maxDepth="32"
                                  maxStringContentLength="2147483647"
                                  maxArrayLength="2147483647"
                                  maxBytesPerRead="4096"
                                  maxNameTableCharCount="2147483647" />
                    <reliableSession ordered="true" 
                                     inactivityTimeout="00:10:00"
                                     enabled="false" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" 
                                   protectionLevel="EncryptAndSign" />
                        <message clientCredentialType="Windows" 
                                 algorithmSuite="Default" />
                    </security>
                </binding>
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://localhost:8002/BuilderService/Builder.svc"
                      binding="netTcpBinding"
                      bindingConfiguration="NetTcpBinding_IBuilder"
                      contract="RGBRef.IBuilder" 
                      name="NetTcpBinding_IBuilder">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

这对我来说看起来很正常(注意:我手动将缓冲区/字符串长度值增加到最大值)。只有与我原来的配置不同的地方:

transferMode="Buffered"
transactionProtocol="OleTransactions"
listenBacklog="10"
<transport clientCredentialType="Windows" 
           protectionLevel="EncryptAndSign" />

不确定服务是否期望这些或其他东西。无论哪种方式,它仍然没有得到任何响应,也没有错误。

【问题讨论】:

    标签: wcf windows-services net.tcp


    【解决方案1】:

    也许该服务出现故障,因为它现在作为 Windows 服务在不同的凭据下运行。编写一些 EventLog 条目以跟踪故障发生的位置。我不相信是回调,我怀疑这是服务失败的其他原因。

    【讨论】:

    • 向windows服务添加了日志记录,但在任何地方都不会出错
    猜你喜欢
    • 2013-03-03
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-28
    相关资源
    最近更新 更多