WCF ConcurrencyMode单和InstanceContextMode PerCall

编程入门 行业动态 更新时间:2024-10-26 22:30:01
本文介绍了WCF ConcurrencyMode单和InstanceContextMode PerCall的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有我的WCF服务配置的问题。我想每次调用我的服务创建服务的新实例。对于并发性,我想一个呼叫另一个开始前完成。

I have an issue with my wcf service config. I would like every call to my service create a new instance of the service. For the concurrency I would like to one call is finished before another start.

因此​​,如果我有这样一个服务:

Thus if I have a service like this one:

[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Single, InstanceContextMode=InstanceContextMode.PerCall)] public class MyService: IMyService { public bool MyServiceOp() { Debug.WriteLine("thread "+ Thread.CurrentThread.ManagedThreadId.ToString()); Debug.WriteLine("start operation "); Do_work() Debug.WriteLine("end operation"); return true; } }

当我把它用在一个循环多个呼叫,跟踪给:

When I call it with multiple call in a loop, the trace give:

thread 1 thread 2 start operation start operation end operation end operation

虽然我想有这样的:

While I would like to have this:

thread 1 start operation end operation thread 2 start operation end operation

这可能吗?谢谢

推荐答案

我知道这个问题被标记为回答,但有一个更好的替代方案

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

如果您使用的是InstanceContextMode.Single那么你会重复使用相同的实例所有呼叫。如果你的服务是长时间运行,这需要您的code完美地管理资源,因为它永远不会被垃圾收集不重新启动服务。

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.

相反保持InstanceContextMode.PerCall为每次调用我的服务创建一个新的实例,然后使用限制:设置的max并发实例,以1 即可。 MSDN文档正是这种不作为一个例子。

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.

更多推荐

WCF ConcurrencyMode单和InstanceContextMode PerCall

本文发布于:2023-11-09 03:12:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571236.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ConcurrencyMode   WCF   PerCall   InstanceContextMode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!