Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

308
Vistas
Converting OwinHttpRequestContext to HttpContext? (IHttpHandler and websockets)

I am trying to implement web sockets in my application that currently implements a RESTful web API. I want certain pieces of information to be able to be exposed by a web socket connection so I am trying to figure out how to upgrade a normal HTTP request to a web socket connection.

I am trying to follow this tutorial

The problem I am having is my controller that handles the Get request inherits from ApiController and I am trying to do this:

 if (HttpContext.Current.IsWebSocketRequest)
    {
        HttpContext.Current.AcceptWebSocketRequest(SomeFunction);
    }
    return new HttpResponseMessage(HttpStatusCode.SwitchingProtocols);

The problem is my HttpContext.Current is always null and I do not know why. It seems like there is no such thing as a HttpContext. I do see however from the request that comes in that their is a HttpRequestContext that comes in with the request. Are these two objects related at all and is there any way I can get access to the .IsWebSocketRequest method so I can try and upgrade the request to a web socket connection?

My controller and what I am trying to do:

    [HttpGet]
    public HttpResponseMessage GetSomething()    
    {
        if (HttpContext.Current.IsWebSocketRequest()){
             Console.WriteLine("web socket request..");  
        }
    }

^ HttpContext.Current is always null. My controller inherits solely from ApiController

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

To access the Httpcontext in an MVC ApiController, use this code.

 if (Request.Properties.ContainsKey("MS_HttpContext")) { HttpContextWrapper HttpContext = (HttpContextWrapper)Request.Properties["MS_HttpContext"]; if (HttpContext != null) { if (HttpContext.IsWebSocketRequest) { HttpContext.AcceptWebSocketRequest(SomeFunction); } } }

This will remove the HttpContext from the request if provided.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda