WCF Service and Streams

I created a simple WCF service (.NET 3.5) that defines 10 contracts, which are mainly calculated based on the data provided. At the moment, I expect very few clients to turn to some of these contracts. How to make the service more responsive? I have the feeling that the service will wait while it processes one request in order to proceed to the next. How to use multithreading in WCF to speed up work?

+6
multithreading c # wcf
source share
3 answers

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

You make a specific statement:

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

A serviceโ€™s concurrency service (the number of calls it can receive at a time) depends on the ConcurrencyMode value for the ServiceBehavior attached to the service. By default, this value is ConcurrencyMode.Single , that is, it will serialize calls one by one.

However, this may not be as much as you think. If your InstanceContextMode service is equal to InstanceContextMode.PerCall then this is not a problem; A new instance of your service will be created for each call and will not be used for other calls.

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

You can always change the ConcurrencyMode , but keep in mind that if you do this, you will have to handle the concurrency problems and access your resources manually since you explicitly told WCF that you would do it.

Itโ€™s important that you donโ€™t change them just because you think it will lead to better performance. Although not so much for concurrency, the installation aspect of your service is a very large part of the serviceโ€™s identification information (if it is session-based or not session-based), and changing them affects customers who consume this service, do it easily.

Of course, this does not say anything about whether the code that actually implements the service is effective. This is definitely what you need to study when you indicate that it is.

+18
source share

This is definitely a pre-mature optimization. First do your service and see if there is a problem or not.

I think you will find that you are not worried. The server will not block a single request because this request is processing. IIS / WCF should handle things for you beautifully as it is.

+3
source share

I am not familiar with WCF, but can the process be asynchronous? If you expect a huge amount of data and heavy computing, one option would be to send an id , calculate the values โ€‹โ€‹in a separate thread, and then provide a method to return the result using the initial id .

Something like:

  int id = Service.CalculateX(...); ... var y = Service.GetResultX(id); 
+1
source share

All Articles