Hi
I'm developing a WCF service, which defines a callback contract through which the server can call a method on the client. Below is a sample code:
[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = (typeof(ISampleServiceCallback)))]
public interface ISampleService
{
[OperationContract(IsInitiating = true, IsTerminating = false, IsOneWay = false)]
bool Register(int controllerId);
[OperationContract(IsInitiating = false, IsTerminating = true, IsOneWay = true)]
void Unregister();
}
[ServiceContract]
public interface ISampleServiceCallback
{
[OperationContract(IsOneWay = false)]
bool ReceiveMessage(string message);
}
The problem arises when the client on exit fails to call the Unregister() method - becouse of a crash or something, so the server still has the reference to the client's callback channel. When the server calls the ReceiveMessage method on the client, that method blocks for the time specified by the "sendTimeout" in the app.config before throwing an exception. I tried using the following approach to call the callback method
public delegate bool ReceiveDelegate(string message);
public void NotifyClient(string message)
{
ReceiveDelegate receiver = new ReceiveDelegate(_callback.ReceiveMessage); //_callback is a reference to the callback channel
receiver.BeginInvoke(message, new AsyncCallback(onReceiveCompleted), receiver);
}
private void onReceiveCompleted(IAsyncResult result)
{
bool ok = false;
try
{
ok = ((ReceiveDelegate)result.AsyncState).EndInvoke(result);
}
catch(Exception ex)
{
string error = ex.Message;
}
}
In the example above, the receiver.BeginInvoke()
still blocks the calling thread - which is wird becouse when I tried calling another long-lasting function the same way and the call to BeginInvoke did not block.
So my question is: How can I define and implement the ReceiveMessage method as asynchronous, so it will not block the calling thread.
Any idea will be appreciated
Uros