public void Configuration(IAppBuilder appBuilder)
        {
            appBuilder.Use <GlobalExceptionMiddleware>();

            appBuilder.Map("/api", api =>
            {
                // Create our config object we'll use to configure the API
                //
                var config = new HttpConfiguration();

                config.UseStructureMap(WebModule.Container);

                var resolver = config.DependencyResolver;
                // Use attribute routing
                //
                config.MapHttpAttributeRoutes();

                config.MessageHandlers.Add(new ApiLogHandler(resolver.GetService <ILogger>()));
                config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());

                // clear the supported mediatypes of the xml formatter
                config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();

                //Remove the XM Formatter from the web api
                config.Formatters.Remove(config.Formatters.XmlFormatter);

                var jsonFormatter = config.Formatters.JsonFormatter;
                jsonFormatter.UseDataContractJsonSerializer = false;

                var settings = jsonFormatter.SerializerSettings;
                settings.ContractResolver = new CamelCasePropertyNamesContractResolver();

#if DEBUG
                settings.Formatting = Formatting.Indented; // Pretty json for developers.
#else
                settings.Formatting = Formatting.None;
#endif

                // Now add in the WebAPI middleware
                //
                api.UseWebApi(config);
            });

            appBuilder.Map("/signalr", map =>
            {
                // Setup the CORS middleware to run before SignalR.
                // By default this will allow all origins. You can
                // configure the set of origins and/or http verbs by
                // providing a cors options with a different policy.
                map.UseCors(CorsOptions.AllowAll);

                var config = new HubConfiguration
                {
                    // You can enable JSONP by uncommenting line below.
                    // JSONP requests are insecure but some older browsers (and some
                    // versions of IE) require JSONP to work cross domain
                    // EnableJSONP = true
                    EnableDetailedErrors    = true,
                    EnableJavaScriptProxies = true
                };

                config.UseStructureMap(WebModule.Container);

                // camelcase contract resolver
                var serializer = JsonSerializer.Create(new JsonSerializerSettings
                {
                    ContractResolver = new SignalRContractResolver()
                });

                config.Resolver.Register(typeof(JsonSerializer), () => serializer);

                GlobalHost.DependencyResolver = config.Resolver;

                // Run the SignalR pipeline. We're not using MapSignalR
                // since this branch is already runs under the "/signalr"
                // path.
                map.RunSignalR(config);
            });

            var webRoot = @"./wwwroot";
            var cfg     = ConfigurationManager.AppSettings["web:wwwroot"];
            if (!string.IsNullOrEmpty(cfg))
            {
                if (Directory.Exists(cfg))
                {
                    if (File.Exists(Path.Combine(cfg, "index.html")))
                    {
                        webRoot = cfg;
                    }
                }
            }

            var physicalFileSystem = new PhysicalFileSystem(webRoot);
            var options            = new FileServerOptions
            {
                EnableDefaultFiles      = true,
                FileSystem              = physicalFileSystem,
                EnableDirectoryBrowsing = false
            };
            options.StaticFileOptions.FileSystem            = physicalFileSystem;
            options.StaticFileOptions.ServeUnknownFileTypes = true;
            options.DefaultFilesOptions.DefaultFileNames    = new[]
            {
                "index.html"
            };

            appBuilder.UseFileServer(options);
        }
Ejemplo n.º 2
0
 public static void UseStructureMap <T>(this HubConfiguration config)
     where T : Registry, new()
 {
     config.UseStructureMap(new T());
 }