.. _refSubscribing: Subscribing ----------- There are two ways to say you want to subscribe to an event or synchronizer. The difference between these is that an event is a one-way fire-and-forget message that is intended for multiple recipients, and a synchronized is a two-way message that is intended for one recipient, but can be called from multiple sources. Direct subscription =================== You can subscribe using the methods found in ``ICambion``, which are accessible in your ``CambionMessageHandler`` object. :: cambion.AddEventHandler(MyEventCallback); cambion.AddSynchronizedHandler(MySynchronizedCallback); The method signature for the callbacks will then be as follows: :: private void MyEventCallback(TEvent data) { } private TResponse MySynchronizedCallback(TRequest data) { } You can also use lambda expressions instead of callback methods: :: cambion.AddEventHandler(data => { }); cambion.AddSynchronizedHandler(data => { return null; }); Make sure that your synchronized methods/expressions always return data. Interface subscription ====================== Another way to subscribe is using an interfaces. To use this approach, the class where you want your subscriptions implement has to have either ``IEventHandler<>`` or ``ISynchronizedHandler<>``. You can now register all your subscriptions in a single call to ``.Register()``. Example ^^^^^^^ :: public class MySubscriptionClass : IEventHandler, ISynchronizedHandler { public MySubscriptionClass(ICambion cambion) { cambion.Register(this); } public void HandleEvent(TEvent data) { } public TResponse HandleSynchronized(TRequest data) { return new TResponse(); } } Async Handlers ============== In addition to the subscription methods described above you have ``Async`` versions of the same methods, and similarily with the interfaces you have ``IAsyncEventHandler<>`` or ``IAsyncSynchronizedHandler<>``, which can be used with ``.Register()``. The main difference with these subscriptions are that the callbacks need to return a ``Task`` instead of ``void``. These can also be marked as ``async`` but should only do si if you actually use ``await`` in your handler. Examples ^^^^^^^^ :: public class MyAsyncSubscriptionClass : IAsyncEventHandler, IAsyncSynchronizedHandler { public MyAsyncSubscriptionClass(ICambion cambion) { cambion.Register(this); cambion.AddAsyncEventHandler(MyAsyncEventCallback); cambion.AddAsyncSynchronizedHandler(MyAsyncSynchronizedCallback); cambion.AddAsyncEventHandler(data => { return Task.CompletedTask; }); cambion.AddAsyncSynchronizedHandler(data => { return Task.FromResult((TResponse)null); }); cambion.AddAsyncEventHandler(async data => { await DoSomethingAsync(); }); cambion.AddAsyncSynchronizedHandler(data => { await DoSomethingAsync(); return null; }); } public Task MyAsyncEventCallback(TEvent data) { return Task.CompletedTask; } public async Task MyAsyncSynchronizedCallback(TRequest data) { await DoSomethingAsync(); return null; } public async Task HandleEventAsync(TEvent data) { await DoSomethingElseAsync(); } public Task HandleSyncronizedAsync(TRequest data) { return Task.FromResult((TResponse)null); } }