2

我正在使用 Topshelf 将 WCF 服务作为 Windows 服务托管。即使只是在控制台上运行,在我向它发送 Ctrl-C 后,它也需要很长时间才能关闭,这在作为服务运行时会被镜像。在我的本地机器上,调用 svcHost.Close(new TimeSpan(0)) 需要 1 毫秒,但在 Topshelf 调用的 Stop 方法结束和代码退出 Runner.Host() 方法之后需要 10240 毫秒。这不是很好,但是在我尝试过的生产服务器上,第二个值是 70s。这比 Windows 在确定服务是垃圾之前提供服务的 30 秒要多得多。

这是我的 Topshelf 代码和服务代码。我已经剥离了很多以删除 Log4Net 日志记录和异常处理,因为我已经验证了没有发生异常。

public class Service
{
    private ServiceHost svcHost;

    public void Start()
    {
        string bindUri = "net.tcp://MyMachineName:10000";
        svcHost = new ServiceHost(typeof(MyServiceClass));
        svcHost.AddServiceEndpoint(typeof(IMyService), new NetTcpBinding("tcp"), bindUri);
        svcHost.Description.Behaviors.Add(new LoggerBehavior());
        svcHost.Open();
    }

    public void Stop()
    {
        svcHost.Close(new TimeSpan(0));
        svcHost = null;
    }
}

class Program
{
    static void Main(string[] args)
    {

        Stopwatch sw = new Stopwatch();
        var cfg = RunnerConfigurator.New(c =>
        {
            c.ConfigureService<Service>(s =>
            {
                s.Named("MyServiceName");
                s.HowToBuildService(x => new Service());
                s.WhenStarted(service => service.Start());
                s.WhenStopped(service =>
                    {
                        sw.Start();
                        service.Stop();
                        sw.Stop();
                        Console.WriteLine("Stop Time: {0}ms", sw.ElapsedMilliseconds); // usually 1-2ms
                        sw.Reset();
                        sw.Start();
                    });
            });
            c.RunAsLocalSystem();
            c.SetDescription("Runs MyServiceName.");
            c.SetDisplayName("MyServiceName");
            c.SetServiceName("MyServiceName");
        });

        Runner.Host(cfg, args);

        sw.Stop();
        // ~10 seconds on my machine, ~70s on a production server!
        Console.WriteLine("Finish Time: {0}ms", sw.ElapsedMilliseconds);
    }
}

超过 10 秒和超过 70 秒似乎太“默认”了,所以我搜索了高低的超时设置以尽可能低,但它们似乎都没有任何好处。这是我的 app.config 代码。

<system.serviceModel>
    <bindings>
        <netTcpBinding>
            <binding name="tcp"
                     maxReceivedMessageSize="52428800"
                     transferMode="Buffered"
                     openTimeout="0:00:01"
                     sendTimeout="0:00:01"
                     receiveTimeout="0:00:01" 
                     closeTimeout="0:00:01">
                <security mode="None" />
            </binding>
        </netTcpBinding>
    </bindings>
    <behaviors>
        <serviceBehaviors>
            <behavior name="MyServiceBehavior">
                <serviceMetadata />
                <serviceDebug includeExceptionDetailInFaults="true" />
                <serviceThrottling maxConcurrentCalls="100" maxConcurrentInstances="100" maxConcurrentSessions="100" />
                <serviceTimeouts transactionTimeout="0:00:01" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <services>
        <service name="MyApp.MyServiceClass" behaviorConfiguration="MyServiceBehavior">
            <host>
                <timeouts openTimeout="0:00:01" closeTimeout="0:00:01" />
            </host>
        </service>
    </services>
</system.serviceModel>

那么我该怎么做才能让 WCF 更快地关闭呢?

4

1 回答 1

1

尝试在 2.1.0.0 中关闭时,Topshelf 似乎挂起。这是我们必须研究的事情。

此外,您始终可以从http://teamcity.codebetter.com/下载最新的 Topshelf 开发分支二进制文件。以访客身份登录并找到 Masstransit 项目,构建位于该部分下。

于 2010-10-01T23:07:53.697 回答