Skip to content

Commit 68c1347

Browse files
committed
Add aspire helpers to configure remote app connections
1 parent b7a60a8 commit 68c1347

File tree

34 files changed

+382
-188
lines changed

34 files changed

+382
-188
lines changed

Microsoft.AspNetCore.SystemWebAdapters.sln

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AuthWindowsCore", "samples\
112112
EndProject
113113
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples.ServiceDefaults", "samples\ServiceDefaults\Samples.ServiceDefaults.csproj", "{D08809BC-D4C3-F7C8-1C00-151CA8C6E504}"
114114
EndProject
115+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNetCore.SystemWebAdapters.Aspire", "src\Microsoft.AspNetCore.SystemWebAdapters.Aspire\Microsoft.AspNetCore.SystemWebAdapters.Aspire.csproj", "{65A176C7-1BF6-4653-925B-13C09F282ABA}"
116+
EndProject
115117
Global
116118
GlobalSection(SolutionConfigurationPlatforms) = preSolution
117119
Debug|Any CPU = Debug|Any CPU
@@ -246,6 +248,14 @@ Global
246248
{D08809BC-D4C3-F7C8-1C00-151CA8C6E504}.Debug|Any CPU.Build.0 = Debug|Any CPU
247249
{D08809BC-D4C3-F7C8-1C00-151CA8C6E504}.Release|Any CPU.ActiveCfg = Release|Any CPU
248250
{D08809BC-D4C3-F7C8-1C00-151CA8C6E504}.Release|Any CPU.Build.0 = Release|Any CPU
251+
{65A176C7-1BF6-4653-925B-13C09F282ABA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
252+
{65A176C7-1BF6-4653-925B-13C09F282ABA}.Debug|Any CPU.Build.0 = Debug|Any CPU
253+
{65A176C7-1BF6-4653-925B-13C09F282ABA}.Release|Any CPU.ActiveCfg = Release|Any CPU
254+
{65A176C7-1BF6-4653-925B-13C09F282ABA}.Release|Any CPU.Build.0 = Release|Any CPU
255+
{D08809BC-D4C3-F7C8-1C00-151CA8C6E504}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
256+
{D08809BC-D4C3-F7C8-1C00-151CA8C6E504}.Debug|Any CPU.Build.0 = Debug|Any CPU
257+
{D08809BC-D4C3-F7C8-1C00-151CA8C6E504}.Release|Any CPU.ActiveCfg = Release|Any CPU
258+
{D08809BC-D4C3-F7C8-1C00-151CA8C6E504}.Release|Any CPU.Build.0 = Release|Any CPU
249259
EndGlobalSection
250260
GlobalSection(SolutionProperties) = preSolution
251261
HideSolutionNode = FALSE
@@ -290,6 +300,8 @@ Global
290300
{AC29CA1D-A850-4C29-B27D-C23BAF3D664C} = {95915611-30BF-4AFF-AE41-5CDC6F57DCF7}
291301
{64B09008-C9CB-BAEC-2AE1-616E6BE9880B} = {AC29CA1D-A850-4C29-B27D-C23BAF3D664C}
292302
{D08809BC-D4C3-F7C8-1C00-151CA8C6E504} = {95915611-30BF-4AFF-AE41-5CDC6F57DCF7}
303+
{65A176C7-1BF6-4653-925B-13C09F282ABA} = {F9DB9323-C919-49E8-8F96-B923D2F42E60}
304+
{D08809BC-D4C3-F7C8-1C00-151CA8C6E504} = {95915611-30BF-4AFF-AE41-5CDC6F57DCF7}
293305
EndGlobalSection
294306
GlobalSection(ExtensibilityGlobals) = postSolution
295307
SolutionGuid = {DABA3C65-9D74-4EB6-9B1C-730328710EAD}

samples/AuthRemoteFormsAuth/AuthRemoteFormsAuthAppHost/AuthRemoteFormsAuthAppHost.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
</ItemGroup>
1717

1818
<ItemGroup>
19+
<ProjectReference Include="..\..\..\src\Microsoft.AspNetCore.SystemWebAdapters.Aspire\Microsoft.AspNetCore.SystemWebAdapters.Aspire.csproj" IsAspireProjectResource="false" />
1920
<ProjectReference Include="..\AuthRemoteFormsAuthCore\AuthRemoteFormsAuthCore.csproj" />
2021
<ProjectReference Include="..\AuthRemoteFormsAuthFramework\AuthRemoteFormsAuthFramework.csproj" />
2122
</ItemGroup>
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
var builder = DistributedApplication.CreateBuilder(args);
1+
using Microsoft.AspNetCore.SystemWebAdapters.Aspire;
22

3-
var remoteApiKey = builder.AddParameter("apiKey", Guid.NewGuid().ToString(), secret: true);
3+
var builder = DistributedApplication.CreateBuilder(args);
44

55
var frameworkApp = builder.AddIISExpress("iis")
66
.AddSiteProject<Projects.AuthRemoteFormsAuthFramework>("framework")
77
.WithDefaultIISExpressEndpoints()
88
.WithOtlpExporter()
9-
.WithEnvironment("RemoteApp__ApiKey", remoteApiKey)
109
.WithHttpHealthCheck();
1110

1211
var coreApp = builder.AddProject<Projects.AuthRemoteFormsAuthCore>("core")
13-
.WithEnvironment("RemoteApp__ApiKey", remoteApiKey)
14-
.WithReference(frameworkApp)
15-
.WithEnvironment("RemoteApp__Url", frameworkApp.GetEndpoint("https"))
1612
.WithHttpHealthCheck()
17-
.WaitFor(frameworkApp);
13+
.WaitFor(frameworkApp)
14+
.WithIncrementalMigrationFallback(frameworkApp, remoteAuthentication: RemoteAuthentication.DefaultScheme);
1815

1916
builder.Build().Run();
Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
var builder = WebApplication.CreateBuilder(args);
22

33
builder.AddServiceDefaults();
4+
builder.AddSystemWebAdapters();
45

56
builder.Services.AddReverseProxy();
67

7-
// Add services to the container.
88
builder.Services.AddControllersWithViews();
99

10-
// Add System.Web adapter services, including registering remote app authentication
11-
builder.Services.AddSystemWebAdapters()
12-
.AddRemoteAppClient(options =>
13-
{
14-
options.RemoteAppUrl = new(builder.Configuration["RemoteApp:Url"]!);
15-
options.ApiKey = builder.Configuration["RemoteApp:ApiKey"]!;
16-
})
17-
.AddAuthenticationClient(true);
18-
1910
var app = builder.Build();
2011

2112
// Configure the HTTP request pipeline.
@@ -40,13 +31,8 @@
4031
name: "default",
4132
pattern: "{controller=Home}/{action=Index}/{id?}");
4233

