WCF方法共享一本字典 [英] WCF methods sharing a dictionary
问题描述
我要创建一个WCF服务库和我有一个关于线程安全食用该库中的方法的问题,以下是完整的实现,我到现在为止。
I'm creating a WCF Service Library and I have a question regarding thread-safety consuming a method inside this library, here is the full implementation that I have until now.
namespace WCFConfiguration
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)]
public class ConfigurationService : IConfigurationService
{
ConcurrentDictionary<Tuple<string,string>, string> configurationDictionary = new ConcurrentDictionary<Tuple<string,string>, string>();
public void Configuration(IEnumerable<Configuration> configurationSet)
{
Tuple<string, string> lookupStrings;
foreach (var config in configurationSet)
{
lookupStrings = new Tuple<string, string>(config.BoxType, config.Size);
configurationDictionary.TryAdd(lookupStrings, config.RowNumber);
}
}
public void ScanReceived(string boxType, string size, string packerId = null)
{
}
}
}
想象一下,我有一个10价值观在我的configurationDictionary,很多人想查询该字典消费ScanReceived方法,是指那些10值来为每个请求ScanReceived客户共享?我需要改变我的ServiceBehavior?
Imagine that I have a 10 values in my configurationDictionary and many people want to query this dictionary consuming ScanReceived method, are those 10 values be shared for each of the clients that request ScanReceived? Do I need to change my ServiceBehavior?
的配置方法只消耗一个人的方式。
The Configuration method is only consumed by one person by the way.
推荐答案
随着 InstanceContextMode
PerCall
你应该让每一个远程电话打进来时(这我假设将是要么配置()
或 ScanReceived您服务,你的字典的新实例()
)。这不会分享 configurationDictionary
多个客户端之间。
With an InstanceContextMode
of PerCall
you should get a new instance of your service and your dictionary every time a remote call comes in (which I'm assuming will be either Configuration()
or ScanReceived()
). This won't share configurationDictionary
between multiple clients.
随着 ConcurrencyMode
单
你永远只能有跑路的code单线程任何一个时间。因此,并发的任何问题都没有实际意义。
With a ConcurrencyMode
of Single
you'll only ever have a single thread running your code at any one time. So any issues of concurrency are moot.
两种方式来分享 configurationDictionary
:
- 请为静态:
私人只读静态ConcurrentDictionary&LT; ...&GT; ...
- 更改
InstanceContextMode
,可能你的ConcurrencyMode
。
- Make it static:
private readonly static ConcurrentDictionary<...> ...
- Change your
InstanceContextMode
, and possibly yourConcurrencyMode
.
我建议第一个选项,如果你不希望数以千计的电话,以每秒为您服务。
I'd recommend the first option if you're not expecting thousands of calls to your service per second.
MSDN有关于 InstanceContextMode 和一些信息< A HREF =http://msdn.microsoft.com/en-us/library/ms752230.aspx相对=nofollow>服务类的实例化。
MSDN has some information about InstanceContextMode and instancing of service classes.
如果您更改为 InstanceContextMode.Single
和 ConcurrencyMode.Multiple
你可以得到许多线程同时执行,你将需要担心同步访问。
If you change to InstanceContextMode.Single
and ConcurrencyMode.Multiple
you can get many threads executing at once and you will need to worry about synchronised access.
最主要的是要注意的是,当你查询/迭代在你的字典里,别人可能会修改。拼抢字典的快照,然后查询快照应该解决这个问题:
The main thing to watch out for is that when you're querying / iterating over your dictionary, someone else might modify it. Grabbing a snapshot of the dictionary and then querying the snapshot should get around that problem:
foreach (var x in configurationDictionary.ToArray())
{
....
}
或者,如果你使用LINQ:
Or, if you're using LINQ:
var someResult = configurationDictionary.ToArray().Where(...).Select(...)
这篇关于WCF方法共享一本字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!