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

查看:30
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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