43-
// Configure the reverse proxy to forward all unhandled requests to the remote app
44-
app.MapForwarder("/{**catch-all}", app.Configuration["RemoteApp:Url"]!)
45-
46-
// If there is a route locally, we want to ensure that is used by default, but otherwise we'll forward
47-
.WithOrder(int.MaxValue)
34+
app.MapDefaultEndpoints();
4835

49-
// If we're going to forward the request, there is no need to run any of the middleware after routing
50-
.ShortCircuit();
36+
app.MapRemoteAppFallback();
5137

5238
app.Run();

samples/AuthRemoteFormsAuth/AuthRemoteFormsAuthFramework/Global.asax.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@ protected void Application_Start()
1919
builder.AddServiceDefaults();
2020
builder.RegisterWebObjectActivator();
2121

22-
builder.Services.AddSystemAdapters()
23-
.AddVirtualizedContentDirectories()
24-
.AddProxySupport(options => options.UseForwardedHeaders = true)
25-
.AddRemoteAppServer(builder.Configuration.GetSection("RemoteApp").Bind)
26-
.AddAuthenticationServer();
22+
builder.AddSystemWebAdapters()
23+
.AddVirtualizedContentDirectories();
2724
});
2825

2926
RouteConfig.RegisterRoutes(RouteTable.Routes);

samples/AuthRemoteIdentity/AuthRemoteIdentityAppHost/AuthRemoteIdentityAppHost.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
</ItemGroup>
1818

