public OxfordDictionaryService(
     IHttpClientFactory httpClientFactory,
     IOptions <OxfordDictionaryConfiguration> options)
 {
     _oxfordDictionaryConfiguration = options.Value;
     _httpClient = httpClientFactory.CreateClient("OxfordDictionaryService");
     _settings   = new System.Lazy <Newtonsoft.Json.JsonSerializerSettings>(() =>
     {
         var settings = new Newtonsoft.Json.JsonSerializerSettings();
         return(settings);
     });
 }
예제 #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            Configuration.GetSection("CollegeApiConfirguration").Bind(_collegeApiConfirguration);

            services.AddDbContext <CollegeContext>(c =>
                                                   c.UseSqlServer(Configuration.GetConnectionString("CollegeConnection")));

            services.AddSwaggerDocument(document =>
            {
                document.SerializerSettings = new JsonSerializerSettings
                {
                    ContractResolver = new DefaultContractResolver
                    {
                        NamingStrategy = new CamelCaseNamingStrategy()
                    }
                };
                document.OperationProcessors.Add(new OperationSecurityScopeProcessor("JWT Token"));
                string[] scopes = { "Authorization" };
                document.DocumentProcessors.Add(new SecurityDefinitionAppender("JWT Token", scopes,
                                                                               new OpenApiSecurityScheme
                {
                    Type        = OpenApiSecuritySchemeType.ApiKey,
                    Name        = "Authorization",
                    Description = "Copy 'Bearer ' + valid JWT token into field",
                    In          = OpenApiSecurityApiKeyLocation.Header
                }));
            });

            services.AddCors(options =>
            {
                options.AddPolicy("AllRequests", builder =>
                {
                    builder.AllowAnyHeader()
                    .AllowAnyMethod()
                    .WithOrigins(
                        _collegeApiConfirguration.SpaSpellingClientBaseUrl,
                        "https://spell-it.co.uk",
                        "https://www.spell-it.co.uk"
                        )
                    .AllowCredentials();
                });
            });

            services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority            = _collegeApiConfirguration.IdentityBaseUrl;
                options.ApiName              = "spelling-api";
                options.RequireHttpsMetadata = true;
            });

            services.AddControllersWithViews(options =>
            {
                var policy = new AuthorizationPolicyBuilder()
                             .RequireAuthenticatedUser()
                             .Build();
                options.Filters.Add(new AuthorizeFilter(policy));

                //options.OutputFormatters
            });


            services.AddScoped(typeof(IAsyncRepository <>), typeof(EfRepository <>));
            services.AddScoped <ICollegeService, CollegeService>();
            services.AddScoped <ICollegeRepository, CollegeRepository>();
            services.AddScoped <IAppUserRepository, AppUserRepository>();
            services.AddScoped <IHomeWorkRepository, HomeWorkRepository>();
            services.AddScoped <IStandardListRepository, StandardListRepository>();
            services.Configure <SendGridConfiguration>(this.Configuration.GetSection("SendGridConfiguration"));
            services.Configure <OxfordDictionaryConfiguration>(this.Configuration.GetSection("OxfordDictionaryConfiguration"));
            services.AddTransient <IEmailSender, EmailSender>();
            services.AddHttpClient("identityClient", client =>
            {
                client.BaseAddress = new Uri(_collegeApiConfirguration.IdentityBaseUrl);
            });
            services.AddHttpClient("OxfordDictionaryService", client =>
            {
                var oxfordDictionaryConfiguration = new OxfordDictionaryConfiguration();
                Configuration.GetSection("OxfordDictionaryConfiguration").Bind(oxfordDictionaryConfiguration);
                client.BaseAddress = new Uri(oxfordDictionaryConfiguration.BaseUrl);
            });

            services.AddScoped <IOxfordDictionaryService, OxfordDictionaryService>();
        }