views:

498

answers:

3

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.

A: 

Is there a lock somewhere in your service function?

Jake Pearson
No there is no locking.
Mykroft
A: 

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.

John Saunders
See my edits above.
Mykroft
+4  A: 

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

marc_s
Please take a look at my edit because I did get it working fine without making any other changes than those i mentioned. I'm not sure why it worked though.
Mykroft
OK, excellent. Yes, hosting a WCF service inside a Winforms app on the main UI thread isn't really a great idea.
marc_s
It's weird though because I have another app that does pretty much the same thing with a NetTCPBinding and it works without these problems.
Mykroft
Ah, yes, the mysteries of WCF..... :-)
marc_s