WCF ConcurrencyMode单和InstanceContextMode PerCall [英] WCF ConcurrencyMode Single and InstanceContextMode PerCall

查看:302
本文介绍了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为每次调用我的服务创建一个新的实例,然后使用限制:设置的<一个href=\"http://msdn.microsoft.com/en-us/library/system.servicemodel.description.servicethrottlingbehavior.maxconcurrentinstances.aspx\">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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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