Wcf Router - Duplex services- resubscription - wcf

I have a duplex wcf service which holds (in memory) list of callback references to its clients. There are two methods Subscribe and Unsubscribe on wcf service.
Its all working fine with One wcf server and multiple clients. Problem started when I introduced two wcf server with load balancing using Wcf router service.
For eg- I have two wcf servers(wcf1 and wcf2) and one router server (r1). When client call "Subscribe" to router endpoint, it broadcasts that request to both the wcf1 and wcf2 servers. So that both of them hold a callback reference to client. Problem starts when one of the wcf server is down. If wcf1 server goes down then all the subscriptions are lost. So when it comes back alive then there are no subscriptions available for it to do the callbacks and there is no way to notify clients to resubscribe.
I tried listening to fault event on the client side. That doesnt get triggered because its connecting to the router endpoint. That event will fire only if router server is down.
Has anyone faced similar issue to mine? Am I doing something wrong?

The only way of handling this is to persist the callback list in some kind of share-able resource, such as a database, a cache, or even a file on a shared drive. This can be accessed by each instance of your service on startup, so all callbacks are loaded back in in the event of failure.
Sorry this is probably not what you want to here.

Related

WCF duplex channel for inter-process communication

I have system like this:
Windows service (WCF, data/events) <-> Web app <-> Web client
I need simultaneous response for clients requests. I have some events from service for clients too. So duplex channel is the way to go. But I need high throughput, because clients calls simultaneously.
Request/reply approach
In order not to serialize channel requests I need more channels for parallel calls, right? But how to handle callback channel then? Ho to keep it still open for receiving events, even on channel errors?
OneWay approach
On channel should be enough (no waiting for data preparation), but how to link data sent to callback with original request, to be able to compose response for client?
What is the way to go? Thank you.
In a simple case, when a web client sends a request to the web app, and web app (possibly) sends a request(s) to WCF service, there's no need in duplex binding at all.
As for events, raised by the service to be fired in Web client, I'd suggest to use a message broker which supports WebSockets - for example RabbitMQ. It has a plugin compatible with WebSockets and WCF binding.
Putting things together, one can create a RabbitMQ server, which accepts messages from WCF service and sends it to Web client, which subscribes to the event feed from Javascript.

How to find out the address of multiple clients connected to a WCF duplex service?

I am currently developing a WCF duplex Service for 2 clients. The first client would be an asp.net webpage which upon receiving a posting, it will send the data over to the service. When the service receives the data, it will then AUTOMATICALLY send it to the second client which is a winform app through the callback channel...
To make it simpler.
Asp.net will invoke the wcf
The wcf will reside on the iis server, same as the asp.net
WCF will require to send a data to the windows form application that is running on a client side. Only 1 instance of this application will be run at a time.
Your service should know nothing about the clients attached to it. Doing so pretty much breaks the intention of WCF.
A better solution might be to have your clients subscribe to "events" that your service can fire off. Or maybe the client can provide some information in their requests that indicates a service and method to call back to when needed.

Duplex WCF service channels continuously alive (WSDualHttpBinding)

The arhitecture:
console application that contains a wcf duplex service;
windows app consumers; every app subscribe to Duplex, so we have a list of subscribers
and the service must send notifications to some of them in case that some events apper;
The problem:
how to maintain the connection alive continuously so that the service can send notifications to clients ?
I found that there are 2 channels for this binding. I need to have permanently the channel from service (for callbacks) open.
Thanks;
I had the same problem than you, to solve it you need to implement a keep alive method. A dummy call to your service is kind of stupid to have a 24/7 WCF service because is not really the best architecture way of WCF but we know sometimes what we need is not always the best thing.
So yep, just implement a call to the WCF service every 5 minutes and by doing that you keep alive the connection. Maybe you can have others problems too, in my case I don't have a lot of subscribers of the WCF service so this solution was fine.
As long as a client is connected to the service, the callback channel will be alive. Once the client disconnects (or the channel gets faulted), the callback channel will close.

Can I listen to a WCF event from a web client?

Can I listen to a WCF event from a web client? Is this possible? I am not talking about call backs, I want the WCF service to raise and event and the web client to be able to listen. Is there a good example of this in C#?
There are no events in WCF. If you want mimic event you still have to call some operation exposed on all clients = you must call WCF service or callback exposed on client.
What do you mean by web client? Do you mean javascript code running in web browser? In such case no you can't achieve that with WCF. You can only use AJAX calls from borowser and continuously poll the service for possible "event".
If you mean ASP.NET application then the answer is theoretically yes, practiacally it will be pretty hard. The reason is that in ASP.NET you handle only current HTTP request by some handler - for example Page. The lifetime of the handler is only for serving the single request. Due to that using duplex service doesn't make to much sense because for receiving callbacks by duplex service your client proxy must live. If you open the proxy in Page it will die after serving the request. If you open the proxy in separate thread you must somehow corelate incomming callbacks to actual client but the client still have to poll the web server to be notified about callbacks. Similar situation will be with exposing the service on ASP.NET application.
Difference between asynchronnous and duplex calls is big. In asynchronnous pattern single request always have single response. Resonse is not sent without request. In duplex pattern you can make single request and receive thousands callback from server.

WCF Duplex Interaction with Web Server

Here is my scenario, and it is causing us a considerable amount of grief at the moment:
We have a vendor web service which provides base level telephony functionality. This service has a SOAP api, which we are leveraging to build up a custom UI that is integrated into our in house web apps. The api functions on 2 levels. You make standard client calls into the service to initiate actions, such as Login, Place Call, Hang Up, etc. On a different thread, the service sends events back to the client to alert the user of things that are occurring on the system (agent successfully logged in, call was disconnected, etc).
I implemented a WCF service to sit between the web server and the vendor service. This WCF service operates in duplex mode, establishing a 2 way connection with the web server. The web server makes outbound calls to the WCF service, which routes them to the vendor's web service. Events are received back to the WCF service, which passes them onto the web server via a callback channel on the WCF client. As events are received on the web server, they are placed into a hash table with the user's name as the key, and a .NET queue as the value to hold the event. Each event is enqueued to the agent who owns it.
On a 2 second interval, the web page polls the web server via an ajax request to get new events for the logged in user. It hits the hash table for the user key, dequeues any events that are present, and serializes them back up to the web page. From there, they are processed in order and appropriate messages are displayed to the user.
This implementation performs well in a single user scenario. The second I put more than 1 user on the system, I start getting frequent timeouts with the following CommunicationException:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
We are running Windows Server 2008 R2 both servers. Both the web app and WCF service are running on .NET 3.5. The WCF service is running under the net.tcp protocol in duplex mode. The web app is ASP.NET MVC 2.
Has anyone dealt with anything like this scenario? Is there a more efficient way (or a widely accepted pattern) to implement this?
EDIT: One thing I forgot to mention - my thought is that the increased traffic (adding additional users) with only 1 dedicated callback channel is causing locking, which then triggers the timeout. There can be up to 10 consecutive callbacks from the service within any 5 second interval.