WCF ConcurrencyMode Single and InstanceContextMode PerCall

亡梦爱人 提交于 2019-11-27 22:00:19

I know this question was marked as answered, but there is a better alternative:

If you use a InstanceContextMode.Single then you will reuse the same instance for all calls. If your service is long running this requires your code to manage resources perfectly, since it will never be garbage collected without a service restart.

Instead keep the InstanceContextMode.PerCall for “every call to my service creates a new instance” and then use throttling: Set the max concurrent instances to 1. The MSDN documentation does exactly this as one of the examples.

What you have there will result in a new instance of the service spinning up with each request (that's what PerCall does).

This should do it:

[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Single, InstanceContextMode=InstanceContextMode.Single)]

FYI if you do this you'll lose all scalability. You'll have a single instance of a single threaded service to respond to all requests.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!