本文整理汇总了C#中MessageBus.Listen方法的典型用法代码示例。如果您正苦于以下问题:C# MessageBus.Listen方法的具体用法?C# MessageBus.Listen怎么用?C# MessageBus.Listen使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MessageBus
的用法示例。
在下文中一共展示了MessageBus.Listen方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MultiplePublishers
public void MultiplePublishers()
{
const string message1 = "Test Message #1";
const string message2 = "Test Message #2";
const string message3 = "Test Message #3";
const string message4 = "Test Message #4";
string receivedMessage = null;
var messageBus = new MessageBus();
var publisher1 = new Subject<string>();
var publisher2 = new Subject<string>();
var subscription1 = messageBus.RegisterPublisher(publisher1);
messageBus.RegisterPublisher(publisher2);
messageBus.Listen<string>().Subscribe(m => receivedMessage = m, e => Assert.Fail(), Assert.Fail);
Assert.IsNull(receivedMessage);
messageBus.Publish(message1);
Assert.AreEqual(message1, receivedMessage);
publisher1.OnNext(message2);
Assert.AreEqual(message2, receivedMessage);
publisher2.OnNext(message3);
Assert.AreEqual(message3, receivedMessage);
subscription1.Dispose();
publisher1.OnNext(message4);
Assert.AreEqual(message3, receivedMessage);
publisher2.OnNext(message4);
Assert.AreEqual(message4, receivedMessage);
}
示例2: Main
static void Main(string[] args)
{
Console.Title = "zmq Pub Sub Sample Client";
const string publishAddress = "tcp://127.0.0.1:54321";
const string listenAddress = "tcp://127.0.0.1:12345";
IMessageCodec messageCodec = new JsonMessageCodec(Encoding.Unicode);
IMessageBus messageBus = new MessageBus(listenAddress, publishAddress, messageCodec);
messageBus.Subscribe<object>(Console.WriteLine);
messageBus.GetMessages<StartedListeningMessage>()
.Where(m => m.Id == messageBus.GetHashCode())
.Subscribe(x => Console.WriteLine("Press ESC to cancel, or any other key to send a message."));
var worker = new BackgroundWorker();
worker.DoWork += (s, e) => messageBus.Listen();
worker.RunWorkerAsync();
bool loop = true;
int i = 1;
while (loop)
{
var key = Console.ReadKey(false);
if (key.Key == ConsoleKey.Escape)
{
loop = false;
continue;
}
string message = string.Format("Message {0:d2} from Client", i);
messageBus.Publish(message);
i++;
}
}
示例3: ExplicitSendMessageShouldWorkEvenAfterRegisteringSource
public void ExplicitSendMessageShouldWorkEvenAfterRegisteringSource()
{
var fixture = new MessageBus();
fixture.RegisterMessageSource(Observable.Never<int>());
bool messageReceived = false;
fixture.Listen<int>().Subscribe(_ => messageReceived = true);
fixture.SendMessage(42);
Assert.True(messageReceived);
}
示例4: ListeningBeforeRegisteringASourceShouldWork
public void ListeningBeforeRegisteringASourceShouldWork()
{
var fixture = new MessageBus();
int result = -1;
fixture.Listen<int>().Subscribe(x => result = x);
Assert.Equal(-1, result);
fixture.SendMessage(42);
Assert.Equal(42, result);
}
示例5: GCShouldNotKillMessageService
public void GCShouldNotKillMessageService()
{
var bus = new MessageBus();
bool recieved_message = false;
var dispose = bus.Listen<int>().Subscribe(x => recieved_message = true);
bus.SendMessage(1);
Assert.True(recieved_message);
GC.Collect();
GC.WaitForPendingFinalizers();
recieved_message = false;
bus.SendMessage(2);
Assert.True(recieved_message);
}
示例6: ListenToMessageWithoutToken
public void ListenToMessageWithoutToken()
{
const string message1 = "Test Message #1";
const string message2 = "Test Message #2";
string receivedMessage = null;
var messageBus = new MessageBus();
messageBus.Listen<string>().Subscribe(m => receivedMessage = m, e => Assert.Fail(), Assert.Fail);
Assert.IsNull(receivedMessage);
messageBus.Publish(message1);
Assert.AreEqual(message1, receivedMessage);
messageBus.Publish(message2);
Assert.AreEqual(message2, receivedMessage);
}
示例7: MessageBusSmokeTest
public void MessageBusSmokeTest()
{
var input = new[] {1, 2, 3, 4};
var result = (new TestScheduler()).With(sched => {
var source = new Subject<int>();
var fixture = new MessageBus();
fixture.RegisterMessageSource(source, "Test");
Assert.False(fixture.IsRegistered(typeof (int)));
Assert.False(fixture.IsRegistered(typeof (int), "Foo"));
var output = fixture.Listen<int>("Test").CreateCollection();
input.Run(source.OnNext);
sched.Start();
return output;
});
input.AssertAreEqual(result);
}
示例8: RegisteringSecondMessageSourceShouldMergeBothSources
public void RegisteringSecondMessageSourceShouldMergeBothSources()
{
var bus = new MessageBus();
var source1 = new Subject<int>();
var source2 = new Subject<int>();
var recieved_message1 = false;
var recieved_message2 = false;
bus.RegisterMessageSource(source1);
bus.Listen<int>().Subscribe(x => recieved_message1 = true);
bus.RegisterMessageSource(source2);
bus.Listen<int>().Subscribe(x => recieved_message2 = true);
source1.OnNext(1);
Assert.True(recieved_message1);
Assert.True(recieved_message2);
recieved_message1 = false;
recieved_message2 = false;
source2.OnNext(2);
Assert.True(recieved_message1);
Assert.True(recieved_message2);
}
示例9: UnsubscribeListener
public void UnsubscribeListener()
{
const string message1 = "Test Message #1";
const string message2 = "Test Message #2";
string receivedMessage = null;
var messageBus = new MessageBus();
var subscription = messageBus.Listen<string>().Subscribe(m => receivedMessage = m, e => Assert.Fail(), Assert.Fail);
Assert.IsNull(receivedMessage);
messageBus.Publish(message1);
Assert.AreEqual(message1, receivedMessage);
subscription.Dispose();
messageBus.Publish(message2);
Assert.AreEqual(message1, receivedMessage);
}
示例10: SuscribersShouldBeWeak
public void SuscribersShouldBeWeak()
{
const string message1 = "Test Message #1";
const string message2 = "Test Message #2";
string token = Guid.NewGuid().ToString();
string receivedMessage = null;
var messageBus = new MessageBus();
messageBus.Listen<string>(token).Subscribe(m => receivedMessage = m, e => Assert.Fail(), Assert.Fail);
Assert.IsNull(receivedMessage);
messageBus.Publish(message1, token);
Assert.AreEqual(message1, receivedMessage);
GC.Collect();
messageBus.Publish(message2);
Assert.AreEqual(message1, receivedMessage);
}
示例11: SubscribeToMessageBus
public void SubscribeToMessageBus()
{
var messageBus = new MessageBus();
messageBus.Listen<string>().Subscribe(_ => Assert.Fail(), e => Assert.Fail(), Assert.Fail);
}
示例12: ShouldNotReceiveMessageWithToken
public void ShouldNotReceiveMessageWithToken()
{
const string message1 = "Test Message #1";
string token = Guid.NewGuid().ToString();
string receivedMessage = null;
var messageBus = new MessageBus();
messageBus.Listen<string>().Subscribe(m => receivedMessage = m, e => Assert.Fail(), Assert.Fail);
Assert.IsNull(receivedMessage);
messageBus.Publish(message1, token);
Assert.IsNull(receivedMessage);
}
示例13: RegisterPublisher
public void RegisterPublisher()
{
const string message1 = "Test Message #1";
const string message2 = "Test Message #2";
const string message3 = "Test Message #3";
string receivedMessage = null;
var publisher = new Subject<string>();
var messageBus = new MessageBus();
messageBus.RegisterPublisher(publisher);
publisher.OnNext(message1);
messageBus.Listen<string>().Subscribe(m => receivedMessage = m, e => Assert.Fail(), Assert.Fail);
Assert.IsNull(receivedMessage);
publisher.OnNext(message2);
Assert.AreEqual(message2, receivedMessage);
publisher.OnNext(message3);
Assert.AreEqual(message3, receivedMessage);
}
示例14: MessageBusThreadingTest
public void MessageBusThreadingTest()
{
var mb = new MessageBus();
int? listenedThread = null;
int? otherThread = null;
int thisThread = Thread.CurrentThread.ManagedThreadId;
Task.Run(() => {
otherThread = Thread.CurrentThread.ManagedThreadId;
mb.Listen<int>().Subscribe(_ => listenedThread = Thread.CurrentThread.ManagedThreadId);
mb.SendMessage<int>(42);
}).Wait();
Assert.NotEqual(listenedThread.Value, thisThread);
Assert.Equal(listenedThread.Value, otherThread.Value);
}