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.