本文整理汇总了C#中MessageBus.Publish方法的典型用法代码示例。如果您正苦于以下问题:C# MessageBus.Publish方法的具体用法?C# MessageBus.Publish怎么用?C# MessageBus.Publish使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MessageBus
的用法示例。
在下文中一共展示了MessageBus.Publish方法的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: 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: 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);
}
示例4: 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);
}
示例5: 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);
}
示例6: 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);
}
示例7: SubscribeNotExisting
public void SubscribeNotExisting()
{
var bus = new MessageBus();
bus.Subscribe<Object>(Handler);
var message = new Object();
bus.Publish<Object>(message);
Assert.AreEqual(1, _Messages.Count);
Assert.AreSame(message, _Messages[0]);
}
示例8: 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);
}
示例9: 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);
}
示例10: 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);
}
示例11: 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());
});
}
示例12: AddingEventAndSendingMessages
public void AddingEventAndSendingMessages()
{
var dr = new DefaultDependencyResolver();
using (var bus = new MessageBus(dr))
{
var subscriber = new TestSubscriber(new[] { "a" });
int max = 100;
var cd = new CountDownRange<int>(Enumerable.Range(0, max));
int prev = -1;
IDisposable subscription = null;
try
{
subscription = bus.Subscribe(subscriber, null, result =>
{
foreach (var m in result.GetMessages())
{
int n = Int32.Parse(m.Value);
Assert.True(prev < n, "out of order");
prev = n;
Assert.True(cd.Mark(n));
}
return TaskAsyncHelper.True;
}, 10);
for (int i = 0; i < max; i++)
{
subscriber.AddEvent("b");
bus.Publish("test", "b", i.ToString()).Wait();
}
Assert.True(cd.Wait(TimeSpan.FromSeconds(10)));
}
finally
{
if (subscription != null)
{
subscription.Dispose();
}
}
}
}
示例13: Main
private static void Main()
{
var bus = new MessageBus("Publisher");
bus.Start();
//for (int i = 0; i < 100; i++)
//{
// bus.Send(new SimpleMessage {Data = "Hello World"}, "ServiceB");
//}
Console.ReadLine();
for (int i = 0; i < 100; i++)
{
bus.Publish(new TradeOpportunityFound());
}
Console.ReadLine();
}
示例14: NewSubscriptionGetsAllMessages
public void NewSubscriptionGetsAllMessages()
{
var dr = new DefaultDependencyResolver();
var bus = new MessageBus(dr);
var subscriber = new TestSubscriber(new[] { "key" });
var wh = new ManualResetEventSlim(initialState: false);
IDisposable subscription = null;
try
{
bus.Publish("test", "key", "1").Wait();
subscription = bus.Subscribe(subscriber, null, result =>
{
if (!result.Terminal)
{
var m = result.GetMessages().Single();
Assert.Equal("key", m.Key);
Assert.Equal("value", m.Value);
wh.Set();
return TaskAsyncHelper.True;
}
return TaskAsyncHelper.False;
}, 10);
bus.Publish("test", "key", "value").Wait();
Assert.True(wh.Wait(TimeSpan.FromSeconds(5)));
}
finally
{
if (subscription != null)
{
subscription.Dispose();
}
}
}
示例15: Unsubscribe_MultipleInstances_SameClass
public void Unsubscribe_MultipleInstances_SameClass()
{
// Arrange
var bus = new MessageBus();
// Subscribe two objects of the same type.
var subscriber1 = new TestSubscriber();
bus.Subscribe<Object>(subscriber1.Handler);
var subscriber2 = new TestSubscriber();
bus.Subscribe<Object>(subscriber2.Handler);
// Act
// Unsubscribe the second of the objects and make sure the other first one still gets messages.
bus.Unsubscribe<Object>(subscriber2.Handler);
var message = new Object();
bus.Publish<Object>(message);
// Assert
Assert.AreEqual(0, subscriber2.Messages.Count);
Assert.AreEqual(1, subscriber1.Messages.Count);
}