From 8b1b444437a84be42e055039193932adf72ec237 Mon Sep 17 00:00:00 2001 From: Bao To Quoc Date: Tue, 16 Apr 2024 01:45:34 +0700 Subject: [PATCH 1/9] Aspire masstransit sample --- .../AspireWithMasstransit.ApiService.csproj | 17 +++ .../HelloAspireEventConsumer.cs | 14 ++ .../Program.cs | 41 ++++++ .../Properties/launchSettings.json | 25 ++++ .../appsettings.Development.json | 8 ++ .../appsettings.json | 9 ++ .../AspireWithMasstransit.AppHost.csproj | 21 +++ .../AspireWithMasstransit.AppHost/Program.cs | 8 ++ .../Properties/launchSettings.json | 29 +++++ .../appsettings.Development.json | 8 ++ .../appsettings.json | 9 ++ ...pireWithMasstransit.ServiceDefaults.csproj | 23 ++++ .../Extensions.cs | 123 ++++++++++++++++++ .../AspireWithMasstransit.sln | 36 +++++ 14 files changed, 371 insertions(+) create mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/AspireWithMasstransit.ApiService.csproj create mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/HelloAspireEventConsumer.cs create mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Program.cs create mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Properties/launchSettings.json create mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/appsettings.Development.json create mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/appsettings.json create mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/AspireWithMasstransit.AppHost.csproj create mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/Program.cs create mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/Properties/launchSettings.json create mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/appsettings.Development.json create mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/appsettings.json create mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/AspireWithMasstransit.ServiceDefaults.csproj create mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/Extensions.cs create mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.sln diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/AspireWithMasstransit.ApiService.csproj b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/AspireWithMasstransit.ApiService.csproj new file mode 100644 index 000000000..6099fd260 --- /dev/null +++ b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/AspireWithMasstransit.ApiService.csproj @@ -0,0 +1,17 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/HelloAspireEventConsumer.cs b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/HelloAspireEventConsumer.cs new file mode 100644 index 000000000..a2e964293 --- /dev/null +++ b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/HelloAspireEventConsumer.cs @@ -0,0 +1,14 @@ +using MassTransit; + +namespace AspireWithMasstransit.ApiService; + +public class HelloAspireEventConsumer(ILogger logger) : IConsumer +{ + public Task Consume(ConsumeContext context) + { + logger.LogInformation("Received: {Message}", context.Message.Message); + return Task.CompletedTask; + } +} + +public record HelloAspireEvent(string Message); diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Program.cs b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Program.cs new file mode 100644 index 000000000..3e93855ae --- /dev/null +++ b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Program.cs @@ -0,0 +1,41 @@ +using System.Reflection; +using AspireWithMasstransit.ApiService; +using MassTransit; + +var builder = WebApplication.CreateBuilder(args); + +// Add service defaults & Aspire components. +builder.AddServiceDefaults(); + +// Add services to the container. +builder.Services.AddProblemDetails(); + +var connectionString = builder.Configuration.GetConnectionString("messaging"); + +builder.Services.AddMassTransit(s => +{ + s.AddConsumers(Assembly.GetExecutingAssembly()); + s.UsingRabbitMq((context, cfg) => + { + cfg.Host(new Uri(connectionString!), "/"); + cfg.ConfigureEndpoints(context); + }); +}); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +app.UseExceptionHandler(); + + +app.MapGet("/", async (IPublishEndpoint publishEndpoint) => +{ + await publishEndpoint.Publish(new HelloAspireEvent("Hello, .NET!")); + await publishEndpoint.Publish(new HelloAspireEvent("Hello, Aspire!")); + + return Results.Ok("ok"); +}); + +app.MapDefaultEndpoints(); + +app.Run(); diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Properties/launchSettings.json b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Properties/launchSettings.json new file mode 100644 index 000000000..8c2c58465 --- /dev/null +++ b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Properties/launchSettings.json @@ -0,0 +1,25 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "", + "applicationUrl": "http://localhost:5421", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "", + "applicationUrl": "https://localhost:7442;http://localhost:5421", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/appsettings.Development.json b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/appsettings.Development.json new file mode 100644 index 000000000..0c208ae91 --- /dev/null +++ b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/appsettings.json b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/appsettings.json new file mode 100644 index 000000000..10f68b8c8 --- /dev/null +++ b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/AspireWithMasstransit.AppHost.csproj b/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/AspireWithMasstransit.AppHost.csproj new file mode 100644 index 000000000..02c916307 --- /dev/null +++ b/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/AspireWithMasstransit.AppHost.csproj @@ -0,0 +1,21 @@ + + + + Exe + net8.0 + enable + enable + true + 7CA514BA-715B-44D5-8C4A-D05E95F6E3BB + + + + + + + + + + + + diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/Program.cs b/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/Program.cs new file mode 100644 index 000000000..f2ee998ec --- /dev/null +++ b/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/Program.cs @@ -0,0 +1,8 @@ +var builder = DistributedApplication.CreateBuilder(args); + +var messaging = builder.AddRabbitMQ("messaging"); + +var apiService = builder.AddProject("apiservice") + .WithReference(messaging); + +builder.Build().Run(); diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/Properties/launchSettings.json b/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/Properties/launchSettings.json new file mode 100644 index 000000000..cdf3d9dea --- /dev/null +++ b/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/Properties/launchSettings.json @@ -0,0 +1,29 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17003;http://localhost:15085", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21117", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22025" + } + }, + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15085", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19120", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20161" + } + } + } +} diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/appsettings.Development.json b/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/appsettings.Development.json new file mode 100644 index 000000000..0c208ae91 --- /dev/null +++ b/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/appsettings.json b/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/appsettings.json new file mode 100644 index 000000000..31c092aa4 --- /dev/null +++ b/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + } +} diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/AspireWithMasstransit.ServiceDefaults.csproj b/samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/AspireWithMasstransit.ServiceDefaults.csproj new file mode 100644 index 000000000..13a6afe2a --- /dev/null +++ b/samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/AspireWithMasstransit.ServiceDefaults.csproj @@ -0,0 +1,23 @@ + + + + net8.0 + enable + enable + true + + + + + + + + + + + + + + + + diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/Extensions.cs b/samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/Extensions.cs new file mode 100644 index 000000000..4be641cbb --- /dev/null +++ b/samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/Extensions.cs @@ -0,0 +1,123 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Logging; +using OpenTelemetry.Logs; +using OpenTelemetry.Metrics; +using OpenTelemetry.Trace; + +namespace Microsoft.Extensions.Hosting; + +public static class Extensions +{ + public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder) + { + builder.ConfigureOpenTelemetry(); + + builder.AddDefaultHealthChecks(); + + builder.Services.AddServiceDiscovery(); + + builder.Services.ConfigureHttpClientDefaults(http => + { + // Turn on resilience by default + http.AddStandardResilienceHandler(); + + // Turn on service discovery by default + http.AddServiceDiscovery(); + }); + + return builder; + } + + public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder) + { + builder.Logging.AddOpenTelemetry(logging => + { + logging.IncludeFormattedMessage = true; + logging.IncludeScopes = true; + }); + + builder.Services.AddOpenTelemetry() + .WithMetrics(metrics => + { + metrics.AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddRuntimeInstrumentation(); + }) + .WithTracing(tracing => + { + if (builder.Environment.IsDevelopment()) + { + // We want to view all traces in development + tracing.SetSampler(new AlwaysOnSampler()); + } + + tracing.AddAspNetCoreInstrumentation() + // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package) + //.AddGrpcClientInstrumentation() + .AddHttpClientInstrumentation(); + }); + + builder.AddOpenTelemetryExporters(); + + return builder; + } + + private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostApplicationBuilder builder) + { + var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); + + if (useOtlpExporter) + { + builder.Services.Configure(logging => logging.AddOtlpExporter()); + builder.Services.ConfigureOpenTelemetryMeterProvider(metrics => metrics.AddOtlpExporter()); + builder.Services.ConfigureOpenTelemetryTracerProvider(tracing => tracing.AddOtlpExporter()); + } + + // Uncomment the following lines to enable the Prometheus exporter (requires the OpenTelemetry.Exporter.Prometheus.AspNetCore package) + // builder.Services.AddOpenTelemetry() + // .WithMetrics(metrics => metrics.AddPrometheusExporter()); + + // Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package) + //if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"])) + //{ + // builder.Services.AddOpenTelemetry() + // .UseAzureMonitor(); + //} + + return builder; + } + + public static IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicationBuilder builder) + { + builder.Services.AddHealthChecks() + // Add a default liveness check to ensure app is responsive + .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); + + return builder; + } + + public static WebApplication MapDefaultEndpoints(this WebApplication app) + { + // Uncomment the following line to enable the Prometheus endpoint (requires the OpenTelemetry.Exporter.Prometheus.AspNetCore package) + // app.MapPrometheusScrapingEndpoint(); + + // Adding health checks endpoints to applications in non-development environments has security implications. + // See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments. + if (app.Environment.IsDevelopment()) + { + // All health checks must pass for app to be considered ready to accept traffic after starting + app.MapHealthChecks("/health"); + + // Only health checks tagged with the "live" tag must pass for app to be considered alive + app.MapHealthChecks("/alive", new HealthCheckOptions + { + Predicate = r => r.Tags.Contains("live") + }); + } + + return app; + } +} \ No newline at end of file diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.sln b/samples/AspireWithMasstransit/AspireWithMasstransit.sln new file mode 100644 index 000000000..d9d37ea33 --- /dev/null +++ b/samples/AspireWithMasstransit/AspireWithMasstransit.sln @@ -0,0 +1,36 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.0.0 +MinimumVisualStudioVersion = 17.8.0.0 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspireWithMasstransit.AppHost", "AspireWithMasstransit.AppHost\AspireWithMasstransit.AppHost.csproj", "{942359AE-4183-4113-B49C-978C7CA24C7E}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspireWithMasstransit.ServiceDefaults", "AspireWithMasstransit.ServiceDefaults\AspireWithMasstransit.ServiceDefaults.csproj", "{40A4FBAC-BDD1-467E-96B9-45DC6FC33ACF}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspireWithMasstransit.ApiService", "AspireWithMasstransit.ApiService\AspireWithMasstransit.ApiService.csproj", "{AD70E94E-D614-4B50-8C08-F620A4E21FA2}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {942359AE-4183-4113-B49C-978C7CA24C7E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {942359AE-4183-4113-B49C-978C7CA24C7E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {942359AE-4183-4113-B49C-978C7CA24C7E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {942359AE-4183-4113-B49C-978C7CA24C7E}.Release|Any CPU.Build.0 = Release|Any CPU + {40A4FBAC-BDD1-467E-96B9-45DC6FC33ACF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {40A4FBAC-BDD1-467E-96B9-45DC6FC33ACF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {40A4FBAC-BDD1-467E-96B9-45DC6FC33ACF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {40A4FBAC-BDD1-467E-96B9-45DC6FC33ACF}.Release|Any CPU.Build.0 = Release|Any CPU + {AD70E94E-D614-4B50-8C08-F620A4E21FA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AD70E94E-D614-4B50-8C08-F620A4E21FA2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AD70E94E-D614-4B50-8C08-F620A4E21FA2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AD70E94E-D614-4B50-8C08-F620A4E21FA2}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {6C5D22D8-6EF2-43FF-A7FA-C8241CA43684} + EndGlobalSection +EndGlobal From 6a4fd02c82ba7e3369cf3b4b839698c70117ef51 Mon Sep 17 00:00:00 2001 From: Bao To Quoc Date: Thu, 18 Apr 2024 20:42:11 +0700 Subject: [PATCH 2/9] add readme --- .../Program.cs | 9 +--- samples/AspireWithMasstransit/README.md | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 samples/AspireWithMasstransit/README.md diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Program.cs b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Program.cs index 3e93855ae..76c8888d4 100644 --- a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Program.cs +++ b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Program.cs @@ -7,9 +7,6 @@ // Add service defaults & Aspire components. builder.AddServiceDefaults(); -// Add services to the container. -builder.Services.AddProblemDetails(); - var connectionString = builder.Configuration.GetConnectionString("messaging"); builder.Services.AddMassTransit(s => @@ -17,17 +14,13 @@ s.AddConsumers(Assembly.GetExecutingAssembly()); s.UsingRabbitMq((context, cfg) => { - cfg.Host(new Uri(connectionString!), "/"); + cfg.Host(new Uri(connectionString!)); cfg.ConfigureEndpoints(context); }); }); var app = builder.Build(); -// Configure the HTTP request pipeline. -app.UseExceptionHandler(); - - app.MapGet("/", async (IPublishEndpoint publishEndpoint) => { await publishEndpoint.Publish(new HelloAspireEvent("Hello, .NET!")); diff --git a/samples/AspireWithMasstransit/README.md b/samples/AspireWithMasstransit/README.md new file mode 100644 index 000000000..00015b155 --- /dev/null +++ b/samples/AspireWithMasstransit/README.md @@ -0,0 +1,41 @@ +--- +languages: +- csharp +products: +- dotnet +- dotnet-aspire +page_type: sample +name: ".NET Aspire Masstransit sample" +urlFragment: "aspire-dapr" +description: "An example of how to integrate Masstransit into a .NET Aspire app." +--- + +# Integrating Masstransit into a .NET Aspire application + +This sample demonstrates an approach for integrating [Masstransit](https://masstransit.io/) into a .NET Aspire application. + +## Pre-requisites + +- [.NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0) +- [Docker Desktop](https://www.docker.com/products/docker-desktop/) +- **Optional** [Visual Studio 2022 17.9 Preview](https://visualstudio.microsoft.com/vs/preview/) + +## Running the app + +If using Visual Studio, open the solution file `AspireWithMasstransit.sln` and launch/debug the `AspireWithMasstransit.AppHost` project. + +If using the .NET CLI, run `dotnet run` from the `AspireWithMasstransit.AppHost` directory. + +## Experiencing the app + +Once the app is running, the .NET Aspire dashboard will launch in your browser: + +Navigate to http://localhost:5421 in the browser to publish some messages. + +After that check the log. Consumer should received and consume the messages. + +``` +2024-04-18T20:37:35.2219340 info: AspireWithMasstransit.ApiService.HelloAspireEventConsumer[0] + Received: Hello, .NET! +2024-04-18T20:37:35.2219350 info: AspireWithMasstransit.ApiService.HelloAspireEventConsumer[0] + Received: Hello, Aspire! \ No newline at end of file From 5c9c4c65991f908bfc17a22326e60358a2b68c69 Mon Sep 17 00:00:00 2001 From: Bao To Quoc Date: Sun, 21 Apr 2024 15:09:49 +0700 Subject: [PATCH 3/9] fix review --- .../Program.cs | 8 ++++--- .../Extensions.cs | 21 +++---------------- samples/AspireWithMasstransit/README.md | 2 +- 3 files changed, 9 insertions(+), 22 deletions(-) diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Program.cs b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Program.cs index 76c8888d4..645e6d4fb 100644 --- a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Program.cs +++ b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Program.cs @@ -1,5 +1,6 @@ using System.Reflection; using AspireWithMasstransit.ApiService; +using AspireWithMasstransit.ServiceDefaults; using MassTransit; var builder = WebApplication.CreateBuilder(args); @@ -7,14 +8,15 @@ // Add service defaults & Aspire components. builder.AddServiceDefaults(); -var connectionString = builder.Configuration.GetConnectionString("messaging"); - +var connectionString = builder.Configuration.GetConnectionString("messaging") + ?? throw new InvalidOperationException("Connection string for RabbitMQ instance 'messaging' was not found."); + builder.Services.AddMassTransit(s => { s.AddConsumers(Assembly.GetExecutingAssembly()); s.UsingRabbitMq((context, cfg) => { - cfg.Host(new Uri(connectionString!)); + cfg.Host(new Uri(connectionString)); cfg.ConfigureEndpoints(context); }); }); diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/Extensions.cs b/samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/Extensions.cs index 4be641cbb..359fa7161 100644 --- a/samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/Extensions.cs +++ b/samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/Extensions.cs @@ -2,12 +2,13 @@ using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using OpenTelemetry.Logs; using OpenTelemetry.Metrics; using OpenTelemetry.Trace; -namespace Microsoft.Extensions.Hosting; +namespace AspireWithMasstransit.ServiceDefaults; public static class Extensions { @@ -55,8 +56,6 @@ public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicati } tracing.AddAspNetCoreInstrumentation() - // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package) - //.AddGrpcClientInstrumentation() .AddHttpClientInstrumentation(); }); @@ -76,17 +75,6 @@ private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostAppli builder.Services.ConfigureOpenTelemetryTracerProvider(tracing => tracing.AddOtlpExporter()); } - // Uncomment the following lines to enable the Prometheus exporter (requires the OpenTelemetry.Exporter.Prometheus.AspNetCore package) - // builder.Services.AddOpenTelemetry() - // .WithMetrics(metrics => metrics.AddPrometheusExporter()); - - // Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package) - //if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"])) - //{ - // builder.Services.AddOpenTelemetry() - // .UseAzureMonitor(); - //} - return builder; } @@ -101,9 +89,6 @@ public static IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicati public static WebApplication MapDefaultEndpoints(this WebApplication app) { - // Uncomment the following line to enable the Prometheus endpoint (requires the OpenTelemetry.Exporter.Prometheus.AspNetCore package) - // app.MapPrometheusScrapingEndpoint(); - // Adding health checks endpoints to applications in non-development environments has security implications. // See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments. if (app.Environment.IsDevelopment()) @@ -120,4 +105,4 @@ public static WebApplication MapDefaultEndpoints(this WebApplication app) return app; } -} \ No newline at end of file +} diff --git a/samples/AspireWithMasstransit/README.md b/samples/AspireWithMasstransit/README.md index 00015b155..4a4f82149 100644 --- a/samples/AspireWithMasstransit/README.md +++ b/samples/AspireWithMasstransit/README.md @@ -30,7 +30,7 @@ If using the .NET CLI, run `dotnet run` from the `AspireWithMasstransit.AppHost` Once the app is running, the .NET Aspire dashboard will launch in your browser: -Navigate to http://localhost:5421 in the browser to publish some messages. +Navigate to https://localhost:7442 in the browser to publish some messages. After that check the log. Consumer should received and consume the messages. From b05a4a8ace53918663e720eaf6ad8f2d2fc943c6 Mon Sep 17 00:00:00 2001 From: Bao To Quoc Date: Mon, 22 Apr 2024 12:11:26 +0700 Subject: [PATCH 4/9] update nuget package --- .../AspireWithMasstransit.AppHost.csproj | 4 ++-- .../AspireWithMasstransit.ServiceDefaults.csproj | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/AspireWithMasstransit.AppHost.csproj b/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/AspireWithMasstransit.AppHost.csproj index 02c916307..8e78ba493 100644 --- a/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/AspireWithMasstransit.AppHost.csproj +++ b/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/AspireWithMasstransit.AppHost.csproj @@ -14,8 +14,8 @@ - - + + diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/AspireWithMasstransit.ServiceDefaults.csproj b/samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/AspireWithMasstransit.ServiceDefaults.csproj index 13a6afe2a..2641f3e19 100644 --- a/samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/AspireWithMasstransit.ServiceDefaults.csproj +++ b/samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/AspireWithMasstransit.ServiceDefaults.csproj @@ -10,14 +10,14 @@ - - - - - - - - + + + + + + + + From 275a9ced92ec9dd3f906fcedb716a756846ebb8e Mon Sep 17 00:00:00 2001 From: Bao To Quoc Date: Mon, 22 Apr 2024 20:46:06 +0700 Subject: [PATCH 5/9] change add consumer to typeof(Program).Assembly --- .../AspireWithMasstransit.ApiService/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Program.cs b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Program.cs index 645e6d4fb..f9f2dc9fa 100644 --- a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Program.cs +++ b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Program.cs @@ -13,7 +13,7 @@ builder.Services.AddMassTransit(s => { - s.AddConsumers(Assembly.GetExecutingAssembly()); + s.AddConsumers(typeof(Program).Assembly); s.UsingRabbitMq((context, cfg) => { cfg.Host(new Uri(connectionString)); From 2fbc6dca2834811b88422255b3ab1d33deb3c3d2 Mon Sep 17 00:00:00 2001 From: Bao To Quoc Date: Fri, 8 Nov 2024 20:03:40 +0700 Subject: [PATCH 6/9] remove old version --- .../AspireWithMasstransit.ApiService.csproj | 17 --- .../HelloAspireEventConsumer.cs | 14 --- .../Program.cs | 36 ------ .../Properties/launchSettings.json | 25 ---- .../appsettings.Development.json | 8 -- .../appsettings.json | 9 -- .../AspireWithMasstransit.AppHost.csproj | 21 ---- .../AspireWithMasstransit.AppHost/Program.cs | 8 -- .../Properties/launchSettings.json | 29 ----- .../appsettings.Development.json | 8 -- .../appsettings.json | 9 -- ...pireWithMasstransit.ServiceDefaults.csproj | 23 ---- .../Extensions.cs | 108 ------------------ .../AspireWithMasstransit.sln | 36 ------ samples/AspireWithMasstransit/README.md | 41 ------- 15 files changed, 392 deletions(-) delete mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/AspireWithMasstransit.ApiService.csproj delete mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/HelloAspireEventConsumer.cs delete mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Program.cs delete mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Properties/launchSettings.json delete mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/appsettings.Development.json delete mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/appsettings.json delete mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/AspireWithMasstransit.AppHost.csproj delete mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/Program.cs delete mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/Properties/launchSettings.json delete mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/appsettings.Development.json delete mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/appsettings.json delete mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/AspireWithMasstransit.ServiceDefaults.csproj delete mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/Extensions.cs delete mode 100644 samples/AspireWithMasstransit/AspireWithMasstransit.sln delete mode 100644 samples/AspireWithMasstransit/README.md diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/AspireWithMasstransit.ApiService.csproj b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/AspireWithMasstransit.ApiService.csproj deleted file mode 100644 index 6099fd260..000000000 --- a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/AspireWithMasstransit.ApiService.csproj +++ /dev/null @@ -1,17 +0,0 @@ - - - - net8.0 - enable - enable - - - - - - - - - - - diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/HelloAspireEventConsumer.cs b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/HelloAspireEventConsumer.cs deleted file mode 100644 index a2e964293..000000000 --- a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/HelloAspireEventConsumer.cs +++ /dev/null @@ -1,14 +0,0 @@ -using MassTransit; - -namespace AspireWithMasstransit.ApiService; - -public class HelloAspireEventConsumer(ILogger logger) : IConsumer -{ - public Task Consume(ConsumeContext context) - { - logger.LogInformation("Received: {Message}", context.Message.Message); - return Task.CompletedTask; - } -} - -public record HelloAspireEvent(string Message); diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Program.cs b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Program.cs deleted file mode 100644 index f9f2dc9fa..000000000 --- a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Program.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using AspireWithMasstransit.ApiService; -using AspireWithMasstransit.ServiceDefaults; -using MassTransit; - -var builder = WebApplication.CreateBuilder(args); - -// Add service defaults & Aspire components. -builder.AddServiceDefaults(); - -var connectionString = builder.Configuration.GetConnectionString("messaging") - ?? throw new InvalidOperationException("Connection string for RabbitMQ instance 'messaging' was not found."); - -builder.Services.AddMassTransit(s => -{ - s.AddConsumers(typeof(Program).Assembly); - s.UsingRabbitMq((context, cfg) => - { - cfg.Host(new Uri(connectionString)); - cfg.ConfigureEndpoints(context); - }); -}); - -var app = builder.Build(); - -app.MapGet("/", async (IPublishEndpoint publishEndpoint) => -{ - await publishEndpoint.Publish(new HelloAspireEvent("Hello, .NET!")); - await publishEndpoint.Publish(new HelloAspireEvent("Hello, Aspire!")); - - return Results.Ok("ok"); -}); - -app.MapDefaultEndpoints(); - -app.Run(); diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Properties/launchSettings.json b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Properties/launchSettings.json deleted file mode 100644 index 8c2c58465..000000000 --- a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/Properties/launchSettings.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "$schema": "https://json.schemastore.org/launchsettings.json", - "profiles": { - "http": { - "commandName": "Project", - "dotnetRunMessages": true, - "launchBrowser": true, - "launchUrl": "", - "applicationUrl": "http://localhost:5421", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "https": { - "commandName": "Project", - "dotnetRunMessages": true, - "launchBrowser": true, - "launchUrl": "", - "applicationUrl": "https://localhost:7442;http://localhost:5421", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - } - } -} diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/appsettings.Development.json b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/appsettings.Development.json deleted file mode 100644 index 0c208ae91..000000000 --- a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/appsettings.Development.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - } -} diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/appsettings.json b/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/appsettings.json deleted file mode 100644 index 10f68b8c8..000000000 --- a/samples/AspireWithMasstransit/AspireWithMasstransit.ApiService/appsettings.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*" -} diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/AspireWithMasstransit.AppHost.csproj b/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/AspireWithMasstransit.AppHost.csproj deleted file mode 100644 index 8e78ba493..000000000 --- a/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/AspireWithMasstransit.AppHost.csproj +++ /dev/null @@ -1,21 +0,0 @@ - - - - Exe - net8.0 - enable - enable - true - 7CA514BA-715B-44D5-8C4A-D05E95F6E3BB - - - - - - - - - - - - diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/Program.cs b/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/Program.cs deleted file mode 100644 index f2ee998ec..000000000 --- a/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/Program.cs +++ /dev/null @@ -1,8 +0,0 @@ -var builder = DistributedApplication.CreateBuilder(args); - -var messaging = builder.AddRabbitMQ("messaging"); - -var apiService = builder.AddProject("apiservice") - .WithReference(messaging); - -builder.Build().Run(); diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/Properties/launchSettings.json b/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/Properties/launchSettings.json deleted file mode 100644 index cdf3d9dea..000000000 --- a/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/Properties/launchSettings.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "$schema": "https://json.schemastore.org/launchsettings.json", - "profiles": { - "https": { - "commandName": "Project", - "dotnetRunMessages": true, - "launchBrowser": true, - "applicationUrl": "https://localhost:17003;http://localhost:15085", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development", - "DOTNET_ENVIRONMENT": "Development", - "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21117", - "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22025" - } - }, - "http": { - "commandName": "Project", - "dotnetRunMessages": true, - "launchBrowser": true, - "applicationUrl": "http://localhost:15085", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development", - "DOTNET_ENVIRONMENT": "Development", - "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19120", - "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20161" - } - } - } -} diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/appsettings.Development.json b/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/appsettings.Development.json deleted file mode 100644 index 0c208ae91..000000000 --- a/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/appsettings.Development.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - } -} diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/appsettings.json b/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/appsettings.json deleted file mode 100644 index 31c092aa4..000000000 --- a/samples/AspireWithMasstransit/AspireWithMasstransit.AppHost/appsettings.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning", - "Aspire.Hosting.Dcp": "Warning" - } - } -} diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/AspireWithMasstransit.ServiceDefaults.csproj b/samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/AspireWithMasstransit.ServiceDefaults.csproj deleted file mode 100644 index 2641f3e19..000000000 --- a/samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/AspireWithMasstransit.ServiceDefaults.csproj +++ /dev/null @@ -1,23 +0,0 @@ - - - - net8.0 - enable - enable - true - - - - - - - - - - - - - - - - diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/Extensions.cs b/samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/Extensions.cs deleted file mode 100644 index 359fa7161..000000000 --- a/samples/AspireWithMasstransit/AspireWithMasstransit.ServiceDefaults/Extensions.cs +++ /dev/null @@ -1,108 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Diagnostics.HealthChecks; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Diagnostics.HealthChecks; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using OpenTelemetry.Logs; -using OpenTelemetry.Metrics; -using OpenTelemetry.Trace; - -namespace AspireWithMasstransit.ServiceDefaults; - -public static class Extensions -{ - public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder) - { - builder.ConfigureOpenTelemetry(); - - builder.AddDefaultHealthChecks(); - - builder.Services.AddServiceDiscovery(); - - builder.Services.ConfigureHttpClientDefaults(http => - { - // Turn on resilience by default - http.AddStandardResilienceHandler(); - - // Turn on service discovery by default - http.AddServiceDiscovery(); - }); - - return builder; - } - - public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder) - { - builder.Logging.AddOpenTelemetry(logging => - { - logging.IncludeFormattedMessage = true; - logging.IncludeScopes = true; - }); - - builder.Services.AddOpenTelemetry() - .WithMetrics(metrics => - { - metrics.AddAspNetCoreInstrumentation() - .AddHttpClientInstrumentation() - .AddRuntimeInstrumentation(); - }) - .WithTracing(tracing => - { - if (builder.Environment.IsDevelopment()) - { - // We want to view all traces in development - tracing.SetSampler(new AlwaysOnSampler()); - } - - tracing.AddAspNetCoreInstrumentation() - .AddHttpClientInstrumentation(); - }); - - builder.AddOpenTelemetryExporters(); - - return builder; - } - - private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostApplicationBuilder builder) - { - var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); - - if (useOtlpExporter) - { - builder.Services.Configure(logging => logging.AddOtlpExporter()); - builder.Services.ConfigureOpenTelemetryMeterProvider(metrics => metrics.AddOtlpExporter()); - builder.Services.ConfigureOpenTelemetryTracerProvider(tracing => tracing.AddOtlpExporter()); - } - - return builder; - } - - public static IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicationBuilder builder) - { - builder.Services.AddHealthChecks() - // Add a default liveness check to ensure app is responsive - .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); - - return builder; - } - - public static WebApplication MapDefaultEndpoints(this WebApplication app) - { - // Adding health checks endpoints to applications in non-development environments has security implications. - // See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments. - if (app.Environment.IsDevelopment()) - { - // All health checks must pass for app to be considered ready to accept traffic after starting - app.MapHealthChecks("/health"); - - // Only health checks tagged with the "live" tag must pass for app to be considered alive - app.MapHealthChecks("/alive", new HealthCheckOptions - { - Predicate = r => r.Tags.Contains("live") - }); - } - - return app; - } -} diff --git a/samples/AspireWithMasstransit/AspireWithMasstransit.sln b/samples/AspireWithMasstransit/AspireWithMasstransit.sln deleted file mode 100644 index d9d37ea33..000000000 --- a/samples/AspireWithMasstransit/AspireWithMasstransit.sln +++ /dev/null @@ -1,36 +0,0 @@ -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.8.0.0 -MinimumVisualStudioVersion = 17.8.0.0 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspireWithMasstransit.AppHost", "AspireWithMasstransit.AppHost\AspireWithMasstransit.AppHost.csproj", "{942359AE-4183-4113-B49C-978C7CA24C7E}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspireWithMasstransit.ServiceDefaults", "AspireWithMasstransit.ServiceDefaults\AspireWithMasstransit.ServiceDefaults.csproj", "{40A4FBAC-BDD1-467E-96B9-45DC6FC33ACF}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspireWithMasstransit.ApiService", "AspireWithMasstransit.ApiService\AspireWithMasstransit.ApiService.csproj", "{AD70E94E-D614-4B50-8C08-F620A4E21FA2}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {942359AE-4183-4113-B49C-978C7CA24C7E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {942359AE-4183-4113-B49C-978C7CA24C7E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {942359AE-4183-4113-B49C-978C7CA24C7E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {942359AE-4183-4113-B49C-978C7CA24C7E}.Release|Any CPU.Build.0 = Release|Any CPU - {40A4FBAC-BDD1-467E-96B9-45DC6FC33ACF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {40A4FBAC-BDD1-467E-96B9-45DC6FC33ACF}.Debug|Any CPU.Build.0 = Debug|Any CPU - {40A4FBAC-BDD1-467E-96B9-45DC6FC33ACF}.Release|Any CPU.ActiveCfg = Release|Any CPU - {40A4FBAC-BDD1-467E-96B9-45DC6FC33ACF}.Release|Any CPU.Build.0 = Release|Any CPU - {AD70E94E-D614-4B50-8C08-F620A4E21FA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {AD70E94E-D614-4B50-8C08-F620A4E21FA2}.Debug|Any CPU.Build.0 = Debug|Any CPU - {AD70E94E-D614-4B50-8C08-F620A4E21FA2}.Release|Any CPU.ActiveCfg = Release|Any CPU - {AD70E94E-D614-4B50-8C08-F620A4E21FA2}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {6C5D22D8-6EF2-43FF-A7FA-C8241CA43684} - EndGlobalSection -EndGlobal diff --git a/samples/AspireWithMasstransit/README.md b/samples/AspireWithMasstransit/README.md deleted file mode 100644 index 4a4f82149..000000000 --- a/samples/AspireWithMasstransit/README.md +++ /dev/null @@ -1,41 +0,0 @@ ---- -languages: -- csharp -products: -- dotnet -- dotnet-aspire -page_type: sample -name: ".NET Aspire Masstransit sample" -urlFragment: "aspire-dapr" -description: "An example of how to integrate Masstransit into a .NET Aspire app." ---- - -# Integrating Masstransit into a .NET Aspire application - -This sample demonstrates an approach for integrating [Masstransit](https://masstransit.io/) into a .NET Aspire application. - -## Pre-requisites - -- [.NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0) -- [Docker Desktop](https://www.docker.com/products/docker-desktop/) -- **Optional** [Visual Studio 2022 17.9 Preview](https://visualstudio.microsoft.com/vs/preview/) - -## Running the app - -If using Visual Studio, open the solution file `AspireWithMasstransit.sln` and launch/debug the `AspireWithMasstransit.AppHost` project. - -If using the .NET CLI, run `dotnet run` from the `AspireWithMasstransit.AppHost` directory. - -## Experiencing the app - -Once the app is running, the .NET Aspire dashboard will launch in your browser: - -Navigate to https://localhost:7442 in the browser to publish some messages. - -After that check the log. Consumer should received and consume the messages. - -``` -2024-04-18T20:37:35.2219340 info: AspireWithMasstransit.ApiService.HelloAspireEventConsumer[0] - Received: Hello, .NET! -2024-04-18T20:37:35.2219350 info: AspireWithMasstransit.ApiService.HelloAspireEventConsumer[0] - Received: Hello, Aspire! \ No newline at end of file From 1f619b5f7f233a06381965be1e0aeca82f165dae Mon Sep 17 00:00:00 2001 From: Bao To Quoc Date: Fri, 8 Nov 2024 20:45:42 +0700 Subject: [PATCH 7/9] upgrade to .NET 9 and fix some reviews --- .../AspireWithMassTransit.ApiService.csproj | 17 +++ .../HelloAspireEventConsumer.cs | 21 ++++ .../Program.cs | 41 +++++++ .../Properties/launchSettings.json | 25 ++++ .../appsettings.Development.json | 8 ++ .../appsettings.json | 9 ++ .../AspireWithMassTransit.AppHost.csproj | 23 ++++ .../AspireWithMassTransit.AppHost/Program.cs | 11 ++ .../Properties/launchSettings.json | 29 +++++ .../appsettings.Development.json | 8 ++ .../appsettings.json | 9 ++ ...pireWithMassTransit.ServiceDefaults.csproj | 22 ++++ .../Extensions.cs | 111 ++++++++++++++++++ .../AspireWithMassTransit.sln | 28 +++++ samples/AspireWithMassTransit/README.md | 41 +++++++ 15 files changed, 403 insertions(+) create mode 100644 samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/AspireWithMassTransit.ApiService.csproj create mode 100644 samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/HelloAspireEventConsumer.cs create mode 100644 samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/Program.cs create mode 100644 samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/Properties/launchSettings.json create mode 100644 samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/appsettings.Development.json create mode 100644 samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/appsettings.json create mode 100644 samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/AspireWithMassTransit.AppHost.csproj create mode 100644 samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/Program.cs create mode 100644 samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/Properties/launchSettings.json create mode 100644 samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/appsettings.Development.json create mode 100644 samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/appsettings.json create mode 100644 samples/AspireWithMassTransit/AspireWithMassTransit.ServiceDefaults/AspireWithMassTransit.ServiceDefaults.csproj create mode 100644 samples/AspireWithMassTransit/AspireWithMassTransit.ServiceDefaults/Extensions.cs create mode 100644 samples/AspireWithMassTransit/AspireWithMassTransit.sln create mode 100644 samples/AspireWithMassTransit/README.md diff --git a/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/AspireWithMassTransit.ApiService.csproj b/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/AspireWithMassTransit.ApiService.csproj new file mode 100644 index 000000000..824221d8e --- /dev/null +++ b/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/AspireWithMassTransit.ApiService.csproj @@ -0,0 +1,17 @@ + + + + net9.0 + enable + enable + + + + + + + + + + + diff --git a/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/HelloAspireEventConsumer.cs b/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/HelloAspireEventConsumer.cs new file mode 100644 index 000000000..261bbafec --- /dev/null +++ b/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/HelloAspireEventConsumer.cs @@ -0,0 +1,21 @@ +using MassTransit; + +namespace AspireWithMassTransit.ApiService; + +public class HelloAspireEventConsumer : IConsumer +{ + private readonly ILogger _logger; + + public HelloAspireEventConsumer(ILogger logger) + { + _logger = logger; + } + + public Task Consume(ConsumeContext context) + { + _logger.LogInformation("Received: {Message}", context.Message.Message); + return Task.CompletedTask; + } +} + +public record HelloAspireEvent(string Message); diff --git a/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/Program.cs b/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/Program.cs new file mode 100644 index 000000000..e484dcf19 --- /dev/null +++ b/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/Program.cs @@ -0,0 +1,41 @@ +using AspireWithMassTransit.ApiService; +using MassTransit; + +var builder = WebApplication.CreateBuilder(args); + +// Add service defaults & Aspire client integrations. +builder.AddServiceDefaults(); + +// Add services to the container. +builder.Services.AddProblemDetails(); + +builder.Services.AddMassTransit(s => +{ + s.AddConsumers(typeof(Program).Assembly); + s.UsingRabbitMq((context, cfg) => + { + var configuration = context.GetRequiredService(); + var host = configuration.GetConnectionString("messaging"); + + cfg.Host(host); + cfg.ConfigureEndpoints(context); + }); +}); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +app.UseExceptionHandler(); + +app.MapGet("/", async (IPublishEndpoint publishEndpoint) => +{ + await publishEndpoint.Publish(new HelloAspireEvent("Hello, .NET!")); + await publishEndpoint.Publish(new HelloAspireEvent("Hello, Aspire!")); + + return Results.Ok("ok"); +}); + +app.MapDefaultEndpoints(); + +app.Run(); + diff --git a/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/Properties/launchSettings.json b/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/Properties/launchSettings.json new file mode 100644 index 000000000..f3b8939a5 --- /dev/null +++ b/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/Properties/launchSettings.json @@ -0,0 +1,25 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "launchUrl": "", + "applicationUrl": "http://localhost:5575", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "launchUrl": "", + "applicationUrl": "https://localhost:7381;http://localhost:5575", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/appsettings.Development.json b/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/appsettings.Development.json new file mode 100644 index 000000000..0c208ae91 --- /dev/null +++ b/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/appsettings.json b/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/appsettings.json new file mode 100644 index 000000000..10f68b8c8 --- /dev/null +++ b/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/AspireWithMassTransit.AppHost.csproj b/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/AspireWithMassTransit.AppHost.csproj new file mode 100644 index 000000000..b2935f7bb --- /dev/null +++ b/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/AspireWithMassTransit.AppHost.csproj @@ -0,0 +1,23 @@ + + + + + + Exe + net9.0 + enable + enable + true + 7d4e2632-ca72-4db3-8665-001fb368e89b + + + + + + + + + + + + diff --git a/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/Program.cs b/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/Program.cs new file mode 100644 index 000000000..908028131 --- /dev/null +++ b/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/Program.cs @@ -0,0 +1,11 @@ +var builder = DistributedApplication.CreateBuilder(args); + +var messaging = builder.AddRabbitMQ("messaging") + .WithLifetime(ContainerLifetime.Persistent) + .WithManagementPlugin() + .WithDataVolume(); + +var apiService = builder.AddProject("apiservice") + .WithReference(messaging); + +builder.Build().Run(); diff --git a/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/Properties/launchSettings.json b/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/Properties/launchSettings.json new file mode 100644 index 000000000..ef796939e --- /dev/null +++ b/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/Properties/launchSettings.json @@ -0,0 +1,29 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17131;http://localhost:15194", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21047", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22126" + } + }, + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15194", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19101", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20047" + } + } + } +} diff --git a/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/appsettings.Development.json b/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/appsettings.Development.json new file mode 100644 index 000000000..0c208ae91 --- /dev/null +++ b/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/appsettings.json b/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/appsettings.json new file mode 100644 index 000000000..31c092aa4 --- /dev/null +++ b/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + } +} diff --git a/samples/AspireWithMassTransit/AspireWithMassTransit.ServiceDefaults/AspireWithMassTransit.ServiceDefaults.csproj b/samples/AspireWithMassTransit/AspireWithMassTransit.ServiceDefaults/AspireWithMassTransit.ServiceDefaults.csproj new file mode 100644 index 000000000..26a119ea3 --- /dev/null +++ b/samples/AspireWithMassTransit/AspireWithMassTransit.ServiceDefaults/AspireWithMassTransit.ServiceDefaults.csproj @@ -0,0 +1,22 @@ + + + + net9.0 + enable + enable + true + + + + + + + + + + + + + + + diff --git a/samples/AspireWithMassTransit/AspireWithMassTransit.ServiceDefaults/Extensions.cs b/samples/AspireWithMassTransit/AspireWithMassTransit.ServiceDefaults/Extensions.cs new file mode 100644 index 000000000..5f8a36a1b --- /dev/null +++ b/samples/AspireWithMassTransit/AspireWithMassTransit.ServiceDefaults/Extensions.cs @@ -0,0 +1,111 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.ServiceDiscovery; +using OpenTelemetry; +using OpenTelemetry.Metrics; +using OpenTelemetry.Trace; + +namespace Microsoft.Extensions.Hosting; + +// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry. +// This project should be referenced by each service project in your solution. +// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults +public static class Extensions +{ + public static TBuilder AddServiceDefaults(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.ConfigureOpenTelemetry(); + + builder.AddDefaultHealthChecks(); + + builder.Services.AddServiceDiscovery(); + + builder.Services.ConfigureHttpClientDefaults(http => + { + // Turn on resilience by default + http.AddStandardResilienceHandler(); + + // Turn on service discovery by default + http.AddServiceDiscovery(); + }); + + // Uncomment the following to restrict the allowed schemes for service discovery. + // builder.Services.Configure(options => + // { + // options.AllowedSchemes = ["https"]; + // }); + + return builder; + } + + public static TBuilder ConfigureOpenTelemetry(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Logging.AddOpenTelemetry(logging => + { + logging.IncludeFormattedMessage = true; + logging.IncludeScopes = true; + }); + + builder.Services.AddOpenTelemetry() + .WithMetrics(metrics => + { + metrics.AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddMeter("MassTransit") + .AddRuntimeInstrumentation(); + }) + .WithTracing(tracing => + { + tracing.AddAspNetCoreInstrumentation() + .AddSource("MassTransit") + .AddHttpClientInstrumentation(); + }); + + builder.AddOpenTelemetryExporters(); + + return builder; + } + + private static TBuilder AddOpenTelemetryExporters(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); + + if (useOtlpExporter) + { + builder.Services.AddOpenTelemetry().UseOtlpExporter(); + } + + return builder; + } + + public static TBuilder AddDefaultHealthChecks(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Services.AddHealthChecks() + // Add a default liveness check to ensure app is responsive + .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); + + return builder; + } + + public static WebApplication MapDefaultEndpoints(this WebApplication app) + { + // Adding health checks endpoints to applications in non-development environments has security implications. + // See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments. + if (app.Environment.IsDevelopment()) + { + // All health checks must pass for app to be considered ready to accept traffic after starting + app.MapHealthChecks("/health"); + + // Only health checks tagged with the "live" tag must pass for app to be considered alive + app.MapHealthChecks("/alive", new HealthCheckOptions + { + Predicate = r => r.Tags.Contains("live") + }); + } + + return app; + } +} diff --git a/samples/AspireWithMassTransit/AspireWithMassTransit.sln b/samples/AspireWithMassTransit/AspireWithMassTransit.sln new file mode 100644 index 000000000..2266d3c01 --- /dev/null +++ b/samples/AspireWithMassTransit/AspireWithMassTransit.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspireWithMassTransit.ApiService", "AspireWithMassTransit.ApiService\AspireWithMassTransit.ApiService.csproj", "{0B88A0EE-6435-4A30-9131-165118F3053C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspireWithMassTransit.AppHost", "AspireWithMassTransit.AppHost\AspireWithMassTransit.AppHost.csproj", "{665B7823-0444-4F4D-9C41-D43CD50DAEF3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspireWithMassTransit.ServiceDefaults", "AspireWithMassTransit.ServiceDefaults\AspireWithMassTransit.ServiceDefaults.csproj", "{77A8E695-50D9-48D4-8ED3-1F458BB31A00}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0B88A0EE-6435-4A30-9131-165118F3053C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0B88A0EE-6435-4A30-9131-165118F3053C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0B88A0EE-6435-4A30-9131-165118F3053C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0B88A0EE-6435-4A30-9131-165118F3053C}.Release|Any CPU.Build.0 = Release|Any CPU + {665B7823-0444-4F4D-9C41-D43CD50DAEF3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {665B7823-0444-4F4D-9C41-D43CD50DAEF3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {665B7823-0444-4F4D-9C41-D43CD50DAEF3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {665B7823-0444-4F4D-9C41-D43CD50DAEF3}.Release|Any CPU.Build.0 = Release|Any CPU + {77A8E695-50D9-48D4-8ED3-1F458BB31A00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {77A8E695-50D9-48D4-8ED3-1F458BB31A00}.Debug|Any CPU.Build.0 = Debug|Any CPU + {77A8E695-50D9-48D4-8ED3-1F458BB31A00}.Release|Any CPU.ActiveCfg = Release|Any CPU + {77A8E695-50D9-48D4-8ED3-1F458BB31A00}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/samples/AspireWithMassTransit/README.md b/samples/AspireWithMassTransit/README.md new file mode 100644 index 000000000..653cafc2f --- /dev/null +++ b/samples/AspireWithMassTransit/README.md @@ -0,0 +1,41 @@ +--- +languages: +- csharp +products: +- dotnet +- dotnet-aspire +page_type: sample +name: ".NET Aspire MassTransit sample" +urlFragment: "aspire-dapr" +description: "An example of how to integrate MassTransit into a .NET Aspire app." +--- + +# Integrating MassTransit into a .NET Aspire application + +This sample demonstrates an approach for integrating [MassTransit](https://MassTransit.io/) into a .NET Aspire application. + +## Pre-requisites + +- [.NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0) +- [Docker Desktop](https://www.docker.com/products/docker-desktop/) +- **Optional** [Visual Studio 2022 17.9 Preview](https://visualstudio.microsoft.com/vs/preview/) + +## Running the app + +If using Visual Studio, open the solution file `AspireWithMassTransit.sln` and launch/debug the `AspireWithMassTransit.AppHost` project. + +If using the .NET CLI, run `dotnet run` from the `AspireWithMassTransit.AppHost` directory. + +## Experiencing the app + +Once the app is running, the .NET Aspire dashboard will launch in your browser: + +Navigate to https://localhost:17131 in the browser to publish some messages. + +After that check the log. Consumer should received and consume the messages. + +``` +2024-04-18T20:37:35.2219340 info: AspireWithMassTransit.ApiService.HelloAspireEventConsumer[0] + Received: Hello, .NET! +2024-04-18T20:37:35.2219350 info: AspireWithMassTransit.ApiService.HelloAspireEventConsumer[0] + Received: Hello, Aspire! \ No newline at end of file From d8e4dbbad0fd90547aee85704ac88547d6f70a9e Mon Sep 17 00:00:00 2001 From: Bao To Quoc Date: Fri, 8 Nov 2024 21:08:33 +0700 Subject: [PATCH 8/9] downgrade to .NET 8 but using Aspire 9 --- .../AspireWithMassTransit.ApiService.csproj | 2 +- .../AspireWithMassTransit.AppHost.csproj | 2 +- .../AspireWithMassTransit.ServiceDefaults.csproj | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/AspireWithMassTransit.ApiService.csproj b/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/AspireWithMassTransit.ApiService.csproj index 824221d8e..fbcb3bfce 100644 --- a/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/AspireWithMassTransit.ApiService.csproj +++ b/samples/AspireWithMassTransit/AspireWithMassTransit.ApiService/AspireWithMassTransit.ApiService.csproj @@ -1,7 +1,7 @@ - net9.0 + net8.0 enable enable diff --git a/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/AspireWithMassTransit.AppHost.csproj b/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/AspireWithMassTransit.AppHost.csproj index b2935f7bb..ffe961f43 100644 --- a/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/AspireWithMassTransit.AppHost.csproj +++ b/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/AspireWithMassTransit.AppHost.csproj @@ -4,7 +4,7 @@ Exe - net9.0 + net8.0 enable enable true diff --git a/samples/AspireWithMassTransit/AspireWithMassTransit.ServiceDefaults/AspireWithMassTransit.ServiceDefaults.csproj b/samples/AspireWithMassTransit/AspireWithMassTransit.ServiceDefaults/AspireWithMassTransit.ServiceDefaults.csproj index 26a119ea3..0033fc0ed 100644 --- a/samples/AspireWithMassTransit/AspireWithMassTransit.ServiceDefaults/AspireWithMassTransit.ServiceDefaults.csproj +++ b/samples/AspireWithMassTransit/AspireWithMassTransit.ServiceDefaults/AspireWithMassTransit.ServiceDefaults.csproj @@ -1,7 +1,7 @@ - net9.0 + net8.0 enable enable true From 0b1813fbb62675bed00fbd6dae7c5f336bf310d7 Mon Sep 17 00:00:00 2001 From: Bao To Quoc Date: Sat, 30 Nov 2024 16:20:17 +0700 Subject: [PATCH 9/9] Update to Aspire 9 GA --- .../AspireWithMassTransit.AppHost.csproj | 6 +++--- .../AspireWithMassTransit.ServiceDefaults.csproj | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/AspireWithMassTransit.AppHost.csproj b/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/AspireWithMassTransit.AppHost.csproj index ffe961f43..3974bb180 100644 --- a/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/AspireWithMassTransit.AppHost.csproj +++ b/samples/AspireWithMassTransit/AspireWithMassTransit.AppHost/AspireWithMassTransit.AppHost.csproj @@ -1,6 +1,6 @@ - + Exe @@ -16,8 +16,8 @@ - - + + diff --git a/samples/AspireWithMassTransit/AspireWithMassTransit.ServiceDefaults/AspireWithMassTransit.ServiceDefaults.csproj b/samples/AspireWithMassTransit/AspireWithMassTransit.ServiceDefaults/AspireWithMassTransit.ServiceDefaults.csproj index 0033fc0ed..4c0f8a211 100644 --- a/samples/AspireWithMassTransit/AspireWithMassTransit.ServiceDefaults/AspireWithMassTransit.ServiceDefaults.csproj +++ b/samples/AspireWithMassTransit/AspireWithMassTransit.ServiceDefaults/AspireWithMassTransit.ServiceDefaults.csproj @@ -11,7 +11,7 @@ - +