Reputation: 620
Background I am creating an ASP.net application which uses a custom library (not yet publicly released, but soon) to provide a long polling handler to a javascript client in jQuery. The client will call the handler, wait until the handler returns some data, display it and then hit the handler again... wash, rinse, repeat.
The handler's job is to take the request, stick it into a collection (normally a List<>
) and wait for an event, containing the data to return to the awaiting requests. I have tested this, using a simple event within a static class on my server and this all works fine.
Problem In order for the messages to be received by all clients, in all AppDomains on my server, I have created a simple C# console app - which is running in the background and provides a DataContract over WCF (NamedPipe). I then created a client object in ASP.net which can talk to the console app over the WCF interface.
I can synchronously pull/push messages into my cache - and this is working fine. However, when I try and use the Asynchronous pattern on the WCF contract, the EndMethod
is fired before I ask it to, straight after returning the IAsyncResult to the BeginMethod.
I cannot see what is wrong - an Google has plenty of resources which show weak examples, and they dont help.
What I would like Is their anyway to bind to an event over WCF through the data contract? That way I can just attach to the event and be done with it. New messages into the cache trigger my event, which triggers the async handler to return all the awaiting requests.
Is this possible, and if so does anyone have any samples/tutorials/examples?
Code To Follow...
Upvotes: 0
Views: 868
Reputation: 67075
I know this is possible as I have done this, and know that there is limited info on this out there (I plan on creating a blog post soon). Basically, you need to create Subscribe/Unsubscribe
WCF methods, which will keep your port open.
Your server code should look like this:
[ServiceContract(SessionMode = SessionMode.Required,
CallbackContract = typeof(IProcessorCallBack))]
public interface IProcessor
{
[OperationContract]
Boolean SubscribeToEvents();
[OperationContract]
Boolean UnsubscribeToEvents();
}
public interface IProcessorCallBack
{
[OperationContract(IsOneWay = true)]
void OnEvent(EventArgs args);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class DDSpeechMikeProcessor:IProcessor
{
private readonly List<IProcessorCallBack> _eventListeners =
new List<IProcessorCallBack>();
public bool SubscribeToEvents()
{
var listenerToCallBacks =
OperationContext.Current.GetCallbackChannel<IProcessorCallBack>();
if(!_eventListeners.Contains(listenerToCallBacks))
_eventListeners.Add(listenerToCallBacks);
}
public bool UnsubscribeToEvents()
{
var listenerToCallBacks =
OperationContext.Current.GetCallbackChannel<IProcessorCallBack>();
if (_eventListeners.Contains(listenerToCallBacks))
_eventListeners.Remove(listenerToCallBacks);
}
//This is your server listening for the main event
//which it will pass on to all listeners
void OnEvent(EventArgs args)
{
foreach(var listener in _eventListeners)
{
try
{
listener.OnEvent(args);
}
catch (Exception exception)
{
RemoveListenerIfBadCommunication(listener, exception);
}
}
}
//If a listener did not unsubscribe before shutting down you will get exceptions
private void RemoveListenerIfBadCommunication(IProcessorCallBack listener,
Exception exception)
{
if (exception.GetType() == typeof(CommunicationException)
|| exception.GetType() == typeof(CommunicationObjectAbortedException)
|| exception.GetType() == typeof(CommunicationObjectFaultedException)
)
_eventListeners.Remove(listener);
}
}
In your client code:
public class Client : IProcessorCallBack
{
DuplexChannelFactory<IProcessor> _processorFactory;
IProcessor _processor
void OpenProcessor()
{
_speechMikeProcessorFactory = new DuplexChannelFactory<IProcessor>(
this,
new NetNamedPipeBinding(),
new EndpointAddress(baseUri + @"/" + HostName));
_processor = _speechMikeProcessorFactory.CreateChannel();
_processor.SubscribeToEvents();
}
void OnEvent(EventArgs args)
{
//Do Stuff
}
}
Let me know if I need to explain anything in more detail
Upvotes: 2