Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What steps do I need to take to use WCF Callbacks?

I am trying to learn WCF. I have a simple client and server application setup and upon pressing a button on the client, it gets an updated value from the server.

My next step is I am trying to do a callback from the server to the client to update its value. I have poured through many examples, and they just seem too big and confusing. Is there anyone that can give my just the simplest example of its implementation in C#?

I keep looking through examples online and I just do not understand what it takes? Of course I could copy the example line by line but that does me no good because I still don't what to implement if I wanted to do this in my own code.

Could someone please help me with a very simple example on what steps I would need to take and what I would need to do in the server code and then in the client code to make this happen?

Thank you

like image 947
kevin bailey Avatar asked Jun 25 '09 14:06

kevin bailey


People also ask

What is a WCF callback?

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.

What is WCF service?

Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application.


2 Answers

Here is about the simplest complete example that I can come up with:

public interface IMyContractCallback {     [OperationContract]     void OnCallback(); }  [ServiceContract(CallbackContract = typeof(IMyContractCallback))] public interface IMyContract {     [OperationContract]     void DoSomething(); }  [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)] public class MyService : IMyContract {     public void DoSomething()     {         Console.WriteLine("Hi from server!");         var callback = OperationContext.Current.GetCallbackChannel<IMyContractCallback>();         callback.OnCallback();     } }  public class MyContractClient : DuplexClientBase<IMyContract> {     public MyContractClient(object callbackInstance, Binding binding, EndpointAddress remoteAddress)         : base(callbackInstance, binding, remoteAddress) { } }  public class MyCallbackClient : IMyContractCallback {     public void OnCallback()     {         Console.WriteLine("Hi from client!");     } }  class Program {     static void Main(string[] args)     {         var uri = new Uri("net.tcp://localhost");         var binding = new NetTcpBinding();         var host = new ServiceHost(typeof(MyService), uri);         host.AddServiceEndpoint(typeof(IMyContract), binding, "");         host.Open();          var callback = new MyCallbackClient();         var client = new MyContractClient(callback, binding, new EndpointAddress(uri));         var proxy = client.ChannelFactory.CreateChannel();         proxy.DoSomething();         // Printed in console:         //  Hi from server!         //  Hi from client!          client.Close();         host.Close();     } } 

A few namespaces will need to be included in order to run the example:

using System; using System.ServiceModel; using System.ServiceModel.Channels; 
like image 173
Ray Vernagus Avatar answered Oct 16 '22 10:10

Ray Vernagus


Grab a copy of "Programming WCF Services, 2nd Edition" by Juval Lowy. There are large sections of the book devoted to Callback operations. In Chapter 5, start on page 214. In the chapter on Concurrency Management (ch. 8) there's even more information.

"Programming WCF Services" is more or less the WCF "bible."

like image 35
Tad Donaghe Avatar answered Oct 16 '22 11:10

Tad Donaghe