// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(Microsoft.Extensions.DependencyInjection.IServiceCollection services) { services.AddControllers(); // 添加MVC服务 services.AddControllersWithViews(); // 注册服务 #region 依赖注入 #region 注册方法注解 // Singleton:单例服务,从当前服务容器中获取这个类型的实例永远是同一个实例; // Scoped:每个作用域生成周期内创建一个实例; // Transient:每一次请求服务都创建一个新实例; #endregion services.AddSingleton <ILogService, SysLogService>(); //services.AddScoped<ILogService, SysLogService>(); //services.AddTransient<ILogService, SysLogService>(); services.AddTransient <IOperationTransient, Operation>(); services.AddScoped <IOperationScoped, Operation>(); services.AddSingleton <IOperationSingleton, Operation>(); services.AddSingleton <IOperationSingletonInstance>(new Operation(Guid.Empty)); services.AddTransient <OperationService, OperationService>(); var serviceCollection = new ServiceCollection() .AddTransient <ILogService, SysLogService>() .AddSingleton <ILogService, SysLogService>() .AddScoped <ILogService, SysLogService>(); #endregion }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(Microsoft.Extensions.DependencyInjection.IServiceCollection services) { services.AddSingleton <Microsoft.AspNetCore.Http.IHttpContextAccessor, Microsoft.AspNetCore.Http.HttpContextAccessor>(); services.AddSingleton <SearchValueTransformer>(); MyBlogCore.SqlFactory fac = CreateAppropriateFactory(); services.AddSingleton <MyBlogCore.SqlFactory>(fac); // This method configures the MVC services for the commonly used features with controllers with views. // This combines the effects of Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore(Microsoft.Extensions.DependencyInjection.IServiceCollection), // Microsoft.Extensions.DependencyInjection.MvcApiExplorerMvcCoreBuilderExtensions.AddApiExplorer(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), // Microsoft.Extensions.DependencyInjection.MvcCoreMvcCoreBuilderExtensions.AddAuthorization(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), // Microsoft.Extensions.DependencyInjection.MvcCorsMvcCoreBuilderExtensions.AddCors(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), // Microsoft.Extensions.DependencyInjection.MvcDataAnnotationsMvcCoreBuilderExtensions.AddDataAnnotations(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), // Microsoft.Extensions.DependencyInjection.MvcCoreMvcCoreBuilderExtensions.AddFormatterMappings(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), // Microsoft.Extensions.DependencyInjection.TagHelperServicesExtensions.AddCacheTagHelper(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), // Microsoft.Extensions.DependencyInjection.MvcViewFeaturesMvcCoreBuilderExtensions.AddViews(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder), // and Microsoft.Extensions.DependencyInjection.MvcRazorMvcCoreBuilderExtensions.AddRazorViewEngine(Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder). // To add services for pages call Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddRazorPages(Microsoft.Extensions.DependencyInjection.IServiceCollection) // services.AddControllersWithViews(); /// string cs = Configuration.GetConnectionString("DefaultConnection"); services.AddControllersWithViews( delegate(Microsoft.AspNetCore.Mvc.MvcOptions opts) { // opts.default } ).AddRazorRuntimeCompilation( delegate(Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.MvcRazorRuntimeCompilationOptions options) { // options.FileProviders.Clear(); options.FileProviders.Add(new DatabaseFileProvider(fac)); // https://github.com/aspnet/FileSystem/blob/master/src/FS.Embedded/EmbeddedFileProvider.cs // options.FileProviders.Add( new Microsoft.Extensions.FileProviders.EmbeddedFileProvider(typeof(Startup).Assembly) ); } ) ; // https://www.mikesdotnetting.com/article/301/loading-asp-net-core-mvc-views-from-a-database-or-other-location // https://stackoverflow.com/questions/38247080/using-razor-outside-of-mvc-in-net-core // https://github.com/dotnet/AspNetCore.Docs/issues/14593 //services.Configure<Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.MvcRazorRuntimeCompilationOptions>( // delegate(Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.MvcRazorRuntimeCompilationOptions options) // { // // Requires adding nuget Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation // // options.FileProviders.Clear(); // options.FileProviders.Add(new DatabaseFileProvider("cs")); // } //); services.AddOptions <Microsoft.AspNetCore.Builder.StaticFileOptions>() .Configure <Microsoft.AspNetCore.Http.IHttpContextAccessor, Microsoft.AspNetCore.Hosting.IWebHostEnvironment>( delegate(Microsoft.AspNetCore.Builder.StaticFileOptions options, Microsoft.AspNetCore.Http.IHttpContextAccessor httpContext, Microsoft.AspNetCore.Hosting.IWebHostEnvironment env) { options.FileProvider = new DomainSpecificFileProvider(httpContext, env); } ); } // End Sub ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(Microsoft.Extensions.DependencyInjection.IServiceCollection services) { var cadena = Configuration.GetConnectionString("DefaultConnection"); services.AddDbContext <GeneralContext>(opt => opt.UseSqlServer(cadena, b => b.MigrationsAssembly("Verduras"))); services.AddControllersWithViews(); #region configure strongly typed settings objects var appSettingsSection = Configuration.GetSection("AppSetting"); services.Configure <AppSetting>(appSettingsSection); #endregion #region Configure jwt authentication inteprete el token var appSettings = appSettingsSection.Get <AppSetting>(); var key = Encoding.ASCII.GetBytes(appSettings.Secret); services.AddAuthentication(x => { x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(x => { x.RequireHttpsMetadata = false; x.SaveToken = true; x.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(key), ValidateIssuer = false, ValidateAudience = false }; } ); #endregion //Agregar OpenApi Swagger services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = "School API", Description = "School API - ASP.NET Core Web API", TermsOfService = new Uri("https://cla.dotnetfoundation.org/"), Contact = new OpenApiContact { Name = "Unicesar", Email = string.Empty, Url = new Uri("https://github.com/borisgr04/CrudNgDotNetCore3"), }, License = new OpenApiLicense { Name = "Licencia dotnet foundation", Url = new Uri("https://www.byasystems.co/license"), } }); }); // In production, the Angular files will be served from this directory services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/dist"; }); }