Search code examples
asp.net-mvcasp.net-core.net-8.0asp.net-core-8ms-yarp

Incremental migration from ASP.NET to ASP.NET Core and failed to proxy on deployment (using YARP)


I'm doing incremental migration by following - https://devblogs.microsoft.com/dotnet/incremental-asp-net-to-asp-net-core-migration/.

I was able achieve that on local using VS, the yarp is proxying to the ASP.NET application, however when I try to deploy the application it is redirecting to actual url (of ASP.NET application) rather than proxying.

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSystemWebAdapters()
       .AddJsonSessionSerializer(options =>
           {
               options.RegisterKey<string>("userInfo");
           })
       .AddRemoteAppClient(option =>
           {
               option.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:cluster1:Destinations:destination1:Address"]);
               option.ApiKey = builder.Configuration["RemoteAppApiKey"];
           })
       .AddSessionClient();

builder.Services.AddHttpForwarder();
builder.Services.AddControllersWithViews();
var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseSystemWebAdapters();
app.MapDefaultControllerRoute().RequireSystemWebAdapterSession(); 

app.MapForwarder("/{**catch-all}", builder.Configuration["ReverseProxy:Clusters:cluster1:Destinations:destination1:Address"]).Add(static builder => ((RouteEndpointBuilder)builder).Order = int.MaxValue);

app.MapControllerRoute("Default", "{controller=Login}/{action=QLogin}/{id?}").RequireSystemWebAdapterSession();

app.Run();

App settings

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "RemoteAppApiKey": "cf3ef610-24eb-4d2f-a8f3-1ba4d2dcb87d",
  "AllowedHosts": "*",
  "ReverseProxy": {
    "Routes": {
      "route1": {
        "ClusterId": "cluster1",
        "Match": {
          "Path": "{**catch-all}"
        }
      }
    },
    "Clusters": {
      "cluster1": {
        "Destinations": {
          "destination1": {
            "Address": "http://localhost:62541"
          }
        }
      }
    }
  }
}

In IIS, I have deployed both applications (ASP.NET and ASP.NET Core) and replace the address in appsettings.json with the ASP.NET url.


Solution

  • I tried to follow the doc and use your appsetting to test it, it works well.

    I suggest you re-check your ASP.NET application's url to firstly make sure it works well.

    Then I suggest you could re-check your IIS settings to make sure you don't have other IIS URL rewrite rule which caused the issue.

    Besides, for session share, I suggest you could follow this article.

    Result:

    My asp.net application port is 8094 and my asp.net core application port is 8092.

    enter image description here