optimise codes

master
Young 7 months ago
parent cdf55587b3
commit 94309870a0

@ -5,6 +5,7 @@ using Infrastructure.Security;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
namespace Infrastructure.Extensions; namespace Infrastructure.Extensions;
@ -31,12 +32,12 @@ public static class AuthenticationSetup
return services; return services;
} }
services.AddSingleton<JwtSecurityTokenHandler>(); services.TryAddSingleton<JwtSecurityTokenHandler>();
services.AddScoped<IUserContext, UserContext>(); services.TryAddScoped<IUserContext, UserContext>();
services.AddSingleton<DefaultTokenHandler>(); services.TryAddSingleton<DefaultTokenHandler>();
services.AddSingleton<IEncryptionService, EncryptionService>(); services.TryAddSingleton<IEncryptionService, EncryptionService>();
services.AddSingleton<IPostConfigureOptions<JwtBearerOptions>, JwtBearerOptionsPostConfigureOptions>(); services.TryAddSingleton<IPostConfigureOptions<JwtBearerOptions>, JwtBearerOptionsPostConfigureOptions>();
services.AddSingleton<ITokenBuilder, TokenBuilder>(); services.TryAddSingleton<ITokenBuilder, TokenBuilder>();
var key = configuration["AUDIENCE_KEY"] ?? audienceOptions.Secret; var key = configuration["AUDIENCE_KEY"] ?? audienceOptions.Secret;
ArgumentException.ThrowIfNullOrEmpty(key); ArgumentException.ThrowIfNullOrEmpty(key);

@ -29,9 +29,9 @@ public static class ControllerSetup
}) })
.ConfigureApiBehaviorOptions(options => .ConfigureApiBehaviorOptions(options =>
{ {
options.InvalidModelStateResponseFactory = context => options.InvalidModelStateResponseFactory = _ =>
{ {
var message = new MessageData(false, "the input wat not valid", 400); var message = new MessageData(false, "the input value not valid", 400);
return new OkObjectResult(message.Serialize()); return new OkObjectResult(message.Serialize());
}; };
}); });

@ -1,5 +1,6 @@
using Infrastructure.Options; using Infrastructure.Options;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using StackExchange.Redis; using StackExchange.Redis;
namespace Infrastructure.Extensions; namespace Infrastructure.Extensions;
@ -16,7 +17,7 @@ public static class RedisSetup
return services; return services;
} }
services.AddSingleton<ConnectionMultiplexer>(provider => services.TryAddSingleton<ConnectionMultiplexer>(_ =>
{ {
var host = configuration["REDIS_HOST"] ?? redisOptions.Host; var host = configuration["REDIS_HOST"] ?? redisOptions.Host;
ArgumentException.ThrowIfNullOrEmpty(host); ArgumentException.ThrowIfNullOrEmpty(host);

Loading…
Cancel
Save