コード例 #1
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            var builder = new ContainerBuilder();
            var config  = GlobalConfiguration.Configuration;

            builder.RegisterWebApiFilterProvider(config);
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            builder.RegisterWebApiModelBinderProvider();
            AutofacWebApiConfig.Register(builder);
            var container = builder.Build();

            var resolver = new AutofacWebApiDependencyResolver(container);

            GlobalConfiguration.Configuration.DependencyResolver = resolver;

            ServiceLocator.SetLocatorProvider(() => new AutofacServiceLocator(container));
        }
コード例 #2
0
        public static HttpConfiguration Register()
        {
            var httpConfiguration = new HttpConfiguration();

            httpConfiguration.SuppressDefaultHostAuthentication();
            httpConfiguration.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

            httpConfiguration.Formatters
            .JsonFormatter
            .MediaTypeMappings
            .Add(new RequestHeaderMapping("Accept", "text/html",
                                          StringComparison.InvariantCultureIgnoreCase, true, "application/json"));

            httpConfiguration.Formatters
            .JsonFormatter
            .SerializerSettings
            .ContractResolver = new CamelCasePropertyNamesContractResolver();

            httpConfiguration.Formatters
            .JsonFormatter
            .SerializerSettings
            .NullValueHandling = NullValueHandling.Ignore;

            httpConfiguration.MessageHandlers.Insert(0, new ServerCompressionHandler(new GZipCompressor(), new DeflateCompressor()));
            httpConfiguration.MapHttpAttributeRoutes();
            httpConfiguration.DependencyResolver = new AutofacWebApiDependencyResolver(AutofacWebApiConfig.Register(new ContainerBuilder()));

            SwaggerConfig.Register(httpConfiguration);

            return(httpConfiguration);
        }