public void RegisterAll()
        {
            #region 测试一次注册全部
            string assemblyName = GetType().Assembly.GetName().ToString();
            RepositoryContainer.RegisterAll(assemblyName);

            var uRep       = RepositoryContainer.Resolve <UserRepAsync>();
            var uRepAsync  = RepositoryContainer.Resolve <UserRepAsync>();
            var uRep2      = RepositoryContainer.Resolve <User2RepositoryAsync>();
            var uRepAsync2 = RepositoryContainer.Resolve <User2RepositoryAsync>();
            Assert.IsNotNull(uRep);
            Assert.IsNotNull(uRepAsync);
            Assert.IsNotNull(uRep2);
            Assert.IsNotNull(uRepAsync2);
            #endregion

            #region 测试replace
            //新注册,应该要跟原对象不同
            RepositoryContainer.RegisterAll(assemblyName);

            Assert.AreNotEqual(uRep, RepositoryContainer.Resolve <UserRepAsync>());
            Assert.AreNotEqual(uRepAsync, RepositoryContainer.Resolve <UserRepAsync>());
            Assert.AreNotEqual(uRep2, RepositoryContainer.Resolve <User2RepositoryAsync>());
            Assert.AreNotEqual(uRepAsync2, RepositoryContainer.Resolve <User2RepositoryAsync>());
            #endregion
        }
Example #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            //注册MongoDB仓储(如果不用可以注释掉)
            RepositoryContainer.RegisterAll(AutofacModuleRegister.GetAllAssembliesName());
            services.AddMvc(option =>
            {
                option.Filters.Add(typeof(ExceptionLogAttribute));
                option.Filters.Add(typeof(RequestLogAttribute));
                option.Filters.Add(typeof(ResponseLogAttribute));
                option.MaxModelValidationErrors = 100;
            })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
            .AddJsonOptions(option =>
            {
                option.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                //忽略循环引用
                option.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                //不使用驼峰样式的key
                option.SerializerSettings.ContractResolver = new DefaultContractResolver();
                //增加参数自动去除前后空格转换器
                option.SerializerSettings.Converters.Add(new TrimmingConverter());
            });
            services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddApiVersioning(options =>
            {
                //当设置为 true 时, API 将返回响应标头中支持的版本信息
                options.ReportApiVersions = true;
                //此选项将用于不提供版本的请求。默认情况下, 假定的 API 版本为1.0。
                options.AssumeDefaultVersionWhenUnspecified = true;
                //此选项用于指定在请求中未指定版本时要使用的默认 API 版本。这将默认版本为1.0。
                options.DefaultApiVersion = new Microsoft.AspNetCore.Mvc.ApiVersion(1, 0);
            });
            //解决跨域(如果不用可以注释掉)
            services.AddCors(options =>
            {
                options.AddPolicy("EnableCrossDomain", builder =>
                {
                    builder.AllowAnyOrigin()//允许任何来源的主机访问
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials();//指定处理cookie
                });
            });
            //注册权限验证
            services.AddScoped <AuthenticationAttribute>();

            //注入 Quartz调度类(如果不用可以注释掉)
            services.AddSingleton <QuartzStartup>();
            services.AddSingleton <ISchedulerFactory, StdSchedulerFactory>();
            services.AddSingleton <IJobFactory, IOCJobFactory>();
            //注入 HttpClientHelp(如果不用可以注释掉)
            services.AddTransient <HttpClientHelp>();
            services.Configure <ApiBehaviorOptions>(options => {
                //禁用.net core webapi 项目本身的模型参数绑定验证体系
                options.SuppressModelStateInvalidFilter = true;
            });
            return(RegisterAutofac(services));//注册Autofac
        }
Example #3
0
        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// </summary>
        /// <param name="services"></param>
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            //注册MongoDB仓储(如果不用可以注释掉)
            RepositoryContainer.RegisterAll(AutofacModuleRegister.GetAllAssembliesName());
            services.AddControllers();
            services.AddMvc(option =>
            {
                option.Filters.Add(typeof(ExceptionLogAttribute));
                option.Filters.Add(typeof(RequestLogAttribute));
                option.Filters.Add(typeof(ResponseLogAttribute));
                option.MaxModelValidationErrors = 100;
            })
            .AddNewtonsoftJson(option =>
            {
                option.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                //忽略循环引用
                option.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                //不使用驼峰样式的key
                option.SerializerSettings.ContractResolver = new DefaultContractResolver();
                //增加参数自动去除前后空格转换器
                option.SerializerSettings.Converters.Add(new TrimmingConverter());
            });

            #region 解决跨域

            //解决跨域(如果不用可以注释掉)
            services.AddCors(options =>
            {
                options.AddPolicy("EnableCrossDomain", builder =>
                {
                    builder.AllowAnyOrigin()//允许任何来源的主机访问
                    //builder.WithOrigins(APIConfig.GetInstance().RequestSource)
                    .AllowAnyMethod()
                    .AllowAnyHeader();
                    //.AllowCredentials();//指定处理cookie
                });
            });

            #endregion

            services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            #region 接口版本控制

            services.AddApiVersioning(options =>
            {
                //当设置为 true 时, API 将返回响应标头中支持的版本信息
                options.ReportApiVersions = true;
                //此选项将用于不提供版本的请求。默认情况下, 假定的 API 版本为1.0。
                options.AssumeDefaultVersionWhenUnspecified = true;
                //此选项用于指定在请求中未指定版本时要使用的默认 API 版本。这将默认版本为1.0。
                options.DefaultApiVersion = new Microsoft.AspNetCore.Mvc.ApiVersion(1, 0);
                options.ApiVersionReader  = new UrlSegmentApiVersionReader();
            })
            .AddVersionedApiExplorer(options =>
            {
                //版本名的格式:v+版本号
                options.GroupNameFormat = "'v'V";
                options.AssumeDefaultVersionWhenUnspecified = true;
            });

            #endregion

            #region 注册Swagger服务

            services.AddSwaggerGen(c =>
            {
                //多版本控制
                foreach (var item in Provider.ApiVersionDescriptions)
                {
                    //添加文档信息
                    c.SwaggerDoc(item.GroupName, new OpenApiInfo
                    {
                        Title       = "绝影框架",
                        Version     = item.ApiVersion.ToString(),
                        Description = "ASP.NET CORE 3.1 WebApi",
                        Contact     = new OpenApiContact
                        {
                            Name  = "PoJun",
                            Email = "*****@*****.**",
                            Url   = new Uri("https://github.com/YGeneral/PoJun.Shadow")
                        }
                    });
                }

                c.DocumentFilter <SwaggerEnumFilter>();

                #region 读取xml信息

                //启用xml注释. 该方法第二个参数启用控制器的注释,默认为false.
                c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"), true);
                c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, $"PoJun.Shadow.Api.ContractModel.xml"), true);
                c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, $"PoJun.Shadow.ContractModel.xml"), true);
                c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, $"PoJun.Shadow.Enum.xml"), true);
                c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, $"PoJun.Shadow.Code.xml"), true);
                c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, $"PoJun.Shadow.Api.IService.xml"), true);
                c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, $"PoJun.Shadow.Api.Service.xml"), true);

                #endregion
            });

            #endregion

            services.AddMvc();
            //注册权限验证
            services.AddScoped <AuthenticationAttribute>();

            #region Quartz调度框架注册

            //注入Quartz调度类(如果不用可以注释掉)
            services.AddSingleton <QuartzStartup>();
            services.AddSingleton <ISchedulerFactory, StdSchedulerFactory>();
            services.AddSingleton <IJobFactory, IOCJobFactory>();

            #endregion

            //注入 HttpClientHelp(如果不用可以注释掉)
            services.AddTransient <HttpClientHelp>();

            #region 自定义job注册

            //注入自定义job
            services.AddSingleton <TestJob>();

            #endregion

            services.Configure <ApiBehaviorOptions>(options =>
            {
                //禁用.net core webapi 项目本身的模型参数绑定验证体系
                options.SuppressModelStateInvalidFilter = true;
            });

            services.AddHttpClient();
            services.AddHttpClient("base").AddTransientHttpErrorPolicy(x => x.RetryAsync(3)); //添加重试策略。 若请求失败,最多可重试三次;
            return(RegisterAutofac(services));                                                //注册Autofac
        }
Example #4
0
        public InsertCompared_UnitTest()
        {
            string assemblyName = GetType().Assembly.GetName().ToString();

            RepositoryContainer.RegisterAll(assemblyName);
        }
Example #5
0
 // This method gets called by the runtime. Use this method to add services to the container.
 public void ConfigureServices(IServiceCollection services)
 {
     //注册MongoDB仓储
     RepositoryContainer.RegisterAll("PoJun.Test.API.Reposiory, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
     services.AddMvc();
 }