本文整理汇总了C#中MessageBus.Subscribe方法的典型用法代码示例。如果您正苦于以下问题:C# MessageBus.Subscribe方法的具体用法?C# MessageBus.Subscribe怎么用?C# MessageBus.Subscribe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MessageBus
的用法示例。
在下文中一共展示了MessageBus.Subscribe方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PublishAndUnsubscribe
public void PublishAndUnsubscribe()
{
// Arrange
var bus = new MessageBus();
Action<Object> handler = null;
handler = (m) =>
{
Messages.Add(m);
// Unsubscribe during the publish handler.
bus.Unsubscribe<Object>(handler);
};
bus.Subscribe<Object>(handler);
bus.Subscribe<Object>((m) =>
{
Messages.Add(m);
});
// Act
var message = Guid.NewGuid().ToString();
bus.Publish<Object>(message);
bus.Publish<Object>(message);
// Assert
Assert.AreEqual(3, Messages.Count);
}
示例2: ConfigureSubscriptions
public static void ConfigureSubscriptions(IContext context, MessageBus messageBus)
{
var notificationByDeviceIdWaiter = context.Kernel.Get<ObjectWaiter>("DeviceNotification.DeviceID");
var commandByDeviceIdWaiter = context.Kernel.Get<ObjectWaiter>("DeviceCommand.DeviceID");
var commandByCommandIdWaiter = context.Kernel.Get<ObjectWaiter>("DeviceCommand.CommandID");
messageBus.Subscribe<DeviceNotificationAddedMessage>(message => notificationByDeviceIdWaiter.NotifyChanges(message.DeviceId, message.Name));
messageBus.Subscribe<DeviceCommandAddedMessage>(message => commandByDeviceIdWaiter.NotifyChanges(message.DeviceId, message.Name));
messageBus.Subscribe<DeviceCommandUpdatedMessage>(message => commandByCommandIdWaiter.NotifyChanges(message.CommandId, null));
}
示例3: SubscribeExisting
public void SubscribeExisting()
{
var bus = new MessageBus();
bus.Subscribe<Object>(Handler);
bus.Subscribe<Object>(Handler);
var message = new Object();
bus.Publish<Object>(message);
Assert.AreEqual(2, _Messages.Count);
Assert.AreSame(message, _Messages[0]);
Assert.AreSame(message, _Messages[1]);
}
示例4: WidgetSyncManager
public WidgetSyncManager ()
{
authManager = ServiceContainer.Resolve<AuthManager>();
authManager.PropertyChanged += OnAuthPropertyChanged;
activeTimeEntryManager = ServiceContainer.Resolve<ActiveTimeEntryManager> ();
widgetUpdateService = ServiceContainer.Resolve<IWidgetUpdateService> ();
messageBus = ServiceContainer.Resolve<MessageBus> ();
subscriptionSyncStarted = messageBus.Subscribe<SyncStartedMessage> (OnSyncWidget);
subscriptionSyncFinished = messageBus.Subscribe<SyncFinishedMessage> (OnSyncWidget);
subscriptionStartStopFinished = messageBus.Subscribe<StartStopMessage> (OnSyncWidget);
}
示例5: WidgetSyncManager
public WidgetSyncManager ()
{
authManager = ServiceContainer.Resolve<AuthManager>();
authManager.PropertyChanged += OnAuthPropertyChanged;
widgetUpdateService = ServiceContainer.Resolve<IWidgetUpdateService>();
timeEntryManager = ServiceContainer.Resolve<ActiveTimeEntryManager> ();
timeEntryManager.PropertyChanged += OnTimeEntryManagerPropertyChanged;
ResetModelToRunning ();
messageBus = ServiceContainer.Resolve<MessageBus> ();
subscriptionSyncStarted = messageBus.Subscribe<SyncStartedMessage> (OnSync);
subscriptionSyncFinished = messageBus.Subscribe<SyncFinishedMessage> (OnSync);
}
示例6: ConfigureMessageBus
public static void ConfigureMessageBus(IContext context, MessageBus messageBus)
{
var clientController = context.Kernel.Get<ClientController>();
var deviceController = context.Kernel.Get<DeviceController>();
messageBus.Subscribe((DeviceNotificationAddedMessage msg) =>
clientController.HandleDeviceNotification(msg.DeviceId, msg.NotificationId));
messageBus.Subscribe((DeviceCommandAddedMessage msg) =>
deviceController.HandleDeviceCommand(msg.DeviceId, msg.CommandId));
messageBus.Subscribe((DeviceCommandUpdatedMessage msg) =>
clientController.HandleCommandUpdate(msg.CommandId));
}
示例7: 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.Subscribe<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);
}
示例8: 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++;
}
}
示例9: ReflectionPluginManager
public MessageBus Bus { get; }//TODO CREATE BASE CLASS FOR MESSAGEBUS COMPONENTS
public ReflectionPluginManager(MessageBus bus)
{
if (bus == null)
{
throw new ArgumentNullException("bus");
}
Bus = bus;
Bus.Subscribe(this);
}
示例10: PublishDifferent
public void PublishDifferent()
{
var bus = new MessageBus();
bus.Subscribe<Object>(Handler);
var message = Guid.NewGuid().ToString();
bus.Publish<String>(message);
Assert.AreEqual(0, _Messages.Count);
}
示例11: 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.Subscribe<TestMessage>(MessageHandlerType.Synchronous, message => receivedMessage1 = message,
false);
bus.Subscribe<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);
}
示例12: Unsubscribe
public void Unsubscribe()
{
var bus = new MessageBus();
bus.Subscribe<Object>(Handler);
bus.Unsubscribe<Object>(Handler);
var message = new Object();
bus.Publish<Object>(message);
Assert.AreEqual(0, _Messages.Count);
}
示例13: PassSubscriptionWithNonGenericParameter
public async void PassSubscriptionWithNonGenericParameter()
{
var messageBus = new MessageBus();
var expected = new Event();
Event actual = null;
messageBus.Subscribe<Event>((e) => actual = e);
await messageBus.Publish(expected);
Assert.Equal(expected, actual);
}
示例14: Publish
public void Publish()
{
// Arrange
var bus = new MessageBus();
bus.Subscribe<Object>(Handler);
// Act
var message = Guid.NewGuid().ToString();
bus.Publish<Object>(message);
// Assert
Assert.AreEqual(1, Messages.Count);
}
示例15: Run
public static IDisposable Run(int connections, int senders, string payload, int messageBufferSize = 10)
{
var resolver = new DefaultDependencyResolver();
var bus = new MessageBus(resolver);
var countDown = new CountdownEvent(senders);
var subscriptions = new List<IDisposable>();
var cancellationTokenSource = new CancellationTokenSource();
// Initialize performance counters for this run
Utility.InitializePerformanceCounters(resolver, cancellationTokenSource.Token);
for (int i = 0; i < connections; i++)
{
string identity = i.ToString();
var subscriber = new Subscriber(identity, new[] { "a", "b", "c" });
IDisposable subscription = bus.Subscribe(subscriber,
cursor: null,
callback: _ => TaskAsyncHelper.True,
maxMessages: messageBufferSize);
subscriptions.Add(subscription);
}
for (var i = 0; i < senders; i++)
{
ThreadPool.QueueUserWorkItem(_ =>
{
while (!cancellationTokenSource.IsCancellationRequested)
{
string source = i.ToString();
bus.Publish(source, "a", payload);
}
countDown.Signal();
});
}
return new DisposableAction(() =>
{
cancellationTokenSource.Cancel();
// Wait for all senders to stop
countDown.Wait(TimeSpan.FromMilliseconds(1000 * senders));
// Shut the bus down and wait for workers to stop
bus.Dispose();
// Dispose of all the subscriptions
subscriptions.ForEach(s => s.Dispose());
});
}