本文整理汇总了C#中MessageBus.Send方法的典型用法代码示例。如果您正苦于以下问题:C# MessageBus.Send方法的具体用法?C# MessageBus.Send怎么用?C# MessageBus.Send使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MessageBus
的用法示例。
在下文中一共展示了MessageBus.Send方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
示例2: Main
private static void Main()
{
var bus = new MessageBus("ServiceA");
bus.Start();
bus.Subscribe<TradeOpportunityFound>("Publisher");
Console.ReadLine();
for (int i = 0; i < 100; i++)
{
bus.Send(new SimpleMessage { Data = "Hello World" }, "ServiceB");
}
Console.ReadLine();
}
示例3: 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);
}
示例4: GetEarlyEventBus
private static MessageBus GetEarlyEventBus(Entities dataContext, IWorkspace workspace, MessageBus persistedEventBus)
{
var messageBus = new MessageBus();
messageBus
.AsObservable<IDomainEvent>()
.Do(e =>
workspace.Committing += (s, ea) =>
{
ObjectStateEntry entry;
if (dataContext.ObjectStateManager.TryGetObjectStateEntry(e.Sender, out entry))
{
//resend the same event after commit
persistedEventBus.Send(e);
}
})
.Subscribe();
return messageBus;
}
示例5: 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);
}
示例6: ShouldPublishMessageToSubscriberAsynchronously
public async Task ShouldPublishMessageToSubscriberAsynchronously()
{
var wait = new TaskCompletionSource<object>();
var done = new TaskCompletionSource<object>();
var bus = new MessageBus();
var message = new object();
bool recieved = false;
var current = Task.CurrentId;
bus.Observe<object>()
.Where(o => Object.ReferenceEquals(message, o))
.Subscribe(async o =>
{
await wait.Task;
recieved = true;
done.TrySetResult(null);
});
Assert.False(recieved);
bus.Send(message);
Assert.False(recieved);
wait.TrySetResult(null);
await done.Task;
Assert.True(recieved);
}
示例7: Can_process_messages_via_RX
public void Can_process_messages_via_RX()
{
var trigger = new AutoResetEvent(false);
IMessageBus bus = new MessageBus();
string receivedMessage=null;
var query = bus.MessageSubject
.Delay(new TimeSpan(0, 0, 1))
.Select(m => m as TestMessage)
.Where(m => m != null)
.Select(m => m.Text)
.Where(s => s == "Hello bus");
query.Subscribe(m =>
{
receivedMessage = m;
trigger.Set();
});
var sentMessage1 = new TestMessage
{
Text = "First message"
};
var sentMessage2 = new TestMessage
{
Text = "Hello bus"
};
bus.Send(sentMessage1);
bus.Send(sentMessage2);
trigger.WaitOne(3000);
Assert.AreEqual(sentMessage2.Text,receivedMessage);
}
示例8: ShouldRequireNonNullMessage
public void ShouldRequireNonNullMessage()
{
var bus = new MessageBus();
var ex = Assert.Throws<ArgumentNullException>(() => bus.Send<object>(null));
Assert.Equal("message", ex.ParamName);
}
示例9: TestRegisterSimpleTypes
public void TestRegisterSimpleTypes()
{
const string testContentString = "abcd";
var testContentDateTime = DateTime.Now;
const int testContentInt = 42;
Reset();
IMessageBus messageBus = new MessageBus();
messageBus.Register<string>(this, TestConstants.MyStringToken, m => ReceivedContentStringA1 = m);
messageBus.Register<DateTime>(this, TestConstants.MyDateTimeToken, m => ReceivedContentDateTime1 = m);
messageBus.Register<int>(this, TestConstants.MyIntToken, m => ReceivedContentInt = m);
Assert.AreEqual(null, ReceivedContentStringA1);
Assert.AreEqual(DateTime.MinValue, ReceivedContentDateTime1);
Assert.AreEqual(default(int), ReceivedContentInt);
messageBus.Send(testContentString, TestConstants.MyStringToken);
Assert.AreEqual(testContentString, ReceivedContentStringA1);
Assert.AreEqual(DateTime.MinValue, ReceivedContentDateTime1);
Assert.AreEqual(default(int), ReceivedContentInt);
messageBus.Send(testContentDateTime, TestConstants.MyDateTimeToken);
Assert.AreEqual(testContentString, ReceivedContentStringA1);
Assert.AreEqual(testContentDateTime, ReceivedContentDateTime1);
Assert.AreEqual(default(int), ReceivedContentInt);
messageBus.Send(testContentInt, TestConstants.MyIntToken);
Assert.AreEqual(testContentString, ReceivedContentStringA1);
Assert.AreEqual(testContentDateTime, ReceivedContentDateTime1);
Assert.AreEqual(testContentInt, ReceivedContentInt);
}
示例10: InnerListHasOneItemAfterUnregisterIsCalledForSpecificActionWhereTwoActionsRegisteredForOneToken
public void InnerListHasOneItemAfterUnregisterIsCalledForSpecificActionWhereTwoActionsRegisteredForOneToken()
{
const string testContent1 = "efgh";
const string testContent2 = "mnop";
const int token = 4567;
Reset();
IMessageBus messageBus = new MessageBus();
Action<string> action1 = m => ReceivedContentStringA = m;
Action<string> action2 = m => ReceivedContentStringB = m;
messageBus.Register(this, token, action1);
messageBus.Register(this, token, action2);
messageBus.Send(testContent1, token);
Assert.AreEqual(testContent1, ReceivedContentStringA);
Assert.AreEqual(testContent1, ReceivedContentStringB);
messageBus.Unregister(this, token, action2); // only unregister action2. Leave action1.
var type = typeof(MessageBus);
// _commandCollection is an instance, private member
BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
// Retrieve a FieldInfo instance corresponding to the field
FieldInfo field = type.GetField("_recipientsStrictAction", flags);
var innerRecipientsDictionary = field.GetValue(messageBus) as Dictionary<Type, List<MessageBus.WeakActionAndToken>>;
messageBus.Send(testContent2, token);
Assert.AreEqual(testContent2, ReceivedContentStringA);
Assert.AreEqual(testContent1, ReceivedContentStringB);
Assert.IsTrue(innerRecipientsDictionary.Count == 1);
}
示例11: TestUnregisterOneAction
public void TestUnregisterOneAction()
{
const string testContentA1 = "abcd";
const string testContentA2 = "efgh";
Reset();
IMessageBus messageBus = new MessageBus();
Action<TestMessageA> actionA1 = m => ReceivedContentStringA1 = m.Content;
messageBus.Register(this, TestConstants.MyContentToken, actionA1);
Assert.AreEqual(null, ReceivedContentStringA1);
messageBus.Send(new TestMessageA
{
Content = testContentA1
}, TestConstants.MyContentToken);
Assert.AreEqual(testContentA1, ReceivedContentStringA1);
messageBus.Unregister(this, TestConstants.MyContentToken, actionA1);
messageBus.Send(new TestMessageA
{
Content = testContentA2
}, TestConstants.MyContentToken);
Assert.AreNotEqual(testContentA2, ReceivedContentStringA1);
}
示例12: TestUnRegisterObjectOnlyOverloadWithOneHandler
public void TestUnRegisterObjectOnlyOverloadWithOneHandler()
{
const string testContentString = "abcd";
const string testContentStringAfterUnregister = "efgh";
Reset();
IMessageBus messageBus = new MessageBus();
messageBus.Register<string>(this, TestConstants.MyStringToken, m => ReceivedContentStringA1 = m);
Assert.AreEqual(null, ReceivedContentStringA1);
messageBus.Send(testContentString, TestConstants.MyStringToken);
Assert.AreEqual(testContentString, ReceivedContentStringA1);
messageBus.Unregister<string>(this); // Use overload which just uses object
messageBus.Send(testContentStringAfterUnregister, TestConstants.MyStringToken);
Assert.AreNotEqual(testContentStringAfterUnregister, ReceivedContentStringA1);
}
示例13: TestUnRegisterObjectOnlyOverloadWithMoreThanOneHandler
public void TestUnRegisterObjectOnlyOverloadWithMoreThanOneHandler()
{
const string testContentString1 = "abcd1";
const string testContentString2 = "abcd2";
const string testContentStringAfterUnregister = "efgh";
Action<string> action1 = m => ReceivedContentStringA = m;
Action<string> action2 = m => ReceivedContentStringA1 = m;
Action<string> action3 = m => ReceivedContentStringB = m;
Reset();
IMessageBus messageBus = new MessageBus();
messageBus.Register<string>(this, TestConstants.MyStringToken, action1);
messageBus.Register<string>(this, TestConstants.MyStringToken, action2);
messageBus.Register<string>(this, TestConstants.MyContentToken, action3);
Assert.AreEqual(null, ReceivedContentStringA);
Assert.AreEqual(null, ReceivedContentStringA1);
Assert.AreEqual(null, ReceivedContentStringB);
messageBus.Send(testContentString1, TestConstants.MyStringToken);
messageBus.Send(testContentString2, TestConstants.MyContentToken);
Assert.AreEqual(testContentString1, ReceivedContentStringA);
Assert.AreEqual(testContentString1, ReceivedContentStringA1);
Assert.AreEqual(testContentString2, ReceivedContentStringB);
messageBus.Unregister<string>(this); // Use overload which just uses object
messageBus.Send(testContentStringAfterUnregister, TestConstants.MyStringToken);
messageBus.Send(testContentStringAfterUnregister, TestConstants.MyContentToken);
Assert.AreNotEqual(testContentStringAfterUnregister, ReceivedContentStringA1);
Assert.AreNotEqual(testContentStringAfterUnregister, ReceivedContentStringB);
}
示例14: TestRegisterUnregisterWithToken
public void TestRegisterUnregisterWithToken()
{
const string testContent1 = "abcd";
const string testContent2 = "efgh";
const string testContent3 = "ijkl";
const int token1 = 1234;
const int token2 = 4567;
Reset();
IMessageBus messageBus = new MessageBus();
messageBus.Register<string>(this, token1, m => ReceivedContentStringA1 = m);
messageBus.Register<string>(this, token2, m => ReceivedContentStringA = m);
Assert.AreEqual(null, ReceivedContentStringA1);
Assert.AreEqual(null, ReceivedContentStringA);
messageBus.Send(testContent1, token1);
Assert.AreEqual(testContent1, ReceivedContentStringA1);
Assert.AreEqual(null, ReceivedContentStringA);
messageBus.Send(testContent2, token2);
Assert.AreEqual(testContent1, ReceivedContentStringA1);
Assert.AreEqual(testContent2, ReceivedContentStringA);
messageBus.Unregister<string>(this, token1);
messageBus.Send(testContent3, token1);
Assert.AreEqual(testContent1, ReceivedContentStringA1);
Assert.AreEqual(testContent2, ReceivedContentStringA);
}
示例15: TestRegisterUnregisterOneActionWithToken
public void TestRegisterUnregisterOneActionWithToken()
{
const string testContent1 = "abcd";
const string testContent2 = "efgh";
const string testContent3 = "ijkl";
const string testContent4 = "mnop";
const int token1 = 1234;
const int token2 = 4567;
Reset();
IMessageBus messageBus = new MessageBus();
Action<string> action1 = m => ReceivedContentStringA1 = m;
Action<string> action2 = m => ReceivedContentStringA = m;
Action<string> action3 = m => ReceivedContentStringB = m;
messageBus.Register(this, token1, action1);
messageBus.Register(this, token2, action2);
messageBus.Register(this, token2, action3);
messageBus.Send(testContent1, token1);
messageBus.Send(testContent2, token2);
Assert.AreEqual(testContent1, ReceivedContentStringA1);
Assert.AreEqual(testContent2, ReceivedContentStringA);
Assert.AreEqual(testContent2, ReceivedContentStringB);
messageBus.Unregister(this, token2, action3);
messageBus.Send(testContent3, token1);
messageBus.Send(testContent4, token2);
Assert.AreEqual(testContent3, ReceivedContentStringA1);
Assert.AreEqual(testContent4, ReceivedContentStringA);
Assert.AreEqual(testContent2, ReceivedContentStringB);
}