Search code examples
c#websocketwebformssignalr

WebSockets is unsupported in the current application configuration


I have a websocket error when trying to use SignalR.

Full Error :

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..    
at System.Web.Util.SynchronizationContextUtil.ValidateMode(SynchronizationContextMode currentMode, 
SynchronizationContextMode requiredMode, String specificErrorMessage)    
at System.Web.HttpContext.AcceptWebSocketRequest(Func`2 userFunc, AspNetWebSocketOptions options)     
at Microsoft.AspNet.SignalR.Transports.WebSocketTransport.AcceptWebSocketRequest(Func`2 callback)     
at Microsoft.AspNet.SignalR.PersistentConnection.ProcessRequestPostGroupRead(HostContext context, 
String groupsToken)     
at Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod[T1,T2,T3,TResult]
(Func`4 func, T1 arg1, T2 arg2, T3 arg3) 

I've checked similar posts on Stackoverflow but my issue seems to be different. Because, I establish connection with SignalR.

    <script src="/Scripts/jquery-3.3.1.js"></script>
    <script src="/Scripts/jquery.signalR-2.2.2.js"></script>
    <script src="/signalr/hubs"></script>

    <script type="text/javascript">

        $(function () {

            var asset1 = document.getElementById('MainContent_MainContent_Asset1Hidden').value;
            var asset2 = document.getElementById('MainContent_MainContent_Asset2Hidden').value;

            var socket = $.connection.marketHub;

            socket.client.refreshSellOrders = function (msg) {
                document.getElementById("MainContent_MainContent_sellOrders").innerHTML = msg;
            };

            socket.client.refreshBuyOrders = function (msg) {
                document.getElementById("MainContent_MainContent_buyOrders").innerHTML = msg;
            };

            socket.client.refreshCompletedOrders = function (msg) {
                document.getElementById("MainContent_MainContent_completedOrders").innerHTML = msg;
            };

            $.connection.hub.qs = { 'asset1': asset1, 'asset2': asset2 };
            $.connection.hub.start();
        });

    </script>

And I have WebSockets enabled.

configurations

Additional code you may need :

    public class MarketHub : Hub
    {

        public static readonly System.Timers.Timer _Timer = new System.Timers.Timer();


        static MarketHub()
        {
        }

        public override System.Threading.Tasks.Task OnConnected()
        {
            string mAsset1 = Context.QueryString["asset1"];
            string mAsset2 = Context.QueryString["asset2"];

            string body = "{0}/{1}";
            string roomName = string.Format(body, mAsset1, mAsset2);


            JoinRoom(roomName);

            return base.OnConnected();

        }
        public System.Threading.Tasks.Task JoinRoom(string roomName)
        {
            System.Threading.Tasks.Task result = Groups.Add(Context.ConnectionId, roomName);
            return result;
        }

    }

On Startup

    protected void Application_Start(object sender, EventArgs e, IAppBuilder app)
        {
            app.MapSignalR();

            GlobalConfiguration.Configure(config =>
            {
                config.MapHttpAttributeRoutes();

                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/v2/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
            });

        }

Web.config

    <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="1234525324532" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
      </dependentAssembly>

Data is fine. SignalR is given correct data. It just doesn't update automatically.


Solution

  • Finally, I realized that I had SignalR 2.4.1 reference installed and I was using 2.2.2 .js file

    <script src="/Scripts/jquery.signalR-2.2.2.js"></script>
    

    replaced it with

    <script src="/Scripts/jquery.signalR-2.4.1.js"></script>