本文整理汇总了C#中ConcurrentBag.OfType方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentBag.OfType方法的具体用法?C# ConcurrentBag.OfType怎么用?C# ConcurrentBag.OfType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentBag
的用法示例。
在下文中一共展示了ConcurrentBag.OfType方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestServiceBusWithEmbeddedBroker
public void TestServiceBusWithEmbeddedBroker()
{
// use the embedded broker
var brokerUri = _broker.FailoverUri;
// set up ServiceBus using fluent interfaces and all current endpoints and pointing at test AMQ broker
IServiceBus serviceBus = ServiceBus.Configure()
.WithActiveMQEndpoints<ITestMessage1>()
.Named("Obvs.TestService")
.UsingQueueFor<TestCommand>().ClientAcknowledge()
.UsingQueueFor<TestCommand2>().ClientAcknowledge()
.UsingQueueFor<IRequest>().AutoAcknowledge()
.ConnectToBroker(brokerUri)
.SerializedAsJson()
.AsClientAndServer()
.PublishLocally()
.OnlyMessagesWithNoEndpoints()
.UsingConsoleLogging()
.Create();
// create threadsafe collection to hold received messages in
ConcurrentBag<IMessage> messages = new ConcurrentBag<IMessage>();
// create some actions that will act as a fake services acting on incoming commands and requests
Action<TestCommand> fakeService1 = command => serviceBus.PublishAsync(new TestEvent {Id = command.Id});
Action<TestRequest> fakeService2 = request => serviceBus.ReplyAsync(request, new TestResponse {Id = request.Id});
AnonymousObserver<IMessage> observer = new AnonymousObserver<IMessage>(messages.Add, Console.WriteLine, () => Console.WriteLine("OnCompleted"));
// subscribe to all messages on the ServiceBus
CompositeDisposable subscriptions = new CompositeDisposable
{
serviceBus.Events.Subscribe(observer),
serviceBus.Commands.Subscribe(observer),
serviceBus.Requests.Subscribe(observer),
serviceBus.Commands.OfType<TestCommand>().Subscribe(fakeService1),
serviceBus.Requests.OfType<TestRequest>().Subscribe(fakeService2)
};
// send some messages
serviceBus.SendAsync(new TestCommand { Id = 123 });
serviceBus.SendAsync(new TestCommand2 { Id = 123 });
serviceBus.SendAsync(new TestCommand3 { Id = 123 });
serviceBus.GetResponses(new TestRequest { Id = 456 }).Subscribe(observer);
// wait some time until we think all messages have been sent and received over AMQ
Thread.Sleep(TimeSpan.FromSeconds(1));
// test we got everything we expected
Assert.That(messages.OfType<TestCommand>().Count() == 1, "TestCommand not received");
Assert.That(messages.OfType<TestCommand2>().Count() == 1, "TestCommand2 not received");
Assert.That(messages.OfType<TestCommand3>().Count() == 1, "TestCommand3 not received");
Assert.That(messages.OfType<TestEvent>().Count() == 1, "TestEvent not received");
Assert.That(messages.OfType<TestRequest>().Count() == 1, "TestRequest not received");
Assert.That(messages.OfType<TestResponse>().Count() == 1, "TestResponse not received");
subscriptions.Dispose();
((IDisposable)serviceBus).Dispose();
// win!
}