gpt4 book ai didi

c# - 从 ASP.NET Core 中的 WebSocket 请求返回什么 IActionResult?

转载 作者:行者123 更新时间:2023-11-30 21:33:48 29 4
gpt4 key购买 nike

我有一个正在运行的 ASP.NET Core 2.1 webapi 正在监听网络套接字请求。如果请求不是针对 WebSocket 升级,我想返回 this.BadRequest()

[Route("api/[controller]")]
[ApiController]
public class SocketController : ControllerBase
{
[HttpGet]
public async Task<IActionResult> Post()
{
if (!this.HttpContext.WebSockets.IsWebSocketRequest)
return this.BadRequest();

using (var webSocket = await this.HttpContext.WebSockets.AcceptWebSocketAsync())
{
using (var jsonRpc = new JsonRpc(new WebSocketMessageHandler(webSocket)))
{
jsonRpc.AddLocalRpcTarget(new SocketServer());
jsonRpc.StartListening();
await jsonRpc.Completion;
}
}

return this.Ok();
}
}

但是无论走成功路径还是失败路径,服务器都会抛出异常:

fail: Microsoft.AspNetCore.Server.Kestrel[13] Connection id "0HLEM87126JEO", Request id "0HLEM87126JEO:00000001": An unhandled exception was thrown by the application. System.InvalidOperationException: StatusCode cannot be set because the response has already started. at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ThrowResponseAlreadyStartedException(String value) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.set_StatusCode(Int32 value) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.Microsoft.AspNetCore.Http.Features.IHttpResponseFeature.set_StatusCode(Int32 value) at Microsoft.AspNetCore.Http.Internal.DefaultHttpResponse.set_StatusCode(Int32 value) at Microsoft.AspNetCore.Mvc.StatusCodeResult.ExecuteResult(ActionContext context) at Microsoft.AspNetCore.Mvc.ActionResult.ExecuteResultAsync(ActionContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsyncTFilter,TFilterAsync at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

如果我将方法更改为只返回 Task 就可以正常工作,这样我就不必提供 IActionResult,但是我没有办法对错误请求返回 400 响应。解决此问题的最佳方法是什么?

我的中间件非常简单:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// app.UseHsts();
}

// app.UseHttpsRedirection();
app.UseWebSockets();
app.UseMvc();
}

最佳答案

secret 是 EmptyResult,它没有 this.Empty() 方法。

public async Task<IActionResult> Post()
{
if (!this.HttpContext.WebSockets.IsWebSocketRequest)
{
return this.BadRequest();
}

using (var webSocket = await this.HttpContext.WebSockets.AcceptWebSocketAsync())
{
using (var jsonRpc = new JsonRpc(new WebSocketMessageHandler(webSocket)))
{
jsonRpc.AddLocalRpcTarget(new SocketServer());
jsonRpc.StartListening();
await jsonRpc.Completion;
}
}

return new EmptyResult();
}

关于c# - 从 ASP.NET Core 中的 WebSocket 请求返回什么 IActionResult?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50938522/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com