예제 #1
0
        public static void GetConfiguration(Owin.IAppBuilder app)
        {
            HttpConfiguration config = new HttpConfiguration();

            config.Filters.Add(new ExceptionHandlingAttribute());
            config.Filters.Add(new ActionHandlingFilter());

            config.Services.Replace(typeof(IAssembliesResolver), new ExtendedDefaultAssembliesResolver());
            config.Routes.MapHttpRoute(
                name: "DefaultAPi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
                );

            //config.Formatters.JsonFormatter.UseDataContractJsonSerializer = true;
            //config.Formatters.Remove(config.Formatters.XmlFormatter);
            //config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

            config.Formatters.XmlFormatter.UseXmlSerializer = true;
            config.Formatters.Remove(config.Formatters.JsonFormatter);

            var builder = new ContainerBuilder();

            //builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            //var types = config.Services.GetAssembliesResolver().GetAssemblies().SelectMany(c => c.GetTypes());
            //var type = typeof(ApiController);
            //var list = types.Where(c => type.IsAssignableFrom(type) && c.IsClass && !c.IsAbstract);
            //builder.RegisterApiControllers(list.ToArray());

            //var assemblies = config.Services.GetAssembliesResolver().GetAssemblies();

            foreach (var item in config.Services.GetAssembliesResolver().GetAssemblies())
            {
                if (item.FullName.IndexOf(".Controller", StringComparison.OrdinalIgnoreCase) > -1)
                {
                    builder.RegisterApiControllers(item);
                }
            }

            //builder.RegisterApiControllers(config.Services.GetAssembliesResolver().GetAssemblies())
            builder.RegisterWebApiFilterProvider(config);
            //builder.RegisterType<CustomActionFilter>()
            //    .AsWebApiActionFilterFor<TestController>()
            //    .InstancePerRequest();
            builder.Register(c => new Logger()).As <ILogger>().InstancePerRequest();
            //builder.Register(c=>c.)


            // Create and assign a dependency resolver for Web API to use.
            var container = builder.Build();

            config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
            app.UseAutofacMiddleware(container);
            app.UseAutofacWebApi(config);
            app.UseWebApi(config);
        }