1919
<ItemGroup>
20+
<ProjectReference Include="..\..\..\src\Microsoft.AspNetCore.SystemWebAdapters.Aspire\Microsoft.AspNetCore.SystemWebAdapters.Aspire.csproj" IsAspireProjectResource="false" />
2021
<ProjectReference Include="..\AuthRemoteIdentityCore\AuthRemoteIdentityCore.csproj" />
2122
<ProjectReference Include="..\AuthRemoteIdentityFramework\AuthRemoteIdentityFramework.csproj" />
2223
</ItemGroup>
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
1+
using Microsoft.AspNetCore.SystemWebAdapters.Aspire;
12
using System.Security.Policy;
23
using Microsoft.Extensions.Configuration;
34

45
var builder = DistributedApplication.CreateBuilder(args);
56

6-
var remoteApiKey = builder.AddParameter("apiKey", Guid.NewGuid().ToString(), secret: true);
7-
8-
var iisExpress = builder.AddIISExpress("iis");
9-
107
var db = builder.AddSqlServer("identityserver")
118
.WithLifetime(ContainerLifetime.Persistent)
129
.AddDatabase("identity");
1310

14-
var frameworkApp = iisExpress.AddSiteProject<Projects.AuthRemoteIdentityFramework>("framework")
11+
var frameworkApp = builder.AddIISExpress("iis")
12+
.AddSiteProject<Projects.AuthRemoteIdentityFramework>("framework")
1513
.WithDefaultIISExpressEndpoints()
16-
.WithEnvironment("RemoteApp__ApiKey", remoteApiKey)
1714
.WithReference(db, connectionName: "DefaultConnection")
1815
.WithOtlpExporter()
1916
.WaitFor(db)
2017
.WithHttpHealthCheck();
2118

2219
var coreApp = builder.AddProject<Projects.AuthRemoteIdentityCore>("core")
23-
.WithEnvironment("RemoteApp__ApiKey", remoteApiKey)
24-
.WithEnvironment("RemoteApp__Url", frameworkApp.GetEndpoint("https"))
2520
.WithHttpHealthCheck()
26-
.WaitFor(frameworkApp);
21+
.WaitFor(frameworkApp)
22+
.WithIncrementalMigrationFallback(frameworkApp, remoteAuthentication: RemoteAuthentication.DefaultScheme);
2723

2824
builder.Build().Run();

samples/AuthRemoteIdentity/AuthRemoteIdentityCore/Program.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,7 @@
3131

3232
// Add services to the container.
3333
builder.Services.AddControllersWithViews();
34-
builder.Services.AddSystemWebAdapters()
35-
.AddRemoteAppClient(options =>
36-
{
37-
options.RemoteAppUrl = new(builder.Configuration["RemoteApp:Url"]!);
38-
options.ApiKey = builder.Configuration["RemoteApp:ApiKey"]!;
39-
})
40-
.AddAuthenticationClient(true);
34+
builder.AddSystemWebAdapters();
4135

4236
var app = builder.Build();
4337

@@ -61,13 +55,6 @@
6155

6256
app.MapDefaultControllerRoute();
6357

64-
// Configure the the reverse proxy to forward all unhandled requests to the remote app
65-
app.MapForwarder("/{**catch-all}", app.Configuration["RemoteApp:Url"]!)
66-
67-
// If there is a route locally, we want to ensure that is used by default, but otherwise we'll forward
68-
.WithOrder(int.MaxValue)
69-
70-
// If we're going to forward the request, there is no need to run any of the middleware after routing
71-
.ShortCircuit();
58+
app.MapRemoteAppFallback();
7259

7360
app.Run();

samples/AuthRemoteIdentity/AuthRemoteIdentityFramework/Global.asax.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@ protected void Application_Start()
1919
builder.AddServiceDefaults();
2020
builder.RegisterWebObjectActivator();
2121

22-
builder.Services.AddSystemAdapters()
23-
.AddVirtualizedContentDirectories()
24-
.AddProxySupport(options => options.UseForwardedHeaders = true)
25-
.AddRemoteAppServer(builder.Configuration.GetSection("RemoteApp").Bind)
26-
.AddAuthenticationServer();
22+
builder.AddSystemWebAdapters()
23+
.AddVirtualizedContentDirectories();
2724
});
2825

2926
AreaRegistration.RegisterAllAreas();

samples/ServiceDefaults/Extensions.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
using OpenTelemetry;
88
using OpenTelemetry.Metrics;
99
using OpenTelemetry.Trace;
10+
using System.Diagnostics.CodeAnalysis;
11+
using Microsoft.Extensions.Options;
12+
13+
1014

1115
#if NET
1216
using Microsoft.AspNetCore.Builder;
@@ -126,16 +130,29 @@ public static WebApplication MapDefaultEndpoints(this WebApplication app)
126130
if (app.Environment.IsDevelopment())
127131
{
128132
// All health checks must pass for app to be considered ready to accept traffic after starting
129-
app.MapHealthChecks("/health");
133+
app.MapHealthChecks("/health").ShortCircuit();
130134

131135
// Only health checks tagged with the "live" tag must pass for app to be considered alive
132136
app.MapHealthChecks("/alive", new HealthCheckOptions
133137
{
134138
Predicate = r => r.Tags.Contains("live")
135-
});
139+
}).ShortCircuit();
136140
}
137141

138142
return app;
139143
}
144+
145+
public static IEndpointConventionBuilder MapRemoteAppFallback(this WebApplication app, [StringSyntax("Route")] string? pattern = "/{**catch-all}")
146+
{
147+
var url = app.Services.GetRequiredService<IOptions<RemoteAppClientOptions>>().Value.RemoteAppUrl.OriginalString;
148+
149+
return app.MapForwarder(pattern, url)
150+
151+
// If there is a route locally, we want to ensure that is used by default, but otherwise we'll forward
152+
.WithOrder(int.MaxValue)
153+
154+
// If we're going to forward the request, there is no need to run any of the middleware after routing
155+
.ShortCircuit();
156+
}
140157
#endif
141158
}

samples/ServiceDefaults/Samples.ServiceDefaults.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" />
2323
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
24+
<PackageReference Include="Yarp.ReverseProxy" />
2425

2526
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SystemWebAdapters.CoreServices\Microsoft.AspNetCore.SystemWebAdapters.CoreServices.csproj" />
2627

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
var builder = DistributedApplication.CreateBuilder(args);
1+
using Microsoft.AspNetCore.SystemWebAdapters.Aspire;
22

3-
var remoteApiKey = builder.AddParameter("apiKey", Guid.NewGuid().ToString(), secret: true);
3+
var builder = DistributedApplication.CreateBuilder(args);
44

55
var frameworkApp = builder.AddIISExpress("iis")
66
.AddSiteProject<Projects.SessionRemoteFramework>("framework")
77
.WithDefaultIISExpressEndpoints()
88
.WithOtlpExporter()
9-
.WithEnvironment("RemoteApp__ApiKey", remoteApiKey)
109
.WithHttpHealthCheck(path: "/framework");
1110

1211
var coreApp = builder.AddProject<Projects.SessionRemoteCore>("core")
13-
.WithEnvironment("RemoteApp__ApiKey", remoteApiKey)
14-
.WithEnvironment("RemoteApp__Url", frameworkApp.GetEndpoint("https"))
1512
.WithHttpHealthCheck()
16-
.WaitFor(frameworkApp);
13+
.WaitFor(frameworkApp)
14+
.WithIncrementalMigrationFallback(frameworkApp, remoteSession: RemoteSession.Enabled);
1715

1816
builder.Build().Run();

samples/SessionRemote/SessionRemoteAppHost/SessionRemoteAppHost.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
</ItemGroup>
1717

1818
<ItemGroup>
19+
<ProjectReference Include="..\..\..\src\Microsoft.AspNetCore.SystemWebAdapters.Aspire\Microsoft.AspNetCore.SystemWebAdapters.Aspire.csproj" IsAspireProjectResource="false" />
1920
<ProjectReference Include="..\SessionRemoteCore\SessionRemoteCore.csproj" />
2021
<ProjectReference Include="..\SessionRemoteFramework\SessionRemoteFramework.csproj" />
2122
</ItemGroup>

samples/SessionRemote/SessionRemoteCore/Program.cs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,15 @@
88

99
builder.Services.AddReverseProxy();
1010

11-
builder.Services.AddSystemWebAdapters()
11+
builder.AddSystemWebAdapters()
1212
.AddSessionSerializer(options =>
1313
{
1414
options.ThrowOnUnknownSessionKey = false;
1515
})
1616
.AddJsonSessionSerializer(options =>
1717
{
1818
options.RegisterKey<int>("CoreCount");
19-
})
20-
.AddRemoteAppClient(options =>
21-
{
22-
options.ApiKey = builder.Configuration["RemoteApp:ApiKey"]!;
23-
options.RemoteAppUrl = new(builder.Configuration["RemoteApp:Url"]);
24-
})
25-
.AddSessionClient();
19+
});
2620

2721
var app = builder.Build();
2822

@@ -44,13 +38,6 @@
4438
return session.Cast<string>().Select(key => new { Key = key, Value = session[key] });
4539
}).RequireSystemWebAdapterSession();
4640

47-
// Configure the the reverse proxy to forward all unhandled requests to the remote app
48-
app.MapForwarder("/{**catch-all}", app.Configuration["RemoteApp:Url"]!)
49-
50-
// If there is a route locally, we want to ensure that is used by default, but otherwise we'll forward
51-
.WithOrder(int.MaxValue)
52-
53-
// If we're going to forward the request, there is no need to run any of the middleware after routing
54-
.ShortCircuit();
41+
app.MapRemoteAppFallback();
5542

5643
app.Run();

samples/SessionRemote/SessionRemoteFramework/Global.asax.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,11 @@ protected void Application_Start()
1616
HttpApplicationHost.RegisterHost(builder =>
1717
{
1818
builder.AddServiceDefaults();
19-
builder.Services.AddSystemAdapters()
20-
.AddProxySupport(options => options.UseForwardedHeaders = true)
21-
.AddSessionSerializer(options =>
22-
{
23-
})
19+
builder.AddSystemWebAdapters()
2420
.AddJsonSessionSerializer(options =>
2521
{
2622
options.RegisterKey<int>("CoreCount");
27-
})
28-
.AddRemoteAppServer(builder.Configuration.GetSection("RemoteApp").Bind)
29-
.AddSessionServer();
23+
});
3024
});
3125
}
3226

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
var builder = DistributedApplication.CreateBuilder(args);
1+
using Microsoft.AspNetCore.SystemWebAdapters.Aspire;
22

3-
var remoteApiKey = builder.AddParameter("apiKey", Guid.NewGuid().ToString(), secret: true);
3+
var builder = DistributedApplication.CreateBuilder(args);
44

55
var frameworkApp = builder.AddIISExpress("iis")
66
.AddSiteProject<Projects.WebFormsToBlazorFramework>("framework")
77
.WithDefaultIISExpressEndpoints()
88
.WithOtlpExporter()
9-
.WithEnvironment("RemoteApp__ApiKey", remoteApiKey)
109
.WithHttpHealthCheck();
1110

1211
var coreApp = builder.AddProject<Projects.WebFormsToBlazorCore>("core")
13-
.WithEnvironment("RemoteApp__ApiKey", remoteApiKey)
14-
.WithEnvironment("RemoteApp__Url", frameworkApp.GetEndpoint("https"))
1512
.WithHttpHealthCheck()
16-
.WaitFor(frameworkApp);
13+
.WaitFor(frameworkApp)
14+
.WithIncrementalMigrationFallback(frameworkApp, remoteSession: RemoteSession.Enabled);
1715

1816
builder.Build().Run();

samples/WebFormsToBlazor/WebFormsToBlazorAppHost/WebFormsToBlazorAppHost.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
</ItemGroup>
1717

1818
<ItemGroup>
19+
<ProjectReference Include="..\..\..\src\Microsoft.AspNetCore.SystemWebAdapters.Aspire\Microsoft.AspNetCore.SystemWebAdapters.Aspire.csproj" IsAspireProjectResource="false" />
1920
<ProjectReference Include="..\WebFormsToBlazorCore\WebFormsToBlazorCore.csproj" />
2021
<ProjectReference Include="..\WebFormsToBlazorFramework\WebFormsToBlazorFramework.csproj" />
2122
</ItemGroup>

0 commit comments

Comments
 (0)