/// <summary> /// /// </summary> /// <param name="services"></param> /// <returns></returns> public static IServiceCollection AddCorsOption(this IServiceCollection services) { if (EasySharpServices.IsInitialized == false) { EasySharpServices.Services = services; EasySharpServices.Initialize(); } Configuration = EasySharpServices.Builder(); var options = new CorsOptions(); Configuration.GetSection(nameof(CorsOptions)).Bind(options); //if Links=null, set links array to empty array var linksOption = options.Links ?? new string[] { }; var policyName = options.Name; if (options.Enabled) { string[] clientUrls = linksOption.ToArray(); services.AddCors(opt => { opt.AddPolicy(policyName, fbuilder => fbuilder.WithOrigins(clientUrls) .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); }); } return(services); }
public static IServiceCollection AddCacheable(this IServiceCollection services) { if (EasySharpServices.IsInitialized == false) { EasySharpServices.Services = services; EasySharpServices.Initialize(); } Configuration = EasySharpServices.Builder(); var options = new Cacheable(); Configuration.GetSection(nameof(Cacheable)).Bind(options); var redisOptions = options.Redis ?? new RedisOptions(); var localStorageOptions = options.LocalStorage ?? new LocalStorageOptions(); if (redisOptions.Enable == true) { services.RedisCache(redisOptions); } if (localStorageOptions.Enable == true) { services.AddLocalStorage(Configuration); } return(services); }
public static IServiceCollection AddEfCore <TContext>(this IServiceCollection services) where TContext : DbContext { if (EasySharpServices.IsInitialized == false) { EasySharpServices.Services = services; EasySharpServices.Initialize(); } Configuration = EasySharpServices.Builder(); var option = new EfCoreOptions(); Configuration.GetSection(nameof(EfCoreOptions)).Bind(option); services.AddDbContext <TContext>( options => options.UseSqlServer( option.ConnectionString )); return(services); }
public static IServiceCollection AddEasySharp(this IServiceCollection services, params Type[] types) { var assemblies = types.Select(type => type.GetTypeInfo().Assembly); foreach (var assembly in assemblies) { services.AddMediatR(assembly); } services.AddScoped <ICommandBus, CommandBus>(); services.AddScoped <IQueryBus, QueryBus>(); services.AddScoped <IEventBus, EventBus>(); services.AddScoped(typeof(IPipelineBehavior <,>), typeof(ValidationBehavior <,>)); EasySharpServices.Services = services; if (EasySharpServices.IsInitialized == false) { EasySharpServices.Initialize(); } Configuration = EasySharpServices.Builder(); services.AddOptions(); services .AddMvc(opt => { opt.Filters.Add <ExceptionFilter>(); opt.EnableEndpointRouting = false; }) .AddNewtonsoftJson(jopt => jopt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore) .AddFluentValidation(cfg => { cfg.RegisterValidatorsFromAssemblies(assemblies); }); return(services); }
public static IServiceCollection AddDocs(this IServiceCollection services) { if (EasySharpServices.IsInitialized == false) { EasySharpServices.Services = services; EasySharpServices.Initialize(); } Configuration = EasySharpServices.Builder(); var options = new SwaggerOptions(); Configuration.GetSection(nameof(SwaggerOptions)).Bind(options); var dt = _registry.TryAdd(RegistryName, true); if (!options.Enabled || !dt) { return(services); } services.AddSingleton(options); Uri termsOfService = new Uri(options.TermsOfService); Uri contactUrl = new Uri(options.SecurityOptions.Contact.Url); Uri licenseUrl = new Uri(options.SecurityOptions.License.Url); services.AddSwaggerGen(c => { c.SwaggerDoc(options.Name, new OpenApiInfo { Version = options.Name, Title = options.Title, Description = options.Description, TermsOfService = termsOfService, Contact = new OpenApiContact { Name = options.SecurityOptions.Contact.Name, Email = options.SecurityOptions.Contact.Email, Url = contactUrl, }, License = new OpenApiLicense { Name = options.SecurityOptions.License.Name, Url = licenseUrl } }); c.CustomSchemaIds(type => type.ToString()); if (options.SecurityOptions.XmlDoc) { // curret project name not libraryName var commentsFileName = $"{Assembly.GetEntryAssembly().GetName().Name}.XML"; var commentsFile = Path.Combine(System.AppContext.BaseDirectory, commentsFileName); //FileHelpers.CreateFileIfDoesNotExist(commentsFile); c.IncludeXmlComments(commentsFile); } if (options.IncludeSecurity) { c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme { Type = SecuritySchemeType.OAuth2, Flows = new OpenApiOAuthFlows { AuthorizationCode = new OpenApiOAuthFlow { AuthorizationUrl = new Uri(options.SecurityOptions.AuthorityURL), TokenUrl = new Uri(options.SecurityOptions.TokenUrl), RefreshUrl = new Uri(options.SecurityOptions.TokenUrl), Scopes = new Dictionary <string, string> { { options.SecurityOptions.Scope.Name, options.SecurityOptions.Scope.Description }, } } } }); c.OperationFilter <AuthorizeCheckOperationFilter>(options); } }); if (options.IncludeSecurity) { services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) .AddIdentityServerAuthentication(x => { x.Authority = options.SecurityOptions.Authority; x.ApiName = options.SecurityOptions.ApiName; x.SupportedTokens = SupportedTokens.Both; x.ApiSecret = options.SecurityOptions.ClientSecret; x.RequireHttpsMetadata = bool.Parse(options.SecurityOptions.RequireHttpsMetadata); x.SaveToken = true; x.EnableCaching = true; x.CacheDuration = TimeSpan.FromMinutes(10); }); } return(services); }