【发布时间】:2011-10-30 15:48:29
【问题描述】:
我的 wcf 服务配置有问题。 我希望每次调用我的服务都创建一个新的服务实例。 对于并发,我希望一个调用在另一个开始之前完成。
因此,如果我有这样的服务:
[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;
}
}
当我在一个循环中多次调用它时,跟踪给出:
thread 1
thread 2
start operation
start operation
end operation
end operation
虽然我想要这个:
thread 1 start operation end operation
thread 2 start operation end operation
这可能吗?谢谢
【问题讨论】:
-
如您所描述的跨单独服务调用同步线程的唯一方法是将服务配置为将并发和实例上下文设置为单一(即单例模式)。如果您这样做,您的应用程序将几乎没有可扩展性,因为您正在创建一个非常有效的瓶颈;)
-
@Sixto:并发和上下文生命周期是两个不同的东西。不需要单例。
标签: c# wcf wcf-configuration servicebehavior