0

I have a browser hosted in a Windows form using CefSharp that no longer fires the Owin AppBuilder.Use method. I am self-hosting Web API. This code was working fine, but just stopped working and there are no exceptions being thrown. The error in the CefSharp browser is 'websocket connection refused'.

I have tried calling this code from a Chrome browser and it works correctly. The websockets connect with no problems.

using WebSocketAccept = Action<IDictionary<string, object>, Func<IDictionary<string, object>, Task>>;

public static class WebApiStartup
{
    // This code configures Web API. The Startup class is specified as a type
    // parameter in the WebApp.Start method.
    public static void Configuration(IAppBuilder appBuilder, IUnityContainer unityContainer)
    {
        var config = new HttpConfiguration
        {
            DependencyResolver = new UnityDependencyResolver(unityContainer)
        };

        // JSONP formatter
        var jsonpFormatter = new JsonpMediaTypeFormatter(config.Formatters.JsonFormatter);
        config.Formatters.Add(jsonpFormatter);

        // JSON formatter
        config.Formatters.JsonFormatter.SerializerSettings.ContractResolver =
            new CamelCasePropertyNamesContractResolver();

        config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;

        // If need to convert enum to string add StringEnumConverter to the settings
        // config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new StringEnumConverter())

        // CORS
        config.EnableCors();

        // Web API routes
        config.MapHttpAttributeRoutes();

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

        //JSON Camel Case
        config.Formatters.Clear();
        config.Formatters.Add(new JsonMediaTypeFormatter());
        config.Formatters.JsonFormatter.SerializerSettings =
            new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };

        appBuilder.Use((context, next) =>
        {
            var accept = context.Get<WebSocketAccept>("websocket.Accept");
            if (accept == null)
            {
                // Not a websocket request
                return next();
            }

            var requestHeaders = GetValue<IDictionary<string, string[]>>(context.Environment, "owin.RequestHeaders");

            Dictionary<string, object> acceptOptions = null;
            string[] subProtocols;
            if (requestHeaders.TryGetValue("Sec-WebSocket-Protocol", out subProtocols) && subProtocols.Length > 0)
            {
                acceptOptions =
                    new Dictionary<string, object>
                    {
                        {"websocket.SubProtocol", subProtocols[0].Split(',').First().Trim()} // Select the first one from the client
                    };
            }

            var controllerMainScreen = new MainScreenController(unityContainer.Resolve<ILogRepository>());

            accept(acceptOptions, controllerMainScreen.ProcessSocketConnection);

            return Task.FromResult<object>(null);
        });

        appBuilder.UseWebApi(config);
    }

    static T GetValue<T>(IDictionary<string, object> env, string key)
    {
        object value;
        return env.TryGetValue(key, out value) && value is T ? (T)value : default(T);
    }

Any suggestion as to where to look to debug/fix this problem? Again the line

accept(acceptOptions, controllerMainScreen.ProcessSocketConnection);

is called and executes correctly when called from a Chrome browser. This code just suddenly stopped working and no exceptions are being thrown.

GamerDev
  • 2,006
  • 4
  • 24
  • 31
  • Have you tried disabling firewall? If firewall is not it then try testing your web application with the Sample Application (cefclient.exe). You can download it from http://opensource.spotify.com/cefbuilds/index.html . This will help isolate the issue better. – Sergey Avdeev Oct 14 '17 at 03:29
  • This is all running on my local dev machine. There is no firewall – GamerDev Oct 15 '17 at 15:45
  • Did the Sample Application work and triggered `appBuilder.Use`? – Sergey Avdeev Oct 17 '17 at 20:13
  • Yes, the sample app worked, and this code in my question worked. My code had been working and it just stopped hitting this line: accept(acceptOptions, controllerMainScreen.ProcessSocketConnection); – GamerDev Oct 19 '17 at 12:23

0 Answers0