本文整理汇总了C#中MessageBus.RegisterHandler方法的典型用法代码示例。如果您正苦于以下问题:C# MessageBus.RegisterHandler方法的具体用法?C# MessageBus.RegisterHandler怎么用?C# MessageBus.RegisterHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MessageBus
的用法示例。
在下文中一共展示了MessageBus.RegisterHandler方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetPersistedEventBus
private static MessageBus GetPersistedEventBus()
{
var persistedEventBus = new MessageBus();
//application handlers
persistedEventBus.RegisterHandler<RepliedToPostEvent>(MessageHandlerType.Synchronous, OnRepliedToPost, true);
persistedEventBus.RegisterHandler<ApprovedCommentEvent>(MessageHandlerType.Synchronous, OnApprovedComment, true);
persistedEventBus.RegisterHandler<FailedMessage>(MessageHandlerType.Synchronous, OnFailMessage, false);
//sync handlers
//persistedEventBus.RegisterHandler<RepliedToPostEvent>(MessageHandlerType.Synchronous, e => UpdateComment( e.Comment), true);
//persistedEventBus.RegisterHandler<ApprovedCommentEvent>(MessageHandlerType.Synchronous, e => UpdateComment( e.Comment), true);
//persistedEventBus.RegisterHandler<AssignedCategoryToPostEvent>(MessageHandlerType.Synchronous, e => CreateCategoryLink(e.Post,e.Category), true);
//persistedEventBus.RegisterHandler<EditedPostEvent>(MessageHandlerType.Synchronous, e => UpdatePost(e.Post), true);
//persistedEventBus.RegisterHandler<EnabledCommentsEvent>(MessageHandlerType.Synchronous, e => UpdatePost(e.Post), true);
//persistedEventBus.RegisterHandler<DisabledCommentsEvent>(MessageHandlerType.Synchronous, e => UpdatePost(e.Post), true);
//persistedEventBus.RegisterHandler<PostCreatedEvent>(MessageHandlerType.Synchronous, e => UpdatePost(e.Post), true);
//persistedEventBus.RegisterHandler<PublishedPostEvent>(MessageHandlerType.Synchronous, e => DeletePost(e.Post), true);
//persistedEventBus.RegisterHandler<UnpublishedPostEvent>(MessageHandlerType.Synchronous, e => DeletePost(e.Post), true);
//persistedEventBus.RegisterHandler<PostDeletedEvent>(MessageHandlerType.Synchronous, e => DeletePost(e.Post), true);
return persistedEventBus;
}
示例2: Main
public static void Main(string[] args)
{
var mb = new MessageBus();
mb.RegisterHandler<Int32>("my.address", message =>
{
});
mb.RegisterHandler<Int32>("my.address", (message, context) =>
{
context.Reply();
});
}
示例3: Can_send_and_receive_async_message
public void Can_send_and_receive_async_message()
{
var trigger = new AutoResetEvent(false);
IMessageBus bus = new MessageBus();
TestMessage receivedMessage = null;
int currentThreadId = Thread.CurrentThread.ManagedThreadId;
int handlerThreadId = 0;
bus.RegisterHandler<TestMessage>(MessageHandlerType.Asynchronous, message =>
{
handlerThreadId = Thread.CurrentThread.ManagedThreadId;
receivedMessage = message;
trigger.Set();
}, false);
var sentMessage = new TestMessage
{
Text = "Hello bus"
};
bus.Send(sentMessage);
trigger.WaitOne(1000);
//ensure we got the message
Assert.AreSame(sentMessage, receivedMessage);
//ensure we didn't handle it in the main thread
Assert.AreNotEqual(currentThreadId, handlerThreadId);
}
示例4: Can_register_multiple_handlers_for_message_type
public void Can_register_multiple_handlers_for_message_type()
{
IMessageBus bus = new MessageBus();
TestMessage receivedMessage1 = null;
TestMessage receivedMessage2 = null;
bus.RegisterHandler<TestMessage>(MessageHandlerType.Synchronous, message => receivedMessage1 = message, false);
bus.RegisterHandler<TestMessage>(MessageHandlerType.Synchronous, message => receivedMessage2 = message, false);
var sentMessage = new TestMessage
{
Text = "Hello bus"
};
bus.Send(sentMessage);
Assert.AreSame(sentMessage, receivedMessage1);
Assert.AreSame(sentMessage, receivedMessage2);
}
示例5: Test
public void Test()
{
var newMessages = new List<object>();
var bus = new MessageBus();
bus.RegisterHandler<object>(newMessages.Add);
var eventStore = new InMemoryEventStore(bus, GivenTheseEvents());
var repository = new DomainRepository(eventStore);
RegisterHandler(bus, repository);
bus.Handle(WhenThisHappens());
var expected = TheseEventsShouldOccur();
Assert.AreEqual(expected.Count(), newMessages.Count);
}
示例6: Can_send_and_receive_sync_message
public void Can_send_and_receive_sync_message()
{
IMessageBus bus = new MessageBus();
TestMessage receivedMessage = null;
bus.RegisterHandler<TestMessage>(MessageHandlerType.Synchronous, message => receivedMessage = message, false);
var sentMessage = new TestMessage
{
Text = "Hello bus"
};
bus.Send(sentMessage);
Assert.AreSame(sentMessage, receivedMessage);
}
示例7: Test
public void Test()
{
var newMessages = new List<object>();
var bus = new MessageBus();
bus.RegisterHandler<object>(newMessages.Add);
var eventStore = new InMemoryEventStore(bus, GivenTheseEvents());
var repository = new DomainRepository(eventStore);
RegisterHandler(bus, repository);
try
{
bus.Handle(WhenThisHappens());
}
catch(Exception e)
{
var expectedException = ThisExceptionShouldOccur();
if(expectedException == null)
Assert.Fail(e.Message);
if(expectedException.GetType().IsAssignableFrom(e.GetType()))
{
Assert.AreEqual(expectedException.Message, e.Message);
return;
}
Assert.Fail("Expected exception of type {0} with message {1} but got exception of type {2} with message {3}", expectedException.GetType(), expectedException.Message, e.GetType(), e.Message);
}
var expectedEvents = TheseEventsShouldOccur().ToList();
var comparer = new CompareObjects();
if(!comparer.Compare(expectedEvents, newMessages))
{
Assert.Fail(comparer.DifferencesString);
}
}
示例8: RegisterHandler
public override void RegisterHandler(MessageBus bus, IRepository repo)
{
var svc = new AccountApplicationService(repo);
bus.RegisterHandler<DebitAccountCommand>(svc.Handle);
}
示例9: Run
int Run()
{
var testTypes = typeof(AccountShouldBeLockedAfterThreeOverdraws).Assembly.GetTypes().Where(x => typeof(TestBase).IsAssignableFrom(x) && x.IsAbstract == false);
foreach (var type in testTypes)
{
var test = Activator.CreateInstance(type) as TestBase;
var newMessages = new List<object>();
var bus = new MessageBus();
bus.RegisterHandler<object>(newMessages.Add);
var eventStore = new InMemoryEventStore(bus, test.GivenTheseEvents());
var repository = new DomainRepository(eventStore);
test.RegisterHandler(bus, repository);
printGivens(type, test);
var command = test.WhenThisHappens();
Exception exception = null;
var expectedException = test.ThisExceptionShouldOccur();
try
{
bus.Handle(command);
}
catch(Exception e)
{
exception = e;
}
var foundException = printException(expectedException, exception, command);
if(foundException)
continue; ;
printThens(test.TheseEventsShouldOccur().ToList(), newMessages, command);
Console.WriteLine();
Console.WriteLine(new string('=', 40));
Console.WriteLine();
}
return _suuccess ? 0 : -1;
}
示例10: Can_send_FailedMessage_when_messagehandler_throws
public void Can_send_FailedMessage_when_messagehandler_throws()
{
IMessageBus bus = new MessageBus();
FailedMessage receivedFailedMessage = null;
bus.RegisterHandler<TestMessage>(MessageHandlerType.Synchronous, message => { throw new Exception("This should throw"); }, true);
bus.RegisterHandler<FailedMessage>(MessageHandlerType.Synchronous, message => receivedFailedMessage = message, false);
var sentMessage = new TestMessage
{
Text = "Hello bus"
};
bus.Send(sentMessage);
Assert.IsNotNull(receivedFailedMessage);
}
示例11: Configuration
private Configuration()
{
/* bus intialisation */
_bus = new MessageBus();
//var eventStore = new SqlServerEventStore(_bus);
//var eventStore = new SqlLiteEventStore(_bus);
var eventStore = new InMemoryEventStore(_bus, inMemDict );
var repository = new DomainRepository(eventStore);
/* Account Domain */
var commandService = new AccountApplicationService(repository);
_bus.RegisterHandler<RegisterAccountCommand>(commandService.Handle);
_bus.RegisterHandler<DebitAccountCommand>(commandService.Handle);
_bus.RegisterHandler<UnlockAccountCommand>(commandService.Handle);
var infoProjection = new AccountInfoProjection();
_bus.RegisterHandler<AccountRegisteredEvent>(infoProjection.Handle);
_bus.RegisterHandler<AccountLockedEvent>(infoProjection.Handle);
_bus.RegisterHandler<AccountUnlockedEvent>(infoProjection.Handle);
var balanceProjection = new AccountBalanceProjection();
_bus.RegisterHandler<AccountRegisteredEvent>(balanceProjection.Handle);
_bus.RegisterHandler<AccountDebitedEvent>(balanceProjection.Handle);
var notification = new NotificationProjection();
_bus.RegisterHandler<AccountRegisteredEvent>(notification.Handle);
_bus.RegisterHandler<AccountDebitedEvent>(notification.Handle);
_bus.RegisterHandler<AccountLockedEvent>(notification.Handle);
_bus.RegisterHandler<AccountUnlockedEvent>(notification.Handle);
_bus.RegisterHandler<AccountOverdrawAttemptedEvent>(notification.Handle);
_AccountReadModel = new AccountReadModelFacade(balanceProjection, infoProjection, notification);
/* News Domain*/
//var newsEventStore = new SqlServerEventStore(_bus);
//var newsEventStore = new SqlLiteEventStore(_bus);
var newsEventStore = new InMemoryEventStore(_bus, inMemDict);
var newsRepository = new DomainRepository(eventStore);
/* Register command on the News bounded context */
var newsCommandService = new AccountManager.Models.News.Domain.NewsApplicationService(newsRepository);
_bus.RegisterHandler<AccountManager.Models.News.Commands.CreateNewsCommand>(newsCommandService.Handle);
_bus.RegisterHandler<AccountManager.Models.News.Commands.PublishNewsCommand>(newsCommandService.Handle);
_bus.RegisterHandler<AccountManager.Models.News.Commands.UnpublishNewsCommand>(newsCommandService.Handle);
_bus.RegisterHandler<AccountManager.Models.News.Commands.UpdateNewsCommand>(newsCommandService.Handle);
_bus.RegisterHandler<AccountManager.Models.News.Commands.DeleteNewsCommand>(newsCommandService.Handle);
var _newsProjection = new AccountManager.Models.News.ReadModel.NewsProjection();
_bus.RegisterHandler<AccountManager.Models.News.Events.NewsCreatedEvent>(_newsProjection.Handle);
_bus.RegisterHandler<AccountManager.Models.News.Events.NewsPublishedEvent>(_newsProjection.Handle);
_bus.RegisterHandler<AccountManager.Models.News.Events.NewsUnpublishedEvent>(_newsProjection.Handle);
_bus.RegisterHandler<AccountManager.Models.News.Events.NewsUpdatedEvent>(_newsProjection.Handle);
_bus.RegisterHandler<AccountManager.Models.News.Events.NewsDeletedEvent>(_newsProjection.Handle);
_NewsReadModel = new NewsReadModelFacade(_newsProjection);
/* rehydrate */
var events = eventStore.GetAllEventsEver();
_bus.Publish(events);
}
示例12: RegisterHandler
public override void RegisterHandler(MessageBus bus, IRepository repo)
{
var svc = new NewsApplicationService(repo);
bus.RegisterHandler<UpdateNewsCommand>(svc.Handle);
bus.RegisterHandler<PublishNewsCommand>(svc.Handle);
}
示例13: Configuration
private Configuration()
{
_bus = new MessageBus();
var eventStore = new SqlEventStore(_bus);
var repository = new DomainRepository(eventStore);
var commandService = new AccountApplicationService(repository);
_bus.RegisterHandler<RegisterAccountCommand>(commandService.Handle);
_bus.RegisterHandler<DebitAccountCommand>(commandService.Handle);
_bus.RegisterHandler<UnlockAccountCommand>(commandService.Handle);
var infoProjection = new AccountInfoProjection();
_bus.RegisterHandler<AccountRegisteredEvent>(infoProjection.Handle);
_bus.RegisterHandler<AccountLockedEvent>(infoProjection.Handle);
_bus.RegisterHandler<AccountUnlockedEvent>(infoProjection.Handle);
var balanceProjection = new AccountBalanceProjection();
_bus.RegisterHandler<AccountRegisteredEvent>(balanceProjection.Handle);
_bus.RegisterHandler<AccountDebitedEvent>(balanceProjection.Handle);
var notification = new NotificationProjection();
_bus.RegisterHandler<AccountRegisteredEvent>(notification.Handle);
_bus.RegisterHandler<AccountDebitedEvent>(notification.Handle);
_bus.RegisterHandler<AccountLockedEvent>(notification.Handle);
_bus.RegisterHandler<AccountUnlockedEvent>(notification.Handle);
_bus.RegisterHandler<OverdrawAttemptedEvent>(notification.Handle);
_readModel = new ReadModelFacade(balanceProjection, infoProjection, notification);
var events = eventStore.GetAllEventsEver();
_bus.Publish(events);
}
示例14: RegisterHandler
public override void RegisterHandler(MessageBus bus, IRepository repo)
{
var executor = new AccountCommandExecutors(repo);
bus.RegisterHandler<DebitCommand>(executor.Handle);
}