Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\AspireWithMassTransit.ServiceDefaults\AspireWithMassTransit.ServiceDefaults.csproj"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="MassTransit.RabbitMQ" Version="8.3.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using MassTransit;

namespace AspireWithMassTransit.ApiService;

public class HelloAspireEventConsumer : IConsumer<HelloAspireEvent>
{
private readonly ILogger<HelloAspireEventConsumer> _logger;

public HelloAspireEventConsumer(ILogger<HelloAspireEventConsumer> logger)
{
_logger = logger;
}

public Task Consume(ConsumeContext<HelloAspireEvent> context)
{
_logger.LogInformation("Received: {Message}", context.Message.Message);
return Task.CompletedTask;
}
}

public record HelloAspireEvent(string Message);
Original file line number Diff line number Diff line change
@@ -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<IConfiguration>();
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();

Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<Sdk Name="Aspire.AppHost.Sdk" Version="9.0.0"/>

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
<UserSecretsId>7d4e2632-ca72-4db3-8665-001fb368e89b</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\AspireWithMassTransit.ApiService\AspireWithMassTransit.ApiService.csproj"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.0.0"/>
<PackageReference Include="Aspire.Hosting.RabbitMQ" Version="9.0.0"/>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var builder = DistributedApplication.CreateBuilder(args);

var messaging = builder.AddRabbitMQ("messaging")
.WithLifetime(ContainerLifetime.Persistent)
.WithManagementPlugin()
.WithDataVolume();

var apiService = builder.AddProject<Projects.AspireWithMassTransit_ApiService>("apiservice")
.WithReference(messaging);

builder.Build().Run();
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireSharedProject>true</IsAspireSharedProject>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App"/>

<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.10.0"/>
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="9.0.0"/>
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0"/>
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0"/>
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0"/>
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0"/>
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0"/>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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<TBuilder>(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<ServiceDiscoveryOptions>(options =>
// {
// options.AllowedSchemes = ["https"];
// });

return builder;
}

public static TBuilder ConfigureOpenTelemetry<TBuilder>(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<TBuilder>(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<TBuilder>(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;
}
}
28 changes: 28 additions & 0 deletions samples/AspireWithMassTransit/AspireWithMassTransit.sln
Original file line number Diff line number Diff line change
@@ -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
Loading