Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Event Declaration

Tags:

wcf

I see that WCF doesn't directly use events and instead uses OneWay delegate calls, but can someone show me a simple example on how to do this?

Here is what I have setup right now:

    [OperationContract(IsOneWay = true)]
    void OnGetMapStoryboardsComplete(object sender, List<Storyboard> results);
like image 670
Eric Packwood Avatar asked Jul 18 '26 23:07

Eric Packwood


1 Answers

Assuming your callback contract interface is called IMyServiceCallback, your service would execute the following code when it wanted to raise the event:

IMyServiceCallback callback = OperationContext.Current.GetCallbackChannel<IMyServiceCallback>();
callback.OnGetMapStoryboardsComplete(...);

I found this article very helpful. It describes a transient event system and a persisted event system, either of which should satisfy any and all event scenarios, IMO.

HTH

To set up the callback contract:

interface IMyServiceCallback
{
    [OperationContract(IsOneWay = true)]
    void OnGetMapStoryboardsComplete(object sender, List<Storyboard>);
}

Then you need to indicate on your service contract that it is using this callback:

[ServiceContract(CallbackContract = typeof(IMyServiceCallback))]
interface IMyService
{
    // ...
}

Once you have done that and implemented your service, create a reference to the service. The client will then have to include a class that implements IMyServiceCallback:

class EventHandler : IMyServiceCallback
{
    public void OnGetMapStoryBoardsComplete(object sender, List<Storyboard>)
    {
        // Do whatever needs to be done when the event is raised.
    }
}

When you connect from the client to the service you need to pass it an InstanceContext built with a reference to the object that will handle the events:

EventHandler eventHandler = new EventHandler();
MyServiceClient client = new MyServiceClient(new InstanceContext(eventHandler));

Does that make sense?

like image 121
Malcolm Avatar answered Jul 21 '26 20:07

Malcolm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!