I’ve got a WCF service where i’m trying to get callbacks working.
The Interface class looks like:
[ServiceContract(
SessionMode = SessionMode.Required,
CallbackContract = typeof(IPredatorEngineCallback))]
public interface IMyApplication
{
[OperationContract]
Boolean DoSomething(string mystring);
[OperationContract]
Boolean SubscribeToEvent();
}
public interface IMyApplicationCallback
{
[OperationContract (IsOneWay = true)]
void EventRaised(EventMessage myEventMessage)
}
EventMessage is class which is in a comms library shared by the server & client.
The implementation of this interface looks like:
[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple)]
public class MyApplication : IMyApplication
{
private List<IMyApplicationCallback> CallbackList = new List<IMyApplicationCallback>;
public Boolean DoSomething(string mystring)
{
// Do stuff
// Now Raise an event
PublishEvent();
return true;
}
public Boolean SubscribeToEvent()
{
IMyApplicationCallback IMAC = OperationContext.Current.GetCallbackChannel<IMyApplicationCallback>();
if (!Callbacklist.Contains(IMAC)
Callbacklist.Add(IMAC);
}
private void PublishEvent()
{
EventMessage myEM = new EventMessage();
// Populate myEM.Fields
Callbacklist.ForEach(delegate(IMyApplicationCallback callback)
{ callback.EventRaised(myEM); });
}
}
The idea is that a client can choose to subscribe to receiving events.
I built my app in stages and immediately prior to implementing callbacks, the service works fine. Calls made to DoSomething() do what they are supposed to do and return pretty much immediately.
Now that i’ve implemented CallBacks, if I comment out the RaiseEvents() line in DoSomething it returns immediately.
My Client code now calls SubscribeToEvent() first, then makes a second call to DoSomething().
If I uncomment it and follow through with the debugger, I see that DoSomething finishes nicely pretty much immediately – no delay. I’ve got both the client & WCF service running on same machine with VS2010. I follow it through to the closing } in DoSomething().
However, the callback function in my client isn’t called. After 1 minute it finally did get called and, I also got an error message stating that my original call had timed out. So I changed app.config to reduce to 10 seconds and the error message is:
"This request operation sent to http://localhost:8732/Design_Time_Addresses/MyApplication/MyAppication did not receive a reply in the configured timeout (00:00:09.987997). The time allotted to this operation may have been a portion of a longer timeout..."
So it appears that something is blocking the return of the first call; but the event has been raised because it eventually gets there. I suspect this is to do with locking/concurrencymode but i’ll admit that i’m lost for a solution.
On the client side i’ve used the proxy code generated by adding a service reference.
Server side app.config (relevant parts only):
</configuration>
Client side App.config:
<configuration>
<system.serviceModel>
<bindings>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_IPredatorEngine" closeTimeout="00:00:10"
openTimeout="00:00:10" receiveTimeout="00:10:00" sendTimeout="00:00:10"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" />
<security mode="Message">
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
</binding>
</wsDualHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8732/Design_Time_Addresses/MyApplication/MyApplication/"
binding="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_IMyApplication"
contract="MyApplication.IMyApplication" name="WSDualHttpBinding_IMyApplication">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
I assume i’ve not got something configured right. Can anyone spot it ?
After much searching and trial and error, I found that I needed to set the SynchronizationContext to false.
I also needed to set the following on the implementation of the Client side code: