WCF ConcurrencyMode Single 和 InstanceContextMode PerCall

编程入门 行业动态 更新时间:2024-10-26 20:25:49
本文介绍了WCF ConcurrencyMode Single 和 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

虽然我想要这个:

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,那么您将为所有调用重用相同的实例.如果您的服务长时间运行,这需要您的代码完美地管理资源,因为如果不重新启动服务,它将永远不会被垃圾收集.

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,然后使用限制:设置 最大并发实例到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 Single 和 InstanceContextMode PerCall

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

发布评论

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

>www.elefans.com

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