WCF服务和线程线程、WCF

2023-09-03 13:03:37 作者:他是暖阳深拥必伤

我创建了一个简单的WCF(.NET 3.5)服务,它定义了10份合约这是基本的计算所提供的数据。目前,我预计比较少的客户打电话到一些合同。如何使服务更加敏感?我该服务将等待,直到它处理一个请求,去到下一个的感觉。 我怎样才能在WCF中使用多线程来加快速度?

I have created a simple WCF (.NET 3.5) service which defines 10 contracts which are basically calculations on the supplied data. At the moment I expect quite few clients to make a call to some of these contracts. How do I make the service more responsive ? I have a feeling that the service will wait until it process one request to go to the next one. How can I use multithreading in WCF to speed things up ?

推荐答案

虽然我同意的贾斯汀的回答,我相信更多的光可以在这里揭示WCF是如何工作的。

While I agree with Justin's answer, I believe some more light can be shed here on how WCF works.

您做具体的语句:

我有一种感觉,该服务将   等到它处理一个请求   去下一个。我如何使用   多线程的WCF提升速度   了?

I have a feeling that the service will wait until it process one request to go to the next one. How can I use multithreading in WCF to speed things up ?

服务(多少电话可同时进行)的并发性依赖于ConcurrencyMode值的 ServiceBehavior 连接到服务。默认情况下,该值为 ConcurrencyMode.Single ,意义,它会一个接一个序列化的呼叫。

The concurrency of a service (how many calls it can take simultaneously) depends on the ConcurrencyMode value for the ServiceBehavior attached to the service. By default, this value is ConcurrencyMode.Single, meaning, it will serialize calls one after another.

不过,这可能不是你想尽可能多的一个问题。如果你的服务的InstanceContextMode等于InstanceContextMode.PerCall那么这是一个不是问题的问题;你的服务的一个新实例将每次通话被创建,而不是用于其他任何电话。

However, this might not be as much of an issue as you think. If your service's InstanceContextMode is equal to InstanceContextMode.PerCall then this is a non-issue; a new instance of your service will be created per call and not used for any other calls.

如果你有一个单身或基于会话的服务对象,然而,再调用的服务实现的实例将被序列化。

If you have a singleton or a session-based service object, however, then the calls to that instance of the service implementation will be serialized.

您可以随时修改 ConcurrencyMode ,但要注意,如果你这样做,你将不得不手动处理并发性问题,并访问你的资源,你已经明确告诉WCF,你会这么做。

You can always change the ConcurrencyMode, but be aware that if you do, you will have to handle concurrency issues and access to your resources manually, as you have explicitly told WCF that you will do so.

重要的是不要改变这些,只是因为你认为的,这将导致更高的性能。虽然没有那么多并发,你服务的实例化方面是服务的身份非常的一部分(如果它是会话或不基于会话的),并改变它们影响客户消费的服务,所以不做淡然。

It's important not to change these just because you think that it will lead to increased performance. While not so much for concurrency, the instancing aspect of your service is very much a part of the identity of the service (if it is session or not session-based) and changing them impacts the clients consuming the service, so don't do it lightly.

当然,这说没什么与否的code,它实际上的执行的服务是有效的。这是肯定的东西,需要寻找到,当你表明这样的话。

Of course, this speaks nothing to whether or not the code that is actually implementing the service is efficient. That is definitely something that needs looking into, when you indicate that is the case.