Skip to content

Instantly share code, notes, and snippets.

@zleao
Last active January 10, 2021 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zleao/cce569fe9e0f162f399e7a2e03581973 to your computer and use it in GitHub Desktop.
Save zleao/cce569fe9e0f162f399e7a2e03581973 to your computer and use it in GitHub Desktop.
Examples of subscriptions using zoft.NotificationService
// OneWay Subscription Examples
// Subscribe OneWay message with default context, using a weak reference
var token = NotificationService.Subscribe<MyMessage>(HandleOneWaySubscriptionAsync);
var token = NotificationService.Subscribe(typeof(MyMessage), HandleOneWaySubscriptionAsync);
// Subscribe OneWay message with specified context, using a weak reference
var token = NotificationService.Subscribe<MyMessage>(HandleOneWaySubscriptionAsync, "myContext");
var token = NotificationService.Subscribe(typeof(MyMessage), HandleOneWaySubscriptionAsync, "myContext");
// Subscribe OneWay message with specified context with the specified reference type (weak or strong)
var token = NotificationService.Subscribe<MyMessage>(HandleOneWaySubscriptionAsync, "myContext", SubscriptionReferenceTypeEnum.Weak);
var token = NotificationService.Subscribe(typeof(MyMessage), HandleOneWaySubscriptionAsync, "myContext", SubscriptionReferenceTypeEnum.Weak);
private Task HandleOneWaySubscriptionAsync(MyMessage msg)
{
//Some handling logic
}
// TwoWay Subscription Examples
// Subscribe TwoWay message with default context, using a weak reference
var token = NotificationService.Subscribe<MyMessage, MyResult>(HandleTwoWaySubscriptionAsync);
var token = NotificationService.Subscribe(typeof(MyMessage), typeof(MyResult), HandleTwoWaySubscriptionAsync);
// Subscribe TwoWay message with specified context, using a weak reference
var token = NotificationService.Subscribe<MyMessage, MyResult>(HandleTwoWaySubscriptionAsync, "myContext");
var token = NotificationService.Subscribe(typeof(MyMessage), typeof(MyResult), HandleTwoWaySubscriptionAsync, "myContext");
// Subscribe TwoWay message with specified context with the specified reference type (weak or strong)
var token = NotificationService.Subscribe<MyMessage, MyResult>(HandleTwoWaySubscriptionAsync, "myContext", SubscriptionReferenceTypeEnum.Weak);
var token = NotificationService.Subscribe(typeof(MyMessage), typeof(MyResult), HandleTwoWaySubscriptionAsync, "myContext", SubscriptionReferenceTypeEnum.Weak);
private Task<MyResult> HandleTwoWaySubscriptionAsync(MyMessage msg)
{
//Some handling logic
return new MyResult();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment