gpt4 book ai didi

c# - Topshelf 中托管的 WCF 服务需要很长时间才能关闭

转载 作者:太空宇宙 更新时间:2023-11-03 14:26:30 25 4
gpt4 key购买 nike

我正在使用 Topshelf 将 WCF 服务托管为 Windows 服务。即使只是在控制台上运行,在我向它发送 Ctrl-C 后它也需要很长时间才能关闭,这在作为服务运行时会反射(reflect)出来。在我的本地机器上,调用 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 更快地关闭呢?

最佳答案

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

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

关于c# - Topshelf 中托管的 WCF 服务需要很长时间才能关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3833131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com