Skip to content

Commit e439e46

Browse files
Copilotjongalloway
andcommitted
Changes before error encountered
Co-authored-by: jongalloway <[email protected]>
1 parent 37c66b5 commit e439e46

File tree

11 files changed

+675
-7
lines changed

11 files changed

+675
-7
lines changed

demo/Program.cs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
using Microsoft.AspNetCore.Builder;
22
using NLWebNet;
3+
using NLWebNet.Extensions;
34
using NLWebNet.Endpoints;
45

56
var builder = WebApplication.CreateBuilder(args);
67

8+
// Detect if running in Aspire and configure accordingly
9+
var isAspireEnabled = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DOTNET_ASPIRE_URLS")) ||
10+
!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT"));
11+
712
// Add services to the container.
813
builder.Services.AddRazorComponents()
914
.AddInteractiveServerComponents();
@@ -28,13 +33,34 @@
2833
});
2934
});
3035

31-
// Add NLWebNet services
32-
builder.Services.AddNLWebNet(options =>
36+
// Add NLWebNet services - use Aspire-optimized version if available
37+
if (isAspireEnabled)
3338
{
34-
// Configure NLWebNet options here
35-
options.DefaultMode = NLWebNet.Models.QueryMode.List;
36-
options.EnableStreaming = true;
37-
});
39+
builder.Services.AddNLWebNetForAspire(options =>
40+
{
41+
// Configure NLWebNet options here
42+
options.DefaultMode = NLWebNet.Models.QueryMode.List;
43+
options.EnableStreaming = true;
44+
// Aspire environments typically handle more load
45+
options.RateLimiting.RequestsPerWindow = 1000;
46+
options.RateLimiting.WindowSizeInMinutes = 1;
47+
});
48+
}
49+
else
50+
{
51+
builder.Services.AddNLWebNet(options =>
52+
{
53+
// Configure NLWebNet options here
54+
options.DefaultMode = NLWebNet.Models.QueryMode.List;
55+
options.EnableStreaming = true;
56+
});
57+
58+
// Add OpenTelemetry for non-Aspire environments (development/testing)
59+
builder.Services.AddNLWebNetOpenTelemetry("NLWebNet.Demo", "1.0.0", otlBuilder =>
60+
{
61+
otlBuilder.AddConsoleExporters(); // Simple console output for development
62+
});
63+
}
3864

3965
// Add OpenAPI for API documentation
4066
builder.Services.AddOpenApi();
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using Aspire.Hosting;
2+
3+
namespace NLWebNet.Extensions;
4+
5+
/// <summary>
6+
/// Extension methods for adding NLWebNet to Aspire host projects
7+
/// Note: This file should only be used in projects that reference Aspire.Hosting packages
8+
/// </summary>
9+
public static class AspireHostingExtensions
10+
{
11+
/// <summary>
12+
/// Adds an NLWebNet application to the Aspire host
13+
/// </summary>
14+
/// <param name="builder">The distributed application builder</param>
15+
/// <param name="name">The name of the application</param>
16+
/// <returns>A resource builder for the NLWebNet application</returns>
17+
public static IResourceBuilder<ProjectResource> AddNLWebNetApp(
18+
this IDistributedApplicationBuilder builder,
19+
string name)
20+
{
21+
return builder.AddProject<Projects.NLWebNet_Demo>(name)
22+
.WithEnvironment("ASPNETCORE_ENVIRONMENT", builder.Environment.EnvironmentName)
23+
.WithEnvironment("OTEL_SERVICE_NAME", name)
24+
.WithEnvironment("OTEL_SERVICE_VERSION", "1.0.0");
25+
}
26+
27+
/// <summary>
28+
/// Adds an NLWebNet application with custom configuration to the Aspire host
29+
/// </summary>
30+
/// <param name="builder">The distributed application builder</param>
31+
/// <param name="name">The name of the application</param>
32+
/// <param name="configure">Configuration callback for the resource</param>
33+
/// <returns>A resource builder for the NLWebNet application</returns>
34+
public static IResourceBuilder<ProjectResource> AddNLWebNetApp(
35+
this IDistributedApplicationBuilder builder,
36+
string name,
37+
Action<IResourceBuilder<ProjectResource>> configure)
38+
{
39+
var resource = builder.AddNLWebNetApp(name);
40+
configure(resource);
41+
return resource;
42+
}
43+
44+
/// <summary>
45+
/// Adds an NLWebNet application with external data backend reference
46+
/// </summary>
47+
/// <param name="builder">The distributed application builder</param>
48+
/// <param name="name">The name of the application</param>
49+
/// <param name="dataBackend">The data backend resource to reference</param>
50+
/// <returns>A resource builder for the NLWebNet application</returns>
51+
public static IResourceBuilder<ProjectResource> AddNLWebNetAppWithDataBackend(
52+
this IDistributedApplicationBuilder builder,
53+
string name,
54+
IResourceBuilder<IResourceWithConnectionString> dataBackend)
55+
{
56+
return builder.AddNLWebNetApp(name)
57+
.WithReference(dataBackend)
58+
.WithEnvironment("NLWebNet__DataBackend__ConnectionString", dataBackend.Resource.GetConnectionString());
59+
}
60+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<IsAspireHost>true</IsAspireHost>
9+
<UserSecretsId>aspire-nlwebnet-host-12345</UserSecretsId>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.0.6" />
14+
<PackageReference Include="Aspire.Hosting.PostgreSQL" Version="9.0.6" />
15+
<PackageReference Include="Aspire.Hosting.Redis" Version="9.0.6" />
16+
<PackageReference Include="Aspire.Hosting.Azure.PostgreSQL" Version="9.0.6" />
17+
<PackageReference Include="Aspire.Hosting.Azure.Redis" Version="9.0.6" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\..\src\NLWebNet\NLWebNet.csproj" />
22+
<ProjectReference Include="..\..\demo\NLWebNet.Demo.csproj" />
23+
</ItemGroup>
24+
25+
</Project>

samples/AspireHost/Program.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using NLWebNet.Extensions;
2+
3+
var builder = DistributedApplication.CreateBuilder(args);
4+
5+
// Add external dependencies (optional - could be databases, message queues, etc.)
6+
// var postgres = builder.AddPostgres("postgres")
7+
// .WithEnvironment("POSTGRES_DB", "nlwebnet")
8+
// .PublishAsAzurePostgresFlexibleServer();
9+
10+
// var redis = builder.AddRedis("redis")
11+
// .PublishAsAzureRedis();
12+
13+
// Add the NLWebNet demo application
14+
var nlwebapp = builder.AddNLWebNetApp("nlwebnet-api")
15+
.WithEnvironment("ASPNETCORE_ENVIRONMENT", builder.Environment.EnvironmentName)
16+
.WithEnvironment("NLWebNet__RateLimiting__RequestsPerWindow", "1000")
17+
.WithEnvironment("NLWebNet__RateLimiting__WindowSizeInMinutes", "1")
18+
.WithEnvironment("NLWebNet__EnableStreaming", "true")
19+
.WithReplicas(2); // Scale out for load testing
20+
21+
// Optional: Add with database dependency
22+
// var nlwebapp = builder.AddNLWebNetAppWithDataBackend("nlwebnet-api", postgres);
23+
24+
// Add a simple frontend (if we had one)
25+
// var frontend = builder.AddProject<Projects.NLWebNet_Frontend>("frontend")
26+
// .WithReference(nlwebapp);
27+
28+
var app = builder.Build();
29+
30+
await app.RunAsync();
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Hosting;
3+
using Microsoft.Extensions.Logging;
4+
using Microsoft.Extensions.ServiceDiscovery;
5+
using NLWebNet.Models;
6+
7+
namespace NLWebNet.Extensions;
8+
9+
/// <summary>
10+
/// Extension methods for .NET Aspire integration with NLWebNet
11+
/// </summary>
12+
public static class AspireExtensions
13+
{
14+
/// <summary>
15+
/// Adds NLWebNet services configured for .NET Aspire environments
16+
/// </summary>
17+
/// <param name="services">The service collection</param>
18+
/// <param name="configureOptions">Optional configuration callback for NLWebNet options</param>
19+
/// <returns>The service collection for chaining</returns>
20+
public static IServiceCollection AddNLWebNetForAspire(
21+
this IServiceCollection services,
22+
Action<NLWebOptions>? configureOptions = null)
23+
{
24+
// Add standard NLWebNet services
25+
services.AddNLWebNet(configureOptions);
26+
27+
// Add service discovery for Aspire
28+
services.AddServiceDiscovery();
29+
30+
// Configure OpenTelemetry for Aspire integration
31+
services.AddNLWebNetOpenTelemetry(builder => builder.ConfigureForAspire());
32+
33+
// Add health checks optimized for Aspire
34+
services.AddHealthChecks()
35+
.AddCheck("aspire-ready", () => Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckResult.Healthy("Ready for Aspire"));
36+
37+
return services;
38+
}
39+
40+
/// <summary>
41+
/// Adds default service configuration suitable for Aspire-hosted applications
42+
/// </summary>
43+
/// <param name="builder">The host application builder</param>
44+
/// <param name="configureOptions">Optional configuration callback for NLWebNet options</param>
45+
/// <returns>The host application builder for chaining</returns>
46+
public static IHostApplicationBuilder AddNLWebNetDefaults(
47+
this IHostApplicationBuilder builder,
48+
Action<NLWebOptions>? configureOptions = null)
49+
{
50+
// Add NLWebNet services configured for Aspire
51+
builder.Services.AddNLWebNetForAspire(configureOptions);
52+
53+
// Configure logging for structured output
54+
builder.Logging.AddJsonConsole(options =>
55+
{
56+
options.IncludeScopes = true;
57+
options.TimestampFormat = "yyyy-MM-ddTHH:mm:ss.fffZ";
58+
options.UseUtcTimestamp = true;
59+
});
60+
61+
return builder;
62+
}
63+
}

0 commit comments

Comments
 (0)