WCF InstanceContextMode. Numerous problems

So, I host the 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 use Named Pipes and have two instances of another application that act as clients in the above WCF service (works in WinForms application). I thought that based on the ConcurrencyMode Multiple parameter, when Client1 calls the test service, Client2 should not wait for the first call to complete. However, when Client1 calls TestIt, Client2 blocks until the call from Client1 is completed!?!?! Shouldn't he create a new instance each time based on the above settings?

Also, is the best way to save a WinForms application that runs the WCF service is to start the WCF service in a separate thread?

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

+4
source share
3 answers

It looks like you want to install this

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

- false. By default, if there is a synchronization context, when service.Open () occurs, WCF picks it up and uses it. But if you do not want this feature, this flag is how to disable it.

+9
source

After you delved into this a bit, the only way I was able to work properly was to run ServiceHost in a separate thread in the WinForms application. If you do not, the ConcurrencyMode and InstanceContextMode attributes will not do anything.

+4
source

I had the same problem.

My class that implemented Callback also contained methods for the wcf client, so when I called some method from the remote service and the service, called the callback method, I created a dead end.

 [CallbackBehavior(UseSynchronizationContext = false, ConcurrencyMode = ConcurrencyMode.Multiple)] public class AppContext : ICustomerOrderCallback { //WCF Proxy client private CustomerOrderClient _client = null; public AppContext() { InstanceContext context = new InstanceContext(this); _client = new CustomerOrderClient(context); _client.Subscribe(); //Remote method for subscribing callback } public void SendMessage(string message) { //Calling Remote method _client.SendMessage(message); } //....code //callback method public void OnMessageReceived(string message) { //.....code } } 

So, I created a separate class for the callback, added the CallBehavior attribute to it, and everything worked fine.

 public class AppContext { private CustomerOrderClient _client = null; private MyCallbackClass _myCallback = null; public AppContext() { _myCallback = new MyCallbackClass(); InstanceContext context = new InstanceContext(_myCallback); _client = new CustomerOrderClient(context); _client.Subscribe(); } public void SendMessage(string message) { _client.SendMessage(message); } } [CallbackBehavior(UseSynchronizationContext = false, ConcurrencyMode = ConcurrencyMode.Multiple)] public class MyCallbackClass : ICustomerOrderCallback { public void OnMessageReceived(string message) { //.....code } } 
+1
source

All Articles