16

How does ServiceStack handle concurrent calls? I'm looking for equivalent of ConcurrencyMode.Multiple in WCF.

My WCF services have this attribute set:

   [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]

Do I need to enable anything in ServiceStack to get it to use multiple threads for each call?

1 Answer 1

16

ServiceStack doesn't have a configurable concurrency model per AppHost, it is dependent upon the AppHost you choose to host your ServiceStack services with:

ASP.NET Host (AppHostBase)

For ASP.NET web hosts, ServiceStack doesn't create any new threads itself, the requests are simply handled on the same IIS/Nginx/etc ASP.NET HTTP WebWorker that handles the request.

HttpListener Self-Host (AppHostHttpListenerBase)

ServiceStack only creates a new thread on Startup when you call new AppHost().Start(url). There are no new threads created at run-time, i.e. the request is handled on the HttpListener async callback thread.

HttpListener Long Running Self-Host (AppHostHttpListenerLongRunningBase)

This is another Self-Host HttpListener option for ServiceStack that uses its own managed ThreadPool to execute the request on (free-ing up the HttpListener async callback thread). The default poolSize of the ThreadPool is 500 threads, though this is configurable in the AppHostHttpListenerLongRunningBase(poolSize) constructor.

RedisMQ Host (RedisMqServer)

A good option for managing long-running tasks is to delegate requests to a Redis MQ Host which is a light-weight MQ Server allowing you to defer and process requests in managed background threads. By default the RedisMqServer spawns a single background thread for each Message type (i.e. Request), though this is configurable on start-up, e.g: in the example below 2 background threads are used to handle PostTwitter requests, whilst only 1 background thread each is used to process CallFacebook and EmailMessage requests:

mq.RegisterHandler<PostTwitter>(ServiceController.ExecuteMessage, noOfThreads:2);
mq.RegisterHandler<CallFacebook>(ServiceController.ExecuteMessage);
mq.RegisterHandler<EmailMessage>(ServiceController.ExecuteMessage);
4
  • So with an AppHostBase hosted on IIS, if 4 clients simultaneously call the same service are each served at the same time, or are they handled one after another? I initially thought WCF would handle each request at the same time, but in fact by default it works sequentially processing one request at a time. A very interesting article explaining the WCF concurrency model is here: codeproject.com/Articles/89858/…
    – Dylan
    Jan 9, 2013 at 17:06
  • IIS handles the multi-threading of the HTTP Worker threads just as it does with every other ASP.NET web application (which is all that ServiceStack is when hosted in ASP.NET). IIS is a multi-threaded Web Server which handles requests concurrently.
    – mythz
    Jan 9, 2013 at 17:17
  • Is this answer just copy'n'paste from here without indication of source or was it the other way around?
    – mono68
    Feb 23, 2017 at 12:39
  • 1
    @mono68 I'm the author of both, initially answered here before adding it to the docs.
    – mythz
    Feb 23, 2017 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.