예제 #1
0
        private static void StartWebApiServices()
        {
            var baseAddress   = ConfigurationManager.AppSettings["ServerAddress"];
            var hostingParams = new SelfHostingParameters(baseAddress);

            _webApiService          = WebApiConfig.SelfHost(hostingParams);
            Console.ForegroundColor = ConsoleColor.Cyan;
        }
예제 #2
0
        public static void Configure(HttpConfiguration config, SelfHostingParameters hostingParameters)
        {
            // Web API configuration and services

            config.EnableCors();

            // Enable this to see WebApi error log in output window
            //config.EnableSystemDiagnosticsTracing();

            if (hostingParameters.ControllerTypes != null)
            {
                config.Services.Replace(typeof(IHttpControllerTypeResolver), new SiemplifyControllerResolver(hostingParameters.ControllerTypes));
            }

            // Web API routes - loads all inheritance of ApiController (via reflection):
            config.MapHttpAttributeRoutes();

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

            //config.SuppressDefaultHostAuthentication();

            config.MessageHandlers.Insert(0, new OwinServerCompressionHandler(new GZipCompressor()));
            //config.MessageHandlers.Add(new ResponseLogger());
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

            config.Formatters.Remove(config.Formatters.JsonFormatter);
            var dataContractJsonFormatter = new DataContractJsonFormatter();

            config.Formatters.Insert(0, dataContractJsonFormatter);
            config.Formatters.JsonFormatter.UseDataContractJsonSerializer = hostingParameters.UseDataContractSerializer;

            dataContractJsonFormatter.MediaTypeMappings.Add(
                new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json")));

            GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(
                new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml")));

#if DEBUG
            config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
#endif
        }
예제 #3
0
        public static IDisposable SelfHost(SelfHostingParameters hostingParams)
        {
            var startupOptions = new StartOptions(hostingParams.BaseAddress);

            return(WebApp.Start(startupOptions, appBuilder =>
            {
                var serverPort = GetPort(startupOptions.Urls.First());
                VerifyFreePort(serverPort);
                HttpListener listener = (HttpListener)appBuilder.Properties["System.Net.HttpListener"];
                listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication
                                                 | AuthenticationSchemes.Negotiate
                                                 | AuthenticationSchemes.Ntlm
                                                 | AuthenticationSchemes.Anonymous;


                OAuthOptions = new OAuthBearerAuthenticationOptions()
                {
                    AuthenticationMode = AuthenticationMode.Active
                };

                appBuilder.UseOAuthBearerAuthentication(OAuthOptions);

                var config = new HttpConfiguration();


                Configure(config, hostingParams);
                appBuilder.UseWebApi(config);
                config.EnsureInitialized();
                var controllerSelectorService = config.Services.GetHttpControllerSelector();

                Console.WriteLine("======= Loading application server controllers");
                var controllers = controllerSelectorService.GetControllerMapping().Values;
                foreach (var controller in controllers)
                {
                    Console.WriteLine(string.Format("Loaded WebApi controller {0}.", controller.ControllerName));
                }
            }));
        }