ServiceStack 5.13.0 元数据和 swagger-ui 页面在 .NET 6 迁移后返回 500 错误

Posted

技术标签:

【中文标题】ServiceStack 5.13.0 元数据和 swagger-ui 页面在 .NET 6 迁移后返回 500 错误【英文标题】:ServiceStack 5.13.0 metadata and swagger-ui pages return a 500 error after .NET 6 migration 【发布时间】:2021-12-26 17:45:38 【问题描述】:

我最近开始将我的微服务迁移到 .NET 6。我从 5.11.0 升级到 ServiceStack 5.13.0,我发现 /metadata 和 /swagger-ui(来自 ServiceStack.Api.OpenApi 包) 页面返回 HTTP 状态代码 500。我没有任何异常。

注意:微服务的其余部分运行良好。我可以毫无问题地拨打其他端点。

但是,当我在 ServiceStack 中启用调试日志时,每当我访问 /metadata 或 /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)

在迁移到 .NET 6 之前没有发生这种情况。两个页面都运行良好。我的启动代码如下:

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);
    

AppConfigurator.cs中ServiceStack相关代码

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

我似乎无法弄清楚问题是什么,任何输入都将受到高度赞赏,非常感谢。

【问题讨论】:

此异常来自 BrowserLink,这是 VS 通常添加的开发时间功能。当您在本地运行时是否仍然发生异常,例如从命令行使用dotnet run @mythz 不,它没有。似乎与 dotnet run 一起工作正常。我通过 Visual Studio 的调试功能运行我的代码。有什么方法可以禁用 BrowserLink?我根本没有使用过 dotnet run,所以我不太熟悉它,我不知道如何在不从 VS 本身运行的情况下调试我的代码。 不要使用 VS,而是用谷歌搜索我 found these answers on disabling BrowserLink,因为这是 VS BrowserLink 的问题,您可以 report an Issue from VS。 【参考方案1】:

在听从了 myz 的建议后,我想出了如何解决这个问题。问题确实是 BrowserLink 和新的热重载功能。

禁用热重载功能可以解决问题。

我按照这里的说明进行操作: How to disable Browser Link in ASP.NET Core (.NET 6, VS 2022)

【讨论】:

以上是关于ServiceStack 5.13.0 元数据和 swagger-ui 页面在 .NET 6 迁移后返回 500 错误的主要内容,如果未能解决你的问题,请参考以下文章

ServiceStack 元数据页面抛出 MemberAccessException:无法创建抽象类

ServiceStack - 检查单元测试中的 WSDL 更改

ServiceStack.Redis 请求次数6000次异常

ServiceStack.Redis 请求次数6000次异常

如何手动释放servicestack.redis连接池连接

具有多个数据库服务器的 ServiceStack OrmLite