.net 多线程环境中的OperationContext.Current.SessionId
我有一个连接到多个客户端的WCF服务。 服务配置如下:.net 多线程环境中的OperationContext.Current.SessionId,.net,multithreading,wcf,operationcontext,.net,Multithreading,Wcf,Operationcontext,我有一个连接到多个客户端的WCF服务。 服务配置如下: ServiceBehavior( InstanceContextMode = InstanceContextMode.Single, UseSynchronizationContext = false, ... 在服务中,我使用OperationContext.Current.SessionId标识客户端 public void Register() { Debug.Writ
ServiceBehavior(
InstanceContextMode = InstanceContextMode.Single,
UseSynchronizationContext = false,
...
在服务中,我使用OperationContext.Current.SessionId标识客户端
public void Register()
{
Debug.WriteLine(OperationContext.Current.SessionId);
}
想象一下以下场景:
-客户端1呼叫寄存器
-该服务在线程1中运行注册表
-一段时间后,Client2调用Register
-服务也在Thread1中运行Register(理论上是可能的,不是吗?)
知道OperationContext.Current是ThreadStatic,
第二次通话显示什么内容:
- 会话1,因为在第一次调用中已设置OperationContext.Current
- 还是第二课时
提前感谢您……您将获得第2次会议。会话和实例化在WCF中是两件不同的事情。我建议您阅读以下两篇博客文章,这将帮助您理解WCF会话和实例化的概念 您还可以使用客户机对此进行测试。我更改了您的WCF方法以返回sessionID并在客户端中使用它
static void Main(string[] args)
{
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
Console.WriteLine(client.Regster());
Console.ReadLine();
client.Close();
}
谢谢你的博客,非常有用。