Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fire events to client using WCF

Tags:

c#

wcf

I know there are tutorials out there regarding WCF callbacks and events, but I'm having trouble getting them to actually work, or they're too complex. I'm a real beginner, so I would love to hear if anyone knows of any solid beginner tutorials that are aimed to what I'm trying to specifically figure out at the moment. Please forgive me if I use the wrong terminology (and please correct me), like I said I'm a beginner.

The Problem:

enter image description here

It might look more complicated than it really is. Basically what I'm trying to accomplish is:

  1. A Host with some local memory (lets say an array of 5 integers) and running a WCF service. that will listen for queries from the client AND fire an update (events?) to the client when one of these integers are changed (from an external source, such as user input via command prompt and Set()).
  2. A Client that can make direct queries to return one of these five integers or subscribe to a particular index of the host array.

What I Can Do:

I can set up the connection, but my service is limited to standalone functions. The client can make "queries", but is limited to remote function calling (such as "add", where all parameters are passed with the function and the processing is done internally).

What I'm Trying To Figure Out:

  1. How can I access some variables held in the host memory from the service contract functions? For example, how can I call a method from the client GetInt() that would simply return something stored in the application memory on the host?
  2. How can I push a "message" to the client from the host? For example, in the host, call TellClient(int x), which would call some function on the client side? Is this possible without running a service on both sides?

TLDR:

Host : Service <-> Client. Is there a way to push data (simply an int) to the client without the client calling any functions (no polling or queries)? Is there a way to have the WCF service access variables stored in instance of the host application without using static members? Can this be accomplished in a simple way?

Thanks for your help and time, I know I wrote a book. If anyone knows of any nice tutorials, please point me to them. But PLEASE - Don't point me to the Add(int x, int y) example where the client just calls add on the host and returns the result - I've already done this a few times over and it's not helping me grasp the real functionality of WCF. I'm really not trying to accomplish anything serious at this point, I'm really trying to keep it simple so I can learn what WCF can do, and I'm not finding the documentation to be very helpful. Thanks again everybody.

like image 607
Softerware Avatar asked Jun 04 '12 14:06

Softerware


People also ask

How will you specify a method is available to access by client in WCF?

With the service running, right click the project that will contain the WCF client proxy and select Add > Service Reference. In the Add Service Reference Dialog, type in the URL to the service you want to call and click the Go button. The dialog will display a list of services available at the address you specify.

What is callback in WCF?

Abstract: In WCF, callback is used to implement PUSH mechanism, so that delayed or long running operation results can be automatically pushed back to the client application. WCF actively supports callback to its client, over the instance context established.

Can we call WCF service from JavaScript?

To consume the WCF service methods from JavaScript, we need to expose them as the RESTful service methods that accept and return the data in either JSON or XML formats. This helps developers to consume the WCF services as easily as the REST services, and use them with the jQuery $.


2 Answers

Generally WCF is used in a request reply way, where clients make requests; and server replies. What you want to achieve is a "push and pull" service; or in Microsoft terms a duplex service.

In duplex services clients just connect to a service and service registers them into some internal list. And whenever an event(or something else) arises, it sends a message to the registered clients. The key terminology for WCF in the context of your question is "duplex services"(you may google it find many results). You may refer to the following tutorials;msdn or codeproject

For the second part of your question, the answer is yes. But this is not that simple. You need to write some "wcf behaviors", for instance an IInstanceProvider may help you. For all requests, you may just create the service instance yourself, with desired parameters injected into the service instance. Referring to the following may help: stackoverflow or msdn.

The question is a bit broad thus I am not 100% sure whether this is a direct answer. But at least using the keywords provided you may find the right direction.

like image 170
daryal Avatar answered Nov 13 '22 10:11

daryal


Have you tried this article yet? http://msdn.microsoft.com/en-us/magazine/cc163537.aspx#S6. I thought it was a decent explanation on callbacks. It discusses a publish/subscribe framework which is a solution to your "no polling or queries" requirement.

like image 24
insipid Avatar answered Nov 13 '22 09:11

insipid