1

我创建了一个 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" />

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

4

1 回答 1

1

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

于 2011-12-29T18:00:21.637 回答