【发布时间】:2010-11-18 15:20:06
【问题描述】:
我们有一个 Web 服务,它将请求发送到一个 Windows 服务,该服务托管一个 WCF 服务进行处理。
界面简洁:
namespace MyApp
{
[ServiceContract]
public interface IMyApp
{
[OperationContract]
string DoSomething(string xml);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
class MyAppWcf : IMyApp
{
public string DoSomething(string xml)
{
Customer customer = GlobalObject.GetCustomer(xml); //millisecs
return customer.DoSomething(xml); //Takes 5-10 seconds
}
}
}
GlobalObject 在 WindowsService.OnStart() 上实例化,并包含客户对象所需的所有静态数据。 接口 DoSomething() 将被调用到 ca。 30 个不同的客户。
问题:
1. 现在默认的线程行为是什么?每次通话都必须等到最后一个通话结束吗?
2. 更改InstanceContextMode会有什么影响?
真实问题:
最多有 1000 个客户对象,可以并行调用 2 个不同的客户对象,但不能并行调用同一个。例如
DoSomething("客户 1"); => 继续。 10 秒内回答
DoSomething("客户 2"); => 与上述调用并行进行。
DoSomething("客户 2"); => 将等待最后一次调用 DoSomething("Customer 2") 完成
我的服务行为设置应该是什么?我是否必须实施锁定机制以防止同一对象被并行处理多次?
谢谢。
编辑 2:GlobalObject.GetCustomer() 只是从字典中检索 XML 中提到的客户。
【问题讨论】:
标签: c# multithreading wcf windows-services