コード例 #1
0
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());

            var container = new UnityContainer();

            container.RegisterServiceDAL();
            container.RegisterServiceBAL();
            container.RegisterType <AuthorizationModuleDbContext>(new HierarchicalLifetimeManager(), new InjectionConstructor("Default"));
            var Mappingconfigure = MappingRegister.Initlize();

            container.RegisterInstance <IMapper>(Mappingconfigure.CreateMapper());

            config.DependencyResolver = new UnityResolver(container);

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
                );
        }
コード例 #2
0
ファイル: Startup.cs プロジェクト: jhyuzhijian/netcore3.0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//应用程序所在目录
            //项目生成的xml文档
            string xmlFile       = Assembly.GetExecutingAssembly().GetName().Name + ".xml";
            string xmlPath       = Path.Combine(AppContext.BaseDirectory, xmlFile);
            string xmlEntityFile = Path.Combine(basePath, "Core_Entity.xml");
            string xmlEntityPath = Path.Combine(AppContext.BaseDirectory, xmlEntityFile);

            services.AddControllers();
            //注入DBContext
            services.AddDbContextToService <SqlServerDbContext>(DataBaseTypeEnum.SqlServer, Configuration.GetConnectionString("SqlConnection"));
            #region 通过dll注入服务
            services.AddSingleton <Core_WebAPI.ServerTest>();
            //services.AddDataService();
            #endregion
            // json.net注入
            services.AddControllers()
            .AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.ContractResolver     = new DefaultContractResolver();
                options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;     // 设置时区为 UTC
                options.SerializerSettings.DateFormatString     = "yyyy-MM-dd HH:mm:ss";
            });
            #region swagger文档注入
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo()
                {
                    Title       = "MyApi",
                    Version     = "v1",
                    Description = "A simple example ASP.NET Core Web API",
                    Contact     = new OpenApiContact
                    {
                        Name  = string.Empty,
                        Email = string.Empty,
                        Url   = new Uri("http://www.baidu.com")
                    },
                    License = new OpenApiLicense
                    {
                        Name = "Nothing",
                        Url  = new Uri("https://example.com/license")
                    }
                });
                //c.CustomSchemaIds(type => type.FullName);//如有相同类名则取消此行注释
                //具有冲突(重复命名方法)取第一个
                c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
                c.IncludeXmlComments(xmlPath);
                c.IncludeXmlComments(xmlEntityPath);
            });
            #endregion
            // 自动注入AutoMapper映射规则
            services.AddAutoMapper(MappingRegister.MapTypes());
            services.AddHttpClient();
        }
コード例 #3
0
        private static IUnityContainer BuildUnityContainer()
        {
            var container = new UnityContainer();

            container.RegisterType <AuthorizationModuleDbContext>(new HierarchicalLifetimeManager(), new InjectionConstructor("Default"));
            container.RegisterServiceBAL();
            container.RegisterServiceDAL();
            var Mappingconfigure = MappingRegister.Initlize();

            container.RegisterInstance <IMapper>(Mappingconfigure.CreateMapper());
            return(container);
        }