예제 #1
0
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Web API routes
            config.MapHttpAttributeRoutes();

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

            // Resolving the dependencies
            var container = RegisterModules.GetUnityContainer();

            config.DependencyResolver = new UnityResolver(container);

            var json = config.Formatters.JsonFormatter;

            json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
            config.Formatters.Remove(config.Formatters.XmlFormatter);

            // Output to be returned in JSON format always
            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

            //Register exception handler
            config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());
        }
예제 #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddApplicationInsightsTelemetry(Configuration);
            //Add Mvc
            services.AddMvc();

            //add connection string
            services.AddDbContext <BlogSimpleContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            //Load appsetting to blogsimple config
            services.ConfigureStartupConfig <BlogSimpleConfig>(Configuration.GetSection("BlogSimple"));

            //Config DI & IOC
            return(new AutofacServiceProvider(RegisterModules.RegisterModule(services)));
        }
 private static void RegisterSystemModules(ContainerBuilder builder)
 {
     // 注册系统定义的模块
     RegisterModules.RegisterSystemModules(builder);
 }