var endpointConfiguration = new EndpointConfiguration("MyEndpoint"); var endpointInstance = await Endpoint.Start(endpointConfiguration).ConfigureAwait(false); var busContext = endpointInstance.CreateBusContext(); // Use busContext to handle incoming messages
public class MyMessage : IMessage { public string Content { get; set; } } public class MyMessageHandler : IHandleMessagesIn this example, we define a message class MyMessage and a message handler class MyMessageHandler that handles messages of type MyMessage. In the handle method, we print out the message content and send a reply message using the endpoint instance passed in the constructor. We then create an endpoint configuration and start the endpoint instance. We create a bus context using the endpoint instance and subscribe the message handler to handle incoming messages of type MyMessage. Finally, we send a message of type MyMessage to the endpoint instance using the bus context. Package library: NServiceBus.{ private readonly IEndpointInstance _endpointInstance; public MyMessageHandler(IEndpointInstance endpointInstance) { _endpointInstance = endpointInstance; } public async Task Handle(MyMessage message, IMessageHandlerContext context) { // Handle the message Console.WriteLine(message.Content); // Reply to the message var replyMessage = new MyMessage { Content = "Hello, world!" }; await _endpointInstance.SendLocal(replyMessage).ConfigureAwait(false); } } var endpointConfiguration = new EndpointConfiguration("MyEndpoint"); endpointConfiguration.UseTransport (); var endpointInstance = await Endpoint.Start(endpointConfiguration).ConfigureAwait(false); var busContext = endpointInstance.CreateBusContext(); // Register the message handler var messageHandler = new MyMessageHandler(endpointInstance); busContext.Subscribe (messageHandler); // Send a message to the endpoint instance var message = new MyMessage { Content = "Hello" }; await busContext.Send(message).ConfigureAwait(false);