当前位置: 首页>>代码示例>>C#>>正文


C# MessageBus.Subscribe方法代码示例

本文整理汇总了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);
        }
开发者ID:brentedwards,项目名称:Charmed,代码行数:26,代码来源:MessageBusTests.cs

示例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));
        }
开发者ID:bestpetrovich,项目名称:devicehive-.net,代码行数:10,代码来源:MessageBusConfig.cs

示例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]);
        }
开发者ID:brentedwards,项目名称:MvvmFabric,代码行数:13,代码来源:MessageBusTests.cs

示例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);
        }
开发者ID:eatskolnikov,项目名称:mobile,代码行数:13,代码来源:WidgetSyncManager.cs

示例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);
        }
开发者ID:VDBBjorn,项目名称:toggl_mobile,代码行数:14,代码来源:WidgetSyncManager.cs

示例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));
        }
开发者ID:oryol,项目名称:devicehive-.net,代码行数:14,代码来源:MessageBusConfig.cs

示例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);
        }
开发者ID:yonglehou,项目名称:Precio.Infrastructure,代码行数:30,代码来源:Messaging.Tests.cs

示例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++;
            }
        }
开发者ID:hotgazpacho,项目名称:zmqPubSub,代码行数:31,代码来源:Program.cs

示例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);
		}
开发者ID:maritaria,项目名称:HotBot,代码行数:11,代码来源:ReflectionPluginManager.cs

示例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);
        }
开发者ID:brentedwards,项目名称:MvvmFabric,代码行数:10,代码来源:MessageBusTests.cs

示例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);
        }
开发者ID:yonglehou,项目名称:Precio.Infrastructure,代码行数:21,代码来源:Messaging.Tests.cs

示例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);
        }
开发者ID:brentedwards,项目名称:MvvmFabric,代码行数:12,代码来源:MessageBusTests.cs

示例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);
        }
开发者ID:lukedoolittle,项目名称:SimpleCQRS.Portable,代码行数:13,代码来源:MessageBusTests.cs

示例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);
        }
开发者ID:brentedwards,项目名称:Charmed,代码行数:13,代码来源:MessageBusTests.cs

示例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());
            });
        }
开发者ID:rustd,项目名称:SignalR,代码行数:51,代码来源:MessageBusRun.cs


注:本文中的MessageBus.Subscribe方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。