Mykroft
Mykroft

Reputation: 13415

Why is my Winforms-hosted WCF service single threaded?

I have a WCF service that I'm using to replace an old ASP.NET web service. The service appears to be working fine but it is unable to handle simultaneous requests for some reason. My implementation of the service has the following properties:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class HHService : IHHService

My host declaration looks like this:

baseAddress = new Uri("http://0.0.0.0:8888/HandHeld/");
host = new ServiceHost(typeof(HHService), baseAddress);

ServiceMetadataBehavior behavior;
behavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (behavior == null)
{
    behavior = new ServiceMetadataBehavior();
    behavior.HttpGetEnabled = true;
    behavior.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
    host.Description.Behaviors.Add(behavior);
}
host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
host.AddServiceEndpoint(typeof(IHHService), new BasicHttpBinding(), "HHService.asmx");
HHService.LogMessage += new EventHandler<HHService.LogMessageEventArgs>(HHService_LogMessage);
host.Open();

The service runs and returns correct results, but if two clients try to make a call at the same time one client will block until the other is finished rather than the calls executing together. I'm not using any configuration files. I'm trying to do everything programmatically. Do i have something setup incorrectly that's causing this behavior? I've run other services using the NetTCPBinding without this problem.

EDIT: In response to John Saunders: I'm not familiar with any ASP.NET compatibility mode. I'm not using any session state the service is stateless it just processes requests. Aside from the implementation of the actual methods everything else I've done is in the code listed here.

Possible Solution:

I was calling the host.Open() function from the form_load event of the main form. I moved the call to a separate thread. All this thread did was call host.Open() but now the service appears to be behaving as I would expect.

Upvotes: 5

Views: 1681

Answers (4)

aboy021
aboy021

Reputation: 2305

This is answered in another question:

[ServiceBehavior(UseSynchronizationContext = false)]

WCF in Winforms app - is it always single-threaded?

Upvotes: 0

marc_s
marc_s

Reputation: 754268

If your instance context mode is PerCall, then your server is always single-threaded, since by definition, every call gets a new server instance.

This works okay in a IIS environment, where IIS can spin up several server instances to handle n concurrent callers, one each as a single-threaded server for each incoming request.

You mention in one of your comments your hosting your WCF inside a forms app - this might be a design decision you need to reconsider - this is not really optimal, since the Winforms app cannot easily handle multiple callers and spin up several instances of the service code.

Marc

Upvotes: 4

John Saunders
John Saunders

Reputation: 161773

Are you using ASP.NET compatibility mode? Session state?


My next question would be: what makes you think it's single-threaded? How did you determine that, and what test do you use to prove that you have not solved the problem? Could be a false positive.

Upvotes: 0

Jake Pearson
Jake Pearson

Reputation: 27717

Is there a lock somewhere in your service function?

Upvotes: 0

Related Questions