tags:

views:

188

answers:

2

Is it possible to pass callback objects (with callback methods) to a wcf service method?

Let's presume i have the following class:

 class Callback
    {
         public Callback(){}
         public int GetSomeData(int param)
         {
           return param;
         }
    }

Is it possible somehow to make a call like :

WCFServiceProxy proxy = new WCFServiceProxy();
Callback myCallback = new Callback();
proxy.SomeMethod(myCallback);

and have the service call GetSomeData() implemented on the client side? Or what would be a working solution for this?

+1  A: 

see Duplex Services

ArsenMkrt
A: 

Yes, you can do that. You have to define a secondary interface that serves as the callback contract.

[ServiceContract]
public interface ICallback
{
    [OperationContract(IsOneWay=true)]
    void InvokeCallback();
}

[ServiceContract(CallbackContract=typeof(ICallback)]
public interface IContract
{
    [OperationContract]
    void DoSomething();
}
[ServiceBehavior]
public class MyService : IContract
{
    void DoSomething() { }
}

That's the basic approach. I would strongly suggestion looking at Juval Lowy's website, IDesign.net. His downloads section has several examples of how to do this.

Matt Davis
Finally i implemented it with a callback contract and it works great. What i also needed is to transfer data in my callback object and i've done that by adding [OperationContract] properties to the callback object.
Elz