Ejemplo n.º 1
0
        public void 配置映射_条件过滤()
        {
            AutoMapperModule.Initialize(new Type[] { typeof(EntityBaseDto), typeof(EntityOrderProfile) });
            var obj2 = obj1.MapTo <EntityBaseDto>();

            Assert.Equal(obj2.EntityOrders.Count(), 3);
        }
Ejemplo n.º 2
0
 protected void Application_Start()
 {
     AreaRegistration.RegisterAllAreas();
     GlobalConfiguration.Configure(WebApiConfig.Register);
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
     RouteConfig.RegisterRoutes(RouteTable.Routes);
     BundleConfig.RegisterBundles(BundleTable.Bundles);
     _containerProvider = new ContainerProvider(AutofacConfig.ConfigureAutofac());
     AutoMapperModule.Initialize();
 }
Ejemplo n.º 3
0
        public void 特性自动映射测试()
        {
            AutoMapperModule.Initialize(new Type[] { typeof(EntityBaseDto) });

            var obj2 = obj1.MapTo <EntityBaseDto>();

            Assert.Equal(obj2.Name, "张三");

            var obj3 = obj2.MapTo <Entity>();

            obj3.Name.ShouldBe("张三");
        }
Ejemplo n.º 4
0
        public void Profile配置映射_覆盖_Attribute映射()
        {
            AutoMapperModule.Initialize(new Type[] { typeof(EntityBaseDto), typeof(EntityProfile) });

            var obj1 = new Entity()
            {
                Id = 1, Name = "张三"
            };
            var obj2 = obj1.MapTo <EntityBaseDto>();

            Assert.Equal(obj2.Name, "张三");

            var obj3 = obj2.MapTo <Entity>();

            obj3.Name.ShouldBe("张三Test");
        }
Ejemplo n.º 5
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // 日志配置
            LogConfig();

            #region 跨域
#if DEBUG
            services.AddCors(options =>
                             options.AddPolicy("AllowSameDomain",
                                               builder => builder
                                               .WithOrigins("http://example.com", "http://www.contoso.com")
                                               .AllowAnyMethod()
                                               .AllowAnyHeader()
                                                                   //.AllowAnyOrigin()    //允许任何来源的主机访问(debug开发允许跨域)
                                               .AllowCredentials() //指定处理cookie
                                               ));
#else
            services.AddCors(options => options.AddPolicy("AllowSameDomain", builder => { }));
#endif
            #endregion

            services.Configure <CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded    = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            //替换控制器所有者  http://www.cnblogs.com/GuZhenYin/p/8301500.html
            services.Replace(ServiceDescriptor.Transient <IControllerActivator, ServiceBasedControllerActivator>());

            //https://docs.microsoft.com/zh-cn/aspnet/core/web-api/?view=aspnetcore-2.1
            services.Configure <ApiBehaviorOptions>(options =>
            {
                //options.SuppressConsumesConstraintForFormFileParameters = true;   //关闭请求multipart推断
                //options.SuppressInferBindingSourcesForParameters = true;          //关闭类型参数推断
                options.SuppressModelStateInvalidFilter = true;   //关闭自动验证对象属性并处理
            });

            //services.AddMvc(options =>
            //{
            //    options.Filters.Add<AuthorizationFilter>();
            //    options.Filters.Add<ActionFilter>();
            //    options.Filters.Add<ExceptionFilter>();
            //}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddControllersWithViews(options =>
            {
                options.Filters.Add <AuthorizationFilter>();
                options.Filters.Add <ActionFilter>();
                options.Filters.Add <ExceptionFilter>();
            }).AddNewtonsoftJson();

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title   = "MsSystem API"
                });

                //Determine base path for the application.
                var basePath = PlatformServices.Default.Application.ApplicationBasePath;
                //Set the comments path for the swagger json and ui.
                var xmlPath = Path.Combine(basePath, "ProjectNameTemplate.WebApi.xml");
                options.IncludeXmlComments(xmlPath);

                options.OperationFilter <OperationFilter>();
            });

            //TODO  这里修改成需要映射的类库集合
            var autpTypes = Assembly.Load("ProjectNameTemplate.WebApi").GetTypes().ToList();
            //autpTypes.AddRange(Assembly.Load("ProjectNameTemplate.Contract").GetTypes().ToList());
            autpTypes.AddRange(Assembly.Load("ProjectNameTemplate.Core").GetTypes().ToList());
            autpTypes.AddRange(Assembly.Load("ProjectNameTemplate.Application").GetTypes().ToList());
            AutoMapperModule.Initialize(autpTypes);

            //return new AutofacServiceProvider(InitContainerBuilder(services));//第三方IOC接管 core内置DI容器
        }