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

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.

Related

WCF listen to Service Bus

I’m new to WCF and are having problems by finding the solution to my problem.
I have WCF set up that handles some logic (simple database handling).
I then have a web page (ASP.NET) that gets it data from WCF (and also sends it data to WCF).
Now I’m trying to use an Azure Service Bus to send data to WCF. It works fine when I go through the ASP.NET website. It works like this:
1, Client application sends data to Service Bus.
2, ASP.NET Webpage reads data from Service Bus.
3, ASP.NET Sends data to WCF.
4, WCF updates the database.
Everything works fine.
But, I would like the WCF to constantly listen to the Service Bus.
How do I do that?
In this scenario you have 2 options:
Use the WCF endpoint of the Service Bus Queues: http://blogs.msdn.com/b/tomholl/archive/2011/10/07/using-service-bus-queues-with-wcf.aspx
Use the service bus relay: https://www.windowsazure.com/en-us/develop/net/how-to-guides/service-bus-relay/
Depending on your scenario you'll use one of these options. The first scenario uses queueing. If your WCF service should be unavailable for any reason your client will still keep working (messages are delivered to the queue and picked up by the WCF service whenever available).
The second scenario will create a bridge between your client and a WCF service (could be on premises, in an other data center, ...). Since it's a direct connection you can receive a response. But keep in mind, if your WCF service is offline, your client will receive an exception and you'll need to handle that.

SignalR - Sending a Message from a WCF Project

I've followed the instructions from https://github.com/SignalR/SignalR/wiki/Hubs
entitled "Broadcasting over a Hub from outside of a Hub".
I got this method working from within an MVC Action in the same project. Requesting the Action sends the update to connected clients.
My problem is that I need to be able to send updates from another project, in particular a WCF Web Services project. My app has an API and a web component and when API users make calls that change things, these updates need to be pushed out to the Web clients via SignalR. And calling a web service with the same code as my Test Action doesn't work.
I also tried the same code inside an nunit unit test that didn't work either.
What do I need to do to make this same method described on the Wiki work for a WCF Project?
The easiest solution is probably to provide an API on your Web Application (use MVC or the new WebAPI) that broadcasts to all connected clients. Any other application (an NT Service, an NUnit test, ...) can call that API if it wants to send a message to the clients.
You can't expect SignalR to do anything if you aren't hosting a Hub either in a Web Application running under IIS, or another application hosting it directly.
If you need two-way communication from your separate application to your clients then simply make your application into a SignalR client too and have it communicate via the Web Application hosted SignalR to the clients and have it listen to messages from them too.
For example, here's how I have configured a complex Service + WebSite + Clients solution (ignore the purple for now):
The Live Web Server allows NT Services to connect and create SignalR Groups. NT Services send to those groups. Web browsers connect to a group and receive messages send to that group. In effect the middle box becomes a pubsubhub.
I cannot get exactly what you aim. But if I understood correctly you're trying to send some kind of notifications raised inside WCF services to SignalR clients.
If that's the case; I can suggest you my approach:
I have some WCF services and a SignalR hub in the same application server. IMHO, the best way to communicate WCF with SignalR hub is by using MSMQ.
When a notification occurs inside a WCF service, it puts the notification payload into MSMQ.
On the other end, SignalR hub listens the same queue. When a message put into the queue, it gets the content and broadcasts to the hub clients. Very easy and straightforward. No extra service/hub call at the server side.
SignalR hub can listen for new queue items by using System.Messaging.MessageQueue#ReceiveCompleted method. When this event raised, SignalR hub gets the queue item and broadcasts to its clients.

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 Service - automated or timed action

We have a WCF service, hosted on IIS7.5, that has to fetch some data from an external web service.
Is there some way to schedule this action to be started, in our WCF service, on a regular (timed) basis?
Thanks
Yes, create a Windows service which wakes up every x minutes or hours and makes the call.
The WCF service is not "always on" - it is typically only created when a request comes in and needs to be handled. But you would typically host your WCF service in either IIS, or self-host - which usually already means a Windows Service anyway.
I would recommend VisualCron which can schedule WCF and web service calls (amont other things).
With that tool you can supply dynamic parameters, retrieve output and parse it. If you want you can forward the data to a file, email etc.

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.