Пример #1
0
        public void IStandardError_Target_IsCallerMemberName_Test()
        {
            var error = StandardException.Caused("Code", "xUnit");

            Assert.Equal(nameof(IStandardError_Target_IsCallerMemberName_Test), error.Target);
            Assert.Equal("xUnit", error.Message);
        }
Пример #2
0
        public void IStandardError_Can_Json_Serialize_Test()
        {
            var exception = StandardException.Caused("Code", "xunit").Append(StandardException.Caused("InnerCode", "details", "InnerTarget"));

            var json = JsonConvert.SerializeObject(exception);

            Assert.False(string.IsNullOrEmpty(json));
        }
Пример #3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddJsonOptions(x =>
            {
                x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                x.SerializerSettings.DateFormatString      = "yyyy-MM-dd HH:mm:ss";
            });

            var connection = Configuration.GetConnectionString("DefaultConnection");

            if (string.IsNullOrWhiteSpace(connection))
            {
                throw StandardException.Caused("10000", "未配置数据库连接字符串");
            }
            //依赖注入
            services.AddScoped <IHospitalService>(sp => new HospitalService(connection));
            #region Swagger

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new Info
                {
                    Version = "v1",
                    Title   = "his服务接口"
                });

                var basePath = PlatformServices.Default.Application.ApplicationBasePath;
                var xmlPath  = Path.Combine(basePath, "DapperTast.xml");
                options.IncludeXmlComments(xmlPath);
                options.DescribeAllEnumsAsStrings();
                options.DocInclusionPredicate((docName, description) => true);
                //options.OperationFilter<HttpHeaderOperation>(); // 添加httpHeader参数
            });
            #endregion
            #region 1认证
            //services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            //.AddJwtBearer(options =>
            //{
            //    options.RequireHttpsMetadata = false; //获取或设置元数据地址或权限是否需要HTTPS。默认值为true。这应该仅在开发环境中禁用。
            //    options.SaveToken = true;
            //    options.TokenValidationParameters = new TokenValidationParameters
            //    {
            //        ValidIssuer = "RayPI",
            //        ValidAudiences = new[] { "yyy", "RayPI" },
            //        ValidateAudience = true,
            //        ValidateIssuer = true,//是否验证Issuer
            //        ValidateIssuerSigningKey = true,//是否验证SecurityKey
            //        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["SecurityKey"])),
            //        ValidateLifetime = true,//是否验证失效时间

            //    };
            //});

            #endregion
        }
Пример #4
0
 public void IStandardError_Fluent_API_Feature_Test()
 {
     Assert.Throws <StandardException>(() => {
         StandardException.Caused("Code", "xunit").Append(StandardException.Caused("InnerCode", "details")).Throw();
     });
 }
Пример #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddJsonOptions(x =>
            {
                x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                x.SerializerSettings.DateFormatString      = "yyyy-MM-dd HH:mm:ss";
            });
            //依赖注入

            var connection = Configuration.GetConnectionString("DefaultConnection");

            if (string.IsNullOrWhiteSpace(connection))
            {
                throw StandardException.Caused("10000", "未配置数据库连接字符串");
            }
            //依赖注入
            services.AddScoped <IDataBaseHelpService>(sp => new DataBaseHelpService(connection));
            services.AddScoped <IDataBaseSqlServerService>(sp => new DataBaseSqlServerService(connection));
            services.AddScoped <IGrammarNewService, GrammarNewService>();
            services.AddScoped <IWebServiceBasic, WebServiceBasic>();
            services.AddScoped <IWebServiceBasicService, WebServiceBasicService>();
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            #region Swagger

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new Info
                {
                    Version = "v1",
                    Title   = "消息管理 API"
                });

                var basePath = PlatformServices.Default.Application.ApplicationBasePath;
                var xmlPath  = Path.Combine(basePath, "MedicalInsurance.xml");
                options.IncludeXmlComments(xmlPath);
                options.DescribeAllEnumsAsStrings();//枚举呈现为字符串
                options.DocInclusionPredicate((docName, description) => true);
                options.AddSecurityDefinition("Bearer", new ApiKeyScheme {
                    In = "header", Description = "请输入带有Bearer的Token", Name = "Authorization", Type = "apiKey"
                });
                options.AddSecurityRequirement(new Dictionary <string, IEnumerable <string> > {
                    { "Bearer", Enumerable.Empty <string>() }
                });
                //options.OperationFilter<HttpHeaderOperation>(); // 添加httpHeader参数
            });
            #endregion
            #region 认证
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.RequireHttpsMetadata      = false; //获取或设置元数据地址或权限是否需要HTTPS。默认值为true。这应该仅在开发环境中禁用。
                options.SaveToken                 = true;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidIssuer              = "User",
                    ValidAudiences           = new[] { "yyy", "User" },
                    ValidateAudience         = true,
                    ValidateIssuer           = true, //是否验证Issuer
                    ValidateIssuerSigningKey = true, //是否验证SecurityKey
                    IssuerSigningKey         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["SecurityKey"])),
                    ValidateLifetime         = true, //是否验证失效时间
                };
            });

            #endregion
            //services.AddCors(c =>
            //{
            //    c.AddPolicy("AllowAnyOrigin", policy =>
            //    {
            //        policy.AllowAnyOrigin()//允许任何源
            //        .AllowAnyMethod()//允许任何方式
            //        .AllowAnyHeader()//允许任何头
            //        .AllowCredentials();//允许cookie
            //    });
            //c.AddPolicy("AllowSpecificOrigin", policy =>
            //{
            //    policy.WithOrigins("http://localhost:8083")
            //    .WithMethods("GET", "POST", "PUT", "DELETE")
            //    .WithHeaders("authorization");
            //});
            //});
        }