Ejemplo n.º 1
0
        internal static void Register(HttpConfiguration config)
        {
            var container = new UnityContainer();
            config.DependencyResolver = new UnityResolver(container);

            Resolver resolver = new Resolver();
            resolver.RegisterTypes(container);

            config.MapHttpAttributeRoutes();

            config.Filters.Add(new GenericExceptionFilterAttribute());

            config.Filters.Add(new LogAttribute("SignboardApi", 500));
        }
Ejemplo n.º 2
0
        internal static void Register(HttpConfiguration config)
        {
            // IOC container
            var container = new UnityContainer();
            config.DependencyResolver = new UnityResolver(container);

            // IOC resolution
            Resolver resolver = new Resolver();
            resolver.RegisterTypes(container);

            // Logger
            var logger = container.Resolve<ILoggingComponent>();
            logger.LogInfo(MethodBase.GetCurrentMethod(), "Web Api configuration started.");

            //var cors = new EnableCorsAttribute("localhost:50937,localhost:80", "*", "*");
            var cors = new EnableCorsAttribute("*", "*", "*");
            config.EnableCors(cors);

            // Ignore any authentication which happens before the Web API pipeline (by ISS or OWIN).  This restricts the API to only authenticate using bearer tokens.
            config.SuppressDefaultHostAuthentication();

            // Enable authentication using bearer tokens
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

            // Custom route constraints
            var constraintResolver = new DefaultInlineConstraintResolver();
            constraintResolver.ConstraintMap.Add("deviceState", typeof(DeviceStateRouteConstraint));
            constraintResolver.ConstraintMap.Add("companyId", typeof(CompanyIdRouteConstraint));
            constraintResolver.ConstraintMap.Add("macAddress", typeof(MacAddressRouteConstraint));

            // API attribute routing
            config.MapHttpAttributeRoutes(constraintResolver);

            // API action filters
            config.Filters.Add(new LogAttribute("PortalApi", 200));
            config.Filters.Add(new ValidateModelStateAttribute());
            config.Filters.Add(new GlobalExceptionFilterAttribute(logger));

            // API formatters
            config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new StringEnumConverter());

            logger.LogInfo(MethodBase.GetCurrentMethod(), "Web Api configuration complete.");
        }