Welcome to MSDN Blogs Sign in | Join | Help

.net ready !!!

        Maxime Lamure
          MCS France
How to: Callback function with WCF

 

The goal is to inform the client that the server process has progressed:

Situation: You have to update a lot of data on the server. You can use a web Service which will update your database. Each time a field is updated, the server can inform the client with the callback function.

Code on the Server side:

Add an interface :

public interface IProgress
{
  [OperationContract(IsOneWay = true)]
  void Update(int value);
}

Call the methode when you want to inform the client:

public boolean TransferMoney(int[] values)
{
  IProgress iProgress = OperationContext.Current.GetCallbackChannel<IProgress>();
  int val;
  foreach (val in values)
  {
    iProgress.Update(val);
    UpdateData(val); //Update the database
  }
  return true;
}

And change your Service Contract:

[ServiceContract(CallbackContract=typeof(IProgress), SessionMode=SessionMode.Required)]

Code on the Client side:

Create the class which implements the Interface and add the method which will be called by the server

class Handler : IBankingServiceCallback
{
  public void Update(int value)
  {
    Console.WriteLine(“DataBase updated with: ”+value);
  }
}

Change the call of the proxy with the InstanceContext in Parameter:

MyProxy proxy = new MyProxy (new InstanceContext(new Handler()));

Posted: Monday, March 19, 2007 7:29 PM by Maxime LAMURE
Filed under: ,

Comments

Dom said:

I've been able to follow you until you last line of code:

MyProxy proxy = new MyProxy (new InstanceContext(new Handler()));

Not sure what you are talking about ... very new to wcf.  A little more details there would have helped me.

# June 19, 2007 3:34 PM

Dom said:

That's what I needed:

DuplexChannelFactory<IServer> channelFactory = new DuplexChannelFactory<IServer>(new InstanceContext(new Handler()), netTcpBinding, "net.tcp://localhost:6000/server");

           IServer server = channelFactory.CreateChannel();

# June 19, 2007 3:47 PM

Glenn Channon said:

Hi,

What is the best way of using WCF and providing feedback to the client using aspx as the UI?

Thanks

# January 22, 2008 4:00 PM

Stephen said:

Hi,

Awesome example.  I do have one problem though...

The line

IProgress iProgress = OperationContext.Current.GetCallbackChannel<IProgress>();

fails because the OperationContext.Current is "null".

Thanks,

Steve

# September 14, 2008 5:28 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker