Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WCF调用中基于请求参数的并行性?_C#_Wcf_Concurrency_Actor Model - Fatal编程技术网

C# WCF调用中基于请求参数的并行性?

C# WCF调用中基于请求参数的并行性?,c#,wcf,concurrency,actor-model,C#,Wcf,Concurrency,Actor Model,是否有任何干净的方法可以让WCF调用基于请求参数应用最大并行度1 假设以下代码: public void MyWcfOperation(int entityId, other arguments) { //do stuff with entity with id = entityId } 我想在这里处理最多1个基于entityId的并行调用。 我知道我可以为WCF设置并发选项,但这要么是并发的,要么是单线程的。但是,当我只需要操作系统对每个entityId进行一次调用时,将整

是否有任何干净的方法可以让WCF调用基于请求参数应用最大并行度1

假设以下代码:

 public void MyWcfOperation(int entityId, other arguments)
 {
      //do stuff with entity with id = entityId
 }
我想在这里处理最多1个基于entityId的并行调用。 我知道我可以为WCF设置并发选项,但这要么是并发的,要么是单线程的。但是,当我只需要操作系统对每个entityId进行一次调用时,将整个操作设置为单次调用似乎有点过激


我假设我必须进行这个操作,但是在这里,我最好的一个涉及演员模型LIB的W/O是什么?

首先,考虑你是否真的需要基于一个ID来同步。你是否预见到了一个放大的问题?一如既往,在寻求这种可能过早的优化之前,测试和基准测试是您的最佳选择

也就是说,如果实体ID的数量不大,您可以使用以下简单的解决方案:

[ServiceBehavior(
   InstanceContextMode = InstanceContextMode.Single, // required to hold the dictionary through calls
   ConcurrencyMode = ConcurrencyMode.Multiple] // let WCF run all the calls concurrently
class Service
{
  private readonly ConcurrentDictionary<int, object> _locks = new ConcurrentDictionary<int, object>();

  public void MyWcfOperation(int entityId, other arguments)
  {
     lock (_locks.GetOrAdd(entityId, i => new Object())
     {
       // do stuff with entity with id = entityId
     }
  }
}
[ServiceBehavior(
InstanceContextMode=InstanceContextMode.Single,//需要通过调用保存字典
ConcurrencyMode=ConcurrencyMode.Multiple]//让WCF同时运行所有调用
班级服务
{
私有只读ConcurrentDictionary _locks=新ConcurrentDictionary();
public void mywcfooperation(int entityId,其他参数)
{
lock(_locks.GetOrAdd(entityId,i=>newobject())
{
//使用id=entityId的实体进行填充
}
}
}
这不涉及清理,因此如果有很多实体,字典将增长为包含每个实体的锁对象