Search code examples
c#asp.netwebsocketsignalr

C# SignalR GetHttpContext / HttpContext Response Does not Exist


Having an issue upgrading an asp.net application to use SignalR with WebSocket Protocol enabled. Beforehand, with just SignalR, I was able to set and add response cookies before the the websocket protocol was enabled. I've noticed now while debugging that with HttpResponse from HttpContext and HttpResponseBase from GetHttpContext, I am either getting a

Response is not available in this context

or

threw an exception of type System.Web.HttpException

 Context.Request.GetHttpContext().Response

 HttpContext.Current.Response.Cookies

Funny thing is, is that the request for of the context still exists and doesn't give me any issues. Also, this happens within the SignalR HUB classes. Any help would be appreciated.


Solution

  • The reason is that SignalR's underlying technology is different to MVC - there is no ability to set cookies (cookies are set in a HTTP response, but there is no such thing when using web sockets).

    So, basically you can't do it. You can read cookies however (the cookies when the websocket connection was initialised anyway...), using the hub's context.