简体   繁体   English

WebSocket in .NET framework 4.6.1 with WebAPI2 controllers

[英]WebSocket in .NET framework 4.6.1 with WebAPI2 controllers

I am trying to write a new WebSocket with a WebAPI2 controller in our .NET framework application.我正在尝试在我们的 .NET 框架应用程序中使用 WebAPI2 controller 编写一个新的 WebSocket。 When I try to hit the websocket endpoint using Postman on localhost, I get the following error saying WebSocket is unsupported.当我尝试在本地主机上使用 Postman 访问 websocket 端点时,出现以下错误,指出 WebSocket 不受支持。

Exception thrown: 'System.InvalidOperationException' in System.Web.dll
An exception of type 'System.InvalidOperationException' occurred in System.Web.dll but was not handled in user code
WebSockets is unsupported in the current application configuration. To enable this, set the following configuration switch in Web.config:
<system.web>
  <httpRuntime targetFramework="4.5" />
</system.web>
For more information, see http://go.microsoft.com/fwlink/?LinkId=252465

My Websocket was implemented as shown on https://www.codeproject.com/Articles/618032/Using-WebSocket-in.NET-4-5-Part-2 .我的 Websocket 的实现方式如https://www.codeproject.com/Articles/618032/Using-WebSocket-in.NET-4-5-Part-2所示。 The code looks like follows:代码如下所示:

public class CANCamController : ApiController
{
    [Route("api2.0/CANCam/Connect")]
    [HttpGet]
    public HttpResponseMessage Connect()
    {
        Debug.WriteLine("Connection request");
        if (HttpContext.Current.IsWebSocketRequest)
        {
            HttpContext.Current.AcceptWebSocketRequest(ProcessCANCam);
        }
        return new HttpResponseMessage(HttpStatusCode.SwitchingProtocols);
    }
    private async Task ProcessCANCam(AspNetWebSocketContext context)
    {
        WebSocket socket = context.WebSocket;
        while (true)
        {
            ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]);
            WebSocketReceiveResult result = await socket.ReceiveAsync(
                buffer, CancellationToken.None);
            if (socket.State == WebSocketState.Open)
            {
                string userMessage = Encoding.UTF8.GetString(
                    buffer.Array, 0, result.Count);
                userMessage = "You sent: " + userMessage + " at " +
                    DateTime.Now.ToLongTimeString();
                buffer = new ArraySegment<byte>(
                    Encoding.UTF8.GetBytes(userMessage));
                await socket.SendAsync(
                    buffer, WebSocketMessageType.Text, true, CancellationToken.None);
            }
            else
            {
                break;
            }
        }
    }

    [Route("api2.0/CANCam/CheckApi")]
    [HttpGet]
    public void CheckApi()
    {
        JsonResponse.NewResponse("API is up and running");
    }
}

I thought websocket was supported on .NET framework 4.5 and above, why would it throw this error on .NET 4.6.1?我以为 websocket 在 .NET 框架 4.5 及以上支持,为什么它会在 .NET 4.6.1 上抛出这个错误? How do I fix this?我该如何解决?

Only add仅添加

<system.web>
  <httpRuntime targetFramework="4.6" />
</system.web>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM