public void SetUp() { _logger = Substitute.For <ILogger <UserRepository> >(); var consulProvider = ConsulConfigProvider.LoadConsulConfig("http://127.0.0.1:18500/v1/kv/", new[] { "MockSite" }); _config = new ConfigurationBuilder() .AddJsonFile(consulProvider, "test.json", true, false) .Build(); _helper = new SqlConnectionHelper(Substitute.For <ILoggerProvider>()); MethodTimeLogger.SetLogger(Substitute.For <ILoggerProvider>()); }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddCors(options => { options.AddPolicy(CorsPolicy, builder => builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader()); }); // 加入 OpenTracing services.AddOpenTracing().AddSingleton <ITracer>(serviceProvider => { const string serviceName = "MockSite.Web"; var tracer = new Tracer.Builder(serviceName) .WithSampler(new ConstSampler(true)) .Build(); // 註冊 Jaeger tracer GlobalTracer.Register(tracer); return(tracer); }); services.AddMvc().AddNewtonsoftJson(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "MockSite API", Version = "v1" }); c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Description = "Please enter JWT with Bearer into field", Name = "Authorization", Type = SecuritySchemeType.ApiKey }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Id = "Bearer", //The name of the previously defined security scheme. Type = ReferenceType.SecurityScheme } }, new List <string>(0) } }); }); // In production, the React files will be served from this directory services.AddSpaStaticFiles(config => { config.RootPath = "ClientApp/build"; }); // Configure JWT authentication services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateIssuerSigningKey = true, ValidIssuer = _configuration.GetSection(AppSetting.JwtIssuerKey).Value, ValidAudience = _configuration.GetSection(AppSetting.JwtAudienceKey).Value, IssuerSigningKey = new SymmetricSecurityKey( Encoding.UTF8.GetBytes(_configuration.GetSection(AppSetting.JwtSecretKey).Value) ) }; }); // Configure DI for application services services.AddScoped <IUserService, UserService>(); var consulIp = _configuration.GetSection(ConsulConfigConst.ConsulIp).Value; var consulPort = _configuration.GetSection(ConsulConfigConst.ConsulPort).Value; var consulModule = _configuration.GetSection(ConsulConfigConst.ConsulModule).Value; var consulProvider = ConsulConfigProvider.LoadConsulConfig($"http://{consulIp}:{consulPort}/v1/kv/", consulModule.Split(',')); var consul = new ConfigurationBuilder() .AddJsonFile(consulProvider, "test.json", true, false) .Build(); // Register gRPC Service var userHost = $"{consul[HostNameConst.TestKey]}:{consul[PortConst.TestKey]}"; var tracingInterceptor = new ClientTracingInterceptor(GlobalTracer.Instance); services.AddSingleton(new Message.UserService.UserServiceClient( CreateKeepAliveWithoutCallChannel(userHost).Intercept(tracingInterceptor)) ); services.AddSingleton(new CurrencyService.CurrencyServiceClient( CreateKeepAliveWithoutCallChannel(userHost).Intercept(tracingInterceptor)) ); services.AddSingleton(new LocalizationService.LocalizationServiceClient( CreateKeepAliveWithoutCallChannel(userHost).Intercept(tracingInterceptor)) ); }