我正在使用Topshelf将WCF服务作为Windows服务托管。即使只是在控制台上运行,在我发送一个Ctrl-C后关闭也需要很长时间,这在作为服务运行时会被镜像。在我的本地机器上,调用svcHost.Close(新的TimeSpan(0))需要1ms,但在Topshelf调用的Stop方法结束和代码落在Runner.Host()方法之后的10240ms之间。这不是很好,但在我尝试的生产服务器上,第二个值是70秒。在确定该服务是一块垃圾之前,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更快关闭呢?
答案 0 :(得分:1)
在2.1.0.0中尝试关闭时,Topshelf似乎挂起了。这是我们必须要研究的内容。
此外,您始终可以从http://teamcity.codebetter.com/下载最新的Topshelf develop分支二进制文件。以访客身份登录并找到Masstransit项目,该版本在该部分下。