优雅地终止 WCF 服务 - 完成所有打开的会话并限制新会话 [英] Gracefully terminate WCF Service - complete all open sessions and restrict new sessions

查看:38
本文介绍了优雅地终止 WCF 服务 - 完成所有打开的会话并限制新会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个 WCF 服务,它托管在 Windows 服务中.它在 PerSession 模式下运行.该服务允许客户端通过该服务远程打开文件、更改文件和关闭文件.到目前为止,一切都非常顺利.

I have a WCF Service that I have written, which is hosted within a Windows service. It is operating in PerSession mode. The service permits clients to open files, make changes to files and close files remotely through the service. So far all works very smoothly.

当 Windows 服务停止时,我希望 WCF 服务不接受任何新会话,但允许已连接的客户端完成其当前会话并工作(在合理的时间段/超时内).

When the Windows service is stopped, I would like to be able have the WCF Service not accept any new sessions and yet allow already connected clients to complete their current sessions and work (within a reasonable period/timeout).

实现这一目标的最佳方法是什么?

What would be the best way to accomplish this?

推荐答案

基本上,调用 ServiceHost.Close() 将完成此任务 - 它会让所有当前正在运行的请求运行完成,但新请求正在被拒绝是否正在进行有序关闭.

Basically, calling ServiceHost.Close() will accomplish this - it will let all currently running request run to completion, but new request are being rejected will the orderly shutdown is in progress.

您为服务配置的绑定上有一个CloseTimeout"设置 - 用于控制 WCF 运行时允许运行请求的时间,直到它们正确关闭.如果超时发生,仍在运行的请求将被终止.

There's a "CloseTimeout" setting on your binding that you configured for your service - that controls how long the WCF runtime will allow running requests to take until they've properly shut down. If that timeout happens, the still running requests will be killed.

另一方面,ServiceHost.Abort() 方法在数字上相当于一把大锤——所有正在运行的请求都会立即终止,然后主机关闭.

The ServiceHost.Abort() method on the other hand is the digital equivalent of a sledgehammer - all running requests are terminated at once, and the host is shut down.

在客户端,您的客户端代理具有相同的 .Close().Abort() 方法,它们的行为方式相同.

ON the client side, you have the same .Close() and .Abort() methods on your client proxy which behave the same way.

这篇关于优雅地终止 WCF 服务 - 完成所有打开的会话并限制新会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