views:

1747

answers:

2

So I'm hosting WCF service in a WinForms application. I have the following

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, 
         InstanceContextMode = InstanceContextMode.PerCall)]
public class Test : ITest
{
    public string TestIt(string input)
    {
        Thread.Sleep(5000);
        return "test";
    }
}

I'm using Named Pipes and have two instances of another application that act as clients to the above WCF service (running in a WinForms application). I thought based on the ConcurrencyMode setting of Multiple that when Client1 calls the Test Service, Client2 doesn't have to wait till the first call is complete. However, when Client1 calls TestIt, Client2 blocks until the call from Client1 is complete!?!?! Shouldn't it make a new instance each time based on the above settings?

Also, is the best way to keep a WinForms application that is hosting a WCF service responsive is by running the WCF service on a separate thread?

NOTE: Setting [CallbackBehavior(UseSynchronizationContext = false)] on the Test class does not alleviate the problem. The service still only responds to one request at a time.

+2  A: 

Sounds like you want to set this

http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.usesynchronizationcontext.aspx

to false. By default, if there is a synchronization context when service.Open() happens, WCF will pick it up and use it. But if you don't want that feature, this flag is how to turn it off.

Brian
Setting [CallbackBehavior(UseSynchronizationContext = false)] on the Test class does not alleviate the problem. The service still only responds to one request at a time.
AKoran
A: 

After digging into this a bit more the only way I was able to get this to work properly was to start the ServiceHost on a separate thread in the WinForms application. If you don't do that setting the ConcurrencyMode and InstanceContextMode attributes does nothing.

AKoran
Brian's answer is correct here AKoran. To stop you from running into thread synchronization issues, by default if you use WCF in a winform, it will synchronise on the winform. Set UseSynchronizationContext=false and you will get WCF to make a thread for you.
Spence
I guess I must be doing something else wrong then because setting these attributes still blocks the entire WinForm app: [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)] [CallbackBehavior(UseSynchronizationContext = false)]
AKoran
Ahh, sometimes when you stare and something too long you lose it...I was using CallBackBehavior instead of ServiceBehavior. You are correct. Thanks again everyone!
AKoran
Feel free to give me a check mark and mark my answer as correct then :)
Brian
I tried already! Apparently I don't have enough "reputation" to check or uncheck marks. When/If I ever get there I'll be sure to come back and give you a mark!
AKoran