Home > Back-end >  ServiceStack 5.13.0 metadata and swagger-ui pages return a 500 error after .NET 6 migration
ServiceStack 5.13.0 metadata and swagger-ui pages return a 500 error after .NET 6 migration

Time:11-19

I've recently started migrating my microservices to .NET 6. I upgraded to ServiceStack 5.13.0 from 5.11.0 and I found out that both the /metadata and the /swagger-ui (from ServiceStack.Api.OpenApi package) pages return HTTP status code 500. I get no exception whatsoever.

Note: The rest of the microservice works perfectly fine. I can make calls to other endpoints without any issue.

However, when I enable debugging logs in ServiceStack, I get the following exceptions whenever I visit either /metadata or /swagger-ui

[Info] Service is starting up.
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7058
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5058
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Projects\chargeleo.users.managementservice\Chargeleo.Users.ManagementService.Web\
fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HMD882JRAOH5", Request id "0HMD882JRAOH5:00000007": An unhandled exception was thrown by the application.
      System.InvalidOperationException: An asynchronous socket operation is already in progress using this SocketAsyncEventArgs instance.
         at System.Net.Sockets.SocketAsyncEventArgs.ThrowForNonFreeStatus(Int32 status)
         at System.Net.Sockets.SocketAsyncEventArgs.set_BufferList(IList`1 value)
         at Microsoft.WebTools.BrowserLink.Net.SocketAdapter.SendAsync(IList`1 buffers)
      --- End of stack trace from previous location ---
         at Microsoft.WebTools.BrowserLink.Net.DelayConnectingHttpSocketAdapter.Microsoft.WebTools.BrowserLink.Net.IHttpSocketAdapter.CompleteRequestAsync()
         at Microsoft.WebTools.BrowserLink.Net.ScriptInjectionFilterStream.WaitForFilterCompleteAsync()
         at Microsoft.WebTools.BrowserLink.Net.BrowserLinkMiddleware.ExecuteWithFilterAsync(IHttpSocketAdapter injectScriptSocket, String requestId, HttpContext httpContext)
         at Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware.InvokeAsync(HttpContext context)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HMD882JRAOH6", Request id "0HMD882JRAOH6:00000001": An unhandled exception was thrown by the application.
      System.InvalidOperationException: Attempted to send data when a send was already in progress.
         at Microsoft.WebTools.BrowserLink.Net.DelayConnectingHttpSocketAdapter.Microsoft.WebTools.BrowserLink.Net.IHttpSocketAdapter.CompleteRequestAsync()
         at Microsoft.WebTools.BrowserLink.Net.ScriptInjectionFilterStream.WaitForFilterCompleteAsync()
         at Microsoft.WebTools.BrowserLink.Net.BrowserLinkMiddleware.ExecuteWithFilterAsync(IHttpSocketAdapter injectScriptSocket, String requestId, HttpContext httpContext)
         at Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware.InvokeAsync(HttpContext context)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

This did not happen before migrating to .NET 6. Both pages were working perfectly fine. My startup code is the following:

Program.cs

using System.Diagnostics;
using ServiceStack;

var builder = WebApplication.CreateBuilder(args);

Debug.WriteLine(builder.Configuration["Environment"]);

var app = builder.Build();
app.UseServiceStack(new AppHost(builder.Configuration["Environment"]));

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

app.Run();

AppHost.cs:

using Funq;
using ServiceStack;
using Chargeleo.Users.ManagementService.Common;

namespace Chargeleo.Users.ManagementService.Web;

public class AppHost : AppHostBase
{
    private AppConfigurator _configurator;
    private readonly string _environment;

    public AppHost(string environment) : base(ServiceConstants.ServiceName, typeof(AppConfigurator).Assembly)
    {
        _environment = environment;
    }

    public override void Configure(Container container)
    {
        _configurator = new AppConfigurator(this, _environment);
    }
}

ServiceStack related code in AppConfigurator.cs

appHost.SetConfig(new HostConfig { DebugMode = true });
appHost.Plugins.Add(new CorsFeature());
appHost.Plugins.Add(new ValidationFeature());
appHost.Plugins.Add(new OpenApiFeature());

I can't seem to figure out what the issue is, any inputs will be highly appreciated, thanks a lot.

CodePudding user response:

I figured out how to solve the problem after following mythz's advice. The issue was indeed BrowserLink and the new hot reload functionality.

Disabling the hot reload functionality solves the problem.

I followed the instructions here: How to disable Browser Link in ASP.NET Core (.NET 6, VS 2022)

  • Related