当前位置:首页 > Web开发 > 正文

command).ConfigureAwait( false); break;} case ConsoleKey.Q:

2024-03-31 Web开发

NServiceBus是.Net平台下的开源的动静处事框架,已经撑持.Net Core。目前不变版本7.1。企业开发需要采办License,开发者可在线下载开发者License。
官方网站:https://particular.net/
官方示例:https://docs.particular.net/get-started/

NServiceBus入门

技术图片


如图所示,项目一共包孕4个端点(Endpoint),也就是四个单独的项目,端点是NServiceBus中的核心观点,,发送动静和事件颁布订阅的根本都是Endpoint。这个项目中包孕发送动静和事件的颁布订阅。

完整的项目布局如图所示:

技术图片

ClientUI class Program { private static ILog log = LogManager.GetLogger<Program>(); static void Main(string[] args) { MainAsync().GetAwaiter().GetResult(); } static async Task RunAsync(IEndpointInstance endpointInstance) { log.Info("Press ‘P‘ to place an order,press ‘Q‘ to quit"); while (true) { var key = Console.ReadKey(); Console.WriteLine(); switch (key.Key) { case ConsoleKey.P: { var command = new PlaceOrder { OrderId = Guid.NewGuid().ToString() }; log.Info($"Sending PlaceOrder with OrderId:{command.OrderId}"); //发送到Sales端点 await endpointInstance.Send("Sales",command).ConfigureAwait(false); break; } case ConsoleKey.Q: return; default: log.Info("Please try again"); break; } } } static async Task MainAsync() { Console.Title = "Client-UI"; var config = new EndpointConfiguration("ClientUI");//设置端点名称 config.UseTransport<LearningTransport>(); //设置动静管道模式,LearningTransport仅仅用来学习,出产慎用 config.UsePersistence<LearningPersistence>();//长期化 var endpointInstance =await Endpoint.Start(config).ConfigureAwait(false); await RunAsync(endpointInstance).ConfigureAwait(false); //RunAsync返回的是Task,所以这里使用ConfigureAwait() await endpointInstance.Stop().ConfigureAwait(false); } } Sales class Program { static async Task Main(string[] args) { Console.Title = "Sales"; var config = new EndpointConfiguration("Sales"); config.UseTransport<LearningTransport>(); config.UsePersistence<LearningPersistence>(); var endpointInstance = await Endpoint.Start(config).ConfigureAwait(false); Console.WriteLine("Press Enter to quit..."); Console.ReadLine(); await endpointInstance.Stop().ConfigureAwait(false); } } public class PlaceOrderHandler:IHandleMessages<PlaceOrder> { private static ILog log = LogManager.GetLogger<PlaceOrderHandler>(); public Task Handle(PlaceOrder message, IMessageHandlerContext context) { //接受端点动静 log.Info($"Received PlaceOrder ,OrderId:{message.OrderId}"); //颁布OrderPlaced事件 var order=new OrderPlaced(); order.OrderId = message.OrderId; return context.Publish(order); } } Billing static async Task Main(string[] args) { Console.Title = "Sales"; var config = new EndpointConfiguration("Billing"); config.UseTransport<LearningTransport>(); config.UsePersistence<LearningPersistence>(); var endpointInstance = await Endpoint.Start(config).ConfigureAwait(false); Console.WriteLine("Press Enter to quit..."); Console.ReadLine(); await endpointInstance.Stop().ConfigureAwait(false); } public class OrderPlacedHandler:IHandleMessages<OrderPlaced> { private static ILog log = LogManager.GetLogger<OrderPlacedHandler>(); public Task Handle(OrderPlaced message, IMessageHandlerContext context) { //订阅OrderPlaced事件 log.Info($"Received OrderPlaced,OrderId {message.OrderId} - Charging credit card"); //颁布OrderBilled事件 var order=new OrderBilled(); order.OrderId = message.OrderId; return context.Publish(order); } } Shipping static async Task Main(string[] args) { Console.Title = "Sales"; var config = new EndpointConfiguration("Shipping"); config.UseTransport<LearningTransport>(); config.UsePersistence<LearningPersistence>(); var endpointInstance = await Endpoint.Start(config).ConfigureAwait(false); Console.WriteLine("Press Enter to quit..."); Console.ReadLine(); await endpointInstance.Stop().ConfigureAwait(false); } public class OrderBilledHandler:IHandleMessages<OrderBilled> { private static ILog log = LogManager.GetLogger<OrderBilledHandler>(); //措置惩罚惩罚OrderBilled订阅事件 public Task Handle(OrderBilled message, IMessageHandlerContext context) { log.Info($"Received OrderBilled,OrderId={message.OrderId} Should we ship now?"); return Task.CompletedTask; } } public class OrderPlacedHandler:IHandleMessages<OrderPlaced> { private static ILog log = LogManager.GetLogger<OrderPlacedHandler>(); //措置惩罚惩罚OrderPlaced订阅事件 public Task Handle(OrderPlaced message, IMessageHandlerContext context) { log.Info($"Received OrderPlaced,OrderId={message.OrderId} Should we ship now?"); return Task.CompletedTask; } } 运行功效

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/31099.html