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…
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/UnsubscribeWCF methods, which will keep your port open.Your server code should look like this:
In your client code:
Let me know if I need to explain anything in more detail