diff --git a/MasstransitDemo.Api/Controllers/NotificationController.cs b/MasstransitDemo.Api/Controllers/NotificationController.cs index f5707ff..08599c8 100644 --- a/MasstransitDemo.Api/Controllers/NotificationController.cs +++ b/MasstransitDemo.Api/Controllers/NotificationController.cs @@ -3,28 +3,28 @@ using MasstransitDemo.Shared; using Microsoft.AspNetCore.Mvc; -namespace MasstransitDemo.Api.Controllers +namespace MasstransitDemo.Api.Controllers; + +[Route("api/[controller]")] +[ApiController] +public class NotificationController : ControllerBase { - [Route("api/[controller]")] - [ApiController] - public class NotificationController : ControllerBase + public readonly IPublishEndpoint publishEndpoint; + + public NotificationController(IPublishEndpoint publishEndpoint) { - public readonly IPublishEndpoint publishEndpoint; + this.publishEndpoint = publishEndpoint; + } - public NotificationController(IPublishEndpoint publishEndpoint) - { - this.publishEndpoint = publishEndpoint; - } + [HttpPost] + public async Task Notify(NotificationDto notificationDto) + { + await publishEndpoint.Publish(new { + NotificationDate = notificationDto.NotificationDate, + NotificationMessage = notificationDto.NotificationMessage, + NotificationType = notificationDto.NotificationType + }); - [HttpPost] - public async Task Notify(NotificationModel model) - { - await publishEndpoint.Publish(new NotificationSubmitted - { - NotificationDate = model.NotificationDate, - NotificationMessage = model.NotificationMessage, - NotificationType = model.NotificationType - }); - } + return Ok(); } } \ No newline at end of file diff --git a/MasstransitDemo.Api/Models/NotificationModel.cs b/MasstransitDemo.Api/Models/NotificationDto.cs similarity index 88% rename from MasstransitDemo.Api/Models/NotificationModel.cs rename to MasstransitDemo.Api/Models/NotificationDto.cs index ffbc3fe..e452eb1 100644 --- a/MasstransitDemo.Api/Models/NotificationModel.cs +++ b/MasstransitDemo.Api/Models/NotificationDto.cs @@ -2,7 +2,7 @@ namespace MasstransitDemo.Api.Models { - public class NotificationModel + public class NotificationDto { public DateTime NotificationDate { get; set; } public string NotificationMessage { get; set; } diff --git a/MasstransitDemo.Api/Models/NotificationSubmitted.cs b/MasstransitDemo.Api/Models/NotificationSubmitted.cs deleted file mode 100644 index 6abd60a..0000000 --- a/MasstransitDemo.Api/Models/NotificationSubmitted.cs +++ /dev/null @@ -1,13 +0,0 @@ -using MasstransitDemo.Shared; - -namespace MasstransitDemo.Api.Models -{ - public class NotificationSubmitted : INotificationSubmitted - { - public DateTime NotificationDate { get; set; } - - public string NotificationMessage { get; set; } - - public NotificationType NotificationType { get; set; } - } -} \ No newline at end of file diff --git a/MasstransitDemo.Api/Observers/PublishObserver.cs b/MasstransitDemo.Api/Observers/PublishObserver.cs deleted file mode 100644 index f94eaeb..0000000 --- a/MasstransitDemo.Api/Observers/PublishObserver.cs +++ /dev/null @@ -1,35 +0,0 @@ -using MassTransit; -using MassTransit.Util; - -namespace MasstransitDemo.Api.Observers -{ - public class PublishObserver : IPublishObserver - { - /// - /// called right before the message is published (sent to exchange or topic) - /// - public Task PrePublish(PublishContext context) - where T : class - { - return TaskUtil.Completed; - } - - /// - /// called after the message is published (and acked by the broker if RabbitMQ) - /// - public Task PostPublish(PublishContext context) - where T : class - { - return TaskUtil.Completed; - } - - /// - /// called if there was an exception publishing the message - /// - public Task PublishFault(PublishContext context, Exception exception) - where T : class - { - return TaskUtil.Completed; - } - } -} \ No newline at end of file diff --git a/MasstransitDemo.Api/Program.cs b/MasstransitDemo.Api/Program.cs index db824ad..62075e7 100644 --- a/MasstransitDemo.Api/Program.cs +++ b/MasstransitDemo.Api/Program.cs @@ -1,7 +1,4 @@ using MassTransit; -using MasstransitDemo.Api.Observers; -using MasstransitDemo.Shared; -using System.Reflection; var builder = WebApplication.CreateBuilder(args); @@ -9,24 +6,17 @@ builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -var services = builder.Services; - -services.AddSingleton(); -services.AddMassTransit(busConfigurator => +builder.Services.AddMassTransit(busConfigurator => { busConfigurator.SetKebabCaseEndpointNameFormatter(); busConfigurator.UsingRabbitMq((context, busFactoryConfigurator) => { busFactoryConfigurator.Host("rabbitmq", hostConfigurator => { }); - - busFactoryConfigurator.ConnectPublishObserver(context.GetRequiredService()); }); - //EndpointConvention.Map(new Uri($"queue:{RabbitMQOptions.TransactionQueue}")); }); var app = builder.Build(); -// Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); diff --git a/MasstransitDemo.Api/Properties/launchSettings.json b/MasstransitDemo.Api/Properties/launchSettings.json index 337d933..2142566 100644 --- a/MasstransitDemo.Api/Properties/launchSettings.json +++ b/MasstransitDemo.Api/Properties/launchSettings.json @@ -11,7 +11,7 @@ "profiles": { "MasstransitDemo.Api": { "commandName": "Project", - "launchBrowser": false, + "launchBrowser": true, "launchUrl": "swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" @@ -21,7 +21,7 @@ }, "IIS Express": { "commandName": "IISExpress", - "launchBrowser": false, + "launchBrowser": true, "launchUrl": "swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" @@ -29,7 +29,7 @@ }, "Docker": { "commandName": "Docker", - "launchBrowser": false, + "launchBrowser": true, "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger", "publishAllPorts": false, "environmentVariables": { diff --git a/MasstransitDemo.Consumer/ConsumeObserver.cs b/MasstransitDemo.Consumer/ConsumeObserver.cs deleted file mode 100644 index b834e61..0000000 --- a/MasstransitDemo.Consumer/ConsumeObserver.cs +++ /dev/null @@ -1,36 +0,0 @@ -using MassTransit; -using MassTransit.Util; - -namespace MasstransitDemo.Consumer; - -public class ConsumeObserver : IConsumeObserver -{ - Task IConsumeObserver.PreConsume(ConsumeContext context) - { - return TaskUtil.Completed; - } - - Task IConsumeObserver.PostConsume(ConsumeContext context) - { - return TaskUtil.Completed; - } - - Task IConsumeObserver.ConsumeFault(ConsumeContext context, Exception exception) - { - //var request = context.Message; - - //var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(request); - - //var exceptionMessage = $"Exception Message: {exception.Message}\n\n"; - - //if (exception.InnerException != default) - //{ - // exceptionMessage += $"InnerException Message: {exception.InnerException.Message}\n\n"; - // exceptionMessage += $"InnerException StackTrace: {exception.InnerException.StackTrace}\n\n"; - //} - - //Log.Error($"request: {requestJson} {exceptionMessage}", exception); - - return TaskUtil.Completed; - } -} \ No newline at end of file diff --git a/MasstransitDemo.Consumer/IConsumer.cs b/MasstransitDemo.Consumer/IConsumer.cs deleted file mode 100644 index 5c7567c..0000000 --- a/MasstransitDemo.Consumer/IConsumer.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace MasstransitDemo.Consumer; - -public interface IConsumer -{ -} \ No newline at end of file diff --git a/MasstransitDemo.Consumer/INotificationProcessor.cs b/MasstransitDemo.Consumer/INotificationProcessor.cs deleted file mode 100644 index 955221b..0000000 --- a/MasstransitDemo.Consumer/INotificationProcessor.cs +++ /dev/null @@ -1,9 +0,0 @@ -using MasstransitDemo.Shared; - -namespace MasstransitDemo.Consumer -{ - public interface INotificationProcessor - { - Task Process(INotificationSubmitted message); - } -} \ No newline at end of file diff --git a/MasstransitDemo.Consumer/NotificationCreatedConsumer.cs b/MasstransitDemo.Consumer/NotificationCreatedConsumer.cs new file mode 100644 index 0000000..a8f902e --- /dev/null +++ b/MasstransitDemo.Consumer/NotificationCreatedConsumer.cs @@ -0,0 +1,15 @@ +using MassTransit; +using MasstransitDemo.Shared; +using System.Text.Json; + +namespace MasstransitDemo.Consumer; + +public class NotificationCreatedConsumer : IConsumer +{ + public async Task Consume(ConsumeContext context) + { + var serializedMessage = JsonSerializer.Serialize(context.Message, new JsonSerializerOptions { }); + + Console.WriteLine($"NotificationCreated event consumed. Message: {serializedMessage}"); + } +} \ No newline at end of file diff --git a/MasstransitDemo.Consumer/NotificationProcessor.cs b/MasstransitDemo.Consumer/NotificationProcessor.cs deleted file mode 100644 index 760eb5f..0000000 --- a/MasstransitDemo.Consumer/NotificationProcessor.cs +++ /dev/null @@ -1,13 +0,0 @@ -using MasstransitDemo.Shared; - -namespace MasstransitDemo.Consumer -{ - public class NotificationProcessor : INotificationProcessor - { - public async Task Process(INotificationSubmitted message) - { - Console.WriteLine(message); - await Task.Delay(10); - } - } -} \ No newline at end of file diff --git a/MasstransitDemo.Consumer/Program.cs b/MasstransitDemo.Consumer/Program.cs index 84b3733..93b63ee 100644 --- a/MasstransitDemo.Consumer/Program.cs +++ b/MasstransitDemo.Consumer/Program.cs @@ -1,6 +1,4 @@ using MassTransit; -using MasstransitDemo.Consumer; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System.Reflection; @@ -8,8 +6,6 @@ builder.ConfigureServices((hostContext, services) => { - services.AddSingleton(); - services.AddSingleton(); services.AddMassTransit(busConfigurator => { var entryAssembly = Assembly.GetExecutingAssembly(); @@ -17,15 +13,13 @@ busConfigurator.AddConsumers(entryAssembly); busConfigurator.UsingRabbitMq((context, busFactoryConfigurator) => { - busConfigurator.SetKebabCaseEndpointNameFormatter(); - busFactoryConfigurator.Host("rabbitmq", h => { }); + busFactoryConfigurator.Host("rabbitmq", "/", h => { }); busFactoryConfigurator.ConfigureEndpoints(context); - busFactoryConfigurator.ConnectConsumeObserver(context.GetRequiredService()); }); }); }); var app = builder.Build(); -app.Run(); +app.Run(); \ No newline at end of file diff --git a/MasstransitDemo.Consumer/SubmitNoticationConsumer.cs b/MasstransitDemo.Consumer/SubmitNoticationConsumer.cs deleted file mode 100644 index efd0f40..0000000 --- a/MasstransitDemo.Consumer/SubmitNoticationConsumer.cs +++ /dev/null @@ -1,20 +0,0 @@ -using MassTransit; -using MasstransitDemo.Shared; - -namespace MasstransitDemo.Consumer -{ - public class SubmitNoticationConsumer : IConsumer - { - private readonly INotificationProcessor notificationProcessor; - - public SubmitNoticationConsumer(INotificationProcessor notificationProcessor) - { - this.notificationProcessor = notificationProcessor; - } - - public async Task Consume(ConsumeContext context) - { - notificationProcessor.Process(context.Message); - } - } -} \ No newline at end of file diff --git a/MasstransitDemo.Shared/INotificationCreated.cs b/MasstransitDemo.Shared/INotificationCreated.cs new file mode 100644 index 0000000..dffbb4f --- /dev/null +++ b/MasstransitDemo.Shared/INotificationCreated.cs @@ -0,0 +1,8 @@ +namespace MasstransitDemo.Shared; + +public interface INotificationCreated +{ + DateTime NotificationDate { get; } + string NotificationMessage { get; } + NotificationType NotificationType { get; } +} \ No newline at end of file diff --git a/MasstransitDemo.Shared/INotificationSubmitted.cs b/MasstransitDemo.Shared/INotificationSubmitted.cs deleted file mode 100644 index 2197d6d..0000000 --- a/MasstransitDemo.Shared/INotificationSubmitted.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace MasstransitDemo.Shared -{ - public interface INotificationSubmitted - { - DateTime NotificationDate { get; } - string NotificationMessage { get; } - NotificationType NotificationType { get; } - } -} \ No newline at end of file diff --git a/MasstransitDemo.Shared/NotificationType.cs b/MasstransitDemo.Shared/NotificationType.cs index 2769eaa..25614de 100644 --- a/MasstransitDemo.Shared/NotificationType.cs +++ b/MasstransitDemo.Shared/NotificationType.cs @@ -1,9 +1,8 @@ -namespace MasstransitDemo.Shared +namespace MasstransitDemo.Shared; + +public enum NotificationType { - public enum NotificationType - { - Email, - Push, - Sms - } + Email, + Push, + Sms } \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 7b21d54..8296d57 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,9 +9,11 @@ services: ports: - '7047:443' - '7048:80' + depends_on: + - "rabbitmq" rabbitmq: - container_name: "rabbitmq" + container_name: "rabbitmqcontainer" image: rabbitmq:management hostname: "rabbitmq" ports: @@ -24,5 +26,4 @@ services: context: . dockerfile: MasstransitDemo.Consumer/Dockerfile depends_on: - - "rabbitmq" - + - "rabbitmq" \ No newline at end of file