Public Task Consume(ConsumeContext context) Public class RegisterCustomerConsumer : IConsumer Here’s the updated RegisterCustomerConsumer object: We’re lucky because the ConsumeContext object of the Consume method has the same function. However we have no access to the bus in the consumer. Here’s an example of publishing an IRegisterCustomer: The IBusControl interface has a Publish method that could do the job. One way of achieving it is publishing the event from the RegisterCustomerConsumer object. Our goal is to publish an event as soon as the command has been taken care of. We’ll have 4 command windows up and running when starting the demo at the end of this post so it’s good to have an easy means of identifying which window belongs to what. We’ll first make a slight change in the Main method of Program.cs:Ĭonsole.Title = "This is the customer registration command receiver." Ĭonsole.WriteLine("CUSTOMER REGISTRATION COMMAND RECEIVER.") We’ve also registered an IConsumer called RegisterCustomerConsumer when building the bus control. It currently listens to commands on the queue called. We also have a project called MassTransit.Receiver in our demo project. When a new customer is registered then we won’t publish all the details to the consuming parties. It has some of the same properties as IRegisterCustomer. Go ahead and insert the following event interface into the library: Our demo project has a M圜ompany.Messaging C# library where we already have a command-style interface called IRegisterCustomer. We know from the previous post and the MassTransit documentation page we referred to that an event name consists of a noun, which is the resource or the domain, followed by a verb in past tense that describes what happened to the resource. The event will be consumed by 2 receivers that the event publisher will not have any knowledge of. The goal is to publish a customer registered event from the register customer command consumer. In this post we’ll extend our demo to publishing a message that can be consumed by multiple receivers. Our first example centred around sending a single command using a single queue. Instead, they are basic terminology in the world of messaging. Therefore commands and events are not some special C# language features in this case. Both are best encapsulated in an interface with get-set properties and separate naming conventions. The message type can by convention be an event or a command. We saw a very basic configuration of the bus control and how to register a consumer for a message type. We managed to send a message from a publisher to a consumer using the MassTransit/RabbitMq client library. Assert.Equal(context.In the previous post we got our hands dirty and started coding a small demo application around MassTransit. context.Headers = "Bob" ?Īwait (mockDataObject) Īssert.True(await ()) Īssert.False(await >()) I need to mock header values on ConsumeContext object here Harness.Consumer(() => new M圜onsumer(_hubContext)) Throw new M圜onsumerException(_reportName, $" Private string TryGetHeader(ConsumeContext context, string key, bool errorOnNull = true) Var userName = TryGetHeader(context, "UserName") ĭoSomethingWith(_hubContext, myProperty, userName) Public async Task Consume(ConsumeContext context) Private readonly IHubContext _hubContext So how do I mock the ConsumeContext type, and set a mock header value? I prefer a solution with NSubstitute public class MyDataObjectCommand I'm using the NSubstitute mocking library. I need to find a way to mock the bus header data otherwise the test will always raise an exception when it executes ("UserName", out object value) I've simplified my consumer class as follows. My consumer is expected a bus header key called UserName. I'm trying to create a unit test that will test my MassTransit (RabbitMq) consumer.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |