// Use ConnectionStrings in configuration services.AddDbContext<BloggingContext>(options => options.UseSqlServer(ConnectionStrings.BloggingDatabase)); }
Unhandled exception. System.InvalidOperationException: Unable to resolve service fortype'ExampleProject.ConnectionStrings'while attempting to activate 'ExampleProject.Startup'. at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass12_0.<UseStartup>b__0(HostBuilderContext context, IServiceCollection services) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build() at ExampleProject.Program.Main(String[] args) in C:\repos\ExampleProject\Program.cs:line 21
publicvoidConfigureServices(IServiceCollection services) { // configure the IdentitySettings for the DI container services.Configure<IdentitySettings>(Configuration.GetSection("Identity"));
// Register the implementations using their implementation name services.AddScoped<FakeIdentityService>(); services.AddScoped<RealIdentityService>();
// Retrieve the IdentitySettings at runtime, and return the correct implementation services.AddScoped<IIdentityService>(ctx => { var identitySettings = ctx.GetRequiredService<IdentitySettings>(); return identitySettings.UseFakeIdentity ? ctx.GetRequiredService<FakeIdentityService>() : ctx.GetRequiredService<RealIdentityService>(); } }); }
publicvoidConfigureServices(IServiceCollection services) { // configure the IdentitySettings for the DI container services.Configure<IdentitySettings>(Configuration.GetSection("Identity"));
// "recreate" the strongly typed settings and manually bind them var identitySettings = new IdentitySettings(); Configuration.GetSection("Identity").Bind(identitySettings)
// conditionally register the correct service if(identitySettings.UseFakeIdentity) { services.AddScoped<IIdentityService, FakeIdentityService>(); } else { services.AddScoped<IIdentityService, RealIdentityService>(); } }
publicvoidConfigureServices(IServiceCollection services) { // configure the ConnectionStrings for the DI container services.Configure<ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
// directly retrieve setting instead of using strongly-typed options var connectionString = Configuration["ConnectionString:BloggingDatabase"];
public class ConfigureIdentityServerOptions : IConfigureNamedOptions<IdentityServerAuthenticationOptions> { readonly IdentitySettings _identitySettings; publicConfigureIdentityServerOptions(IdentitySettings identitySettings) { _identitySettings = identitySettings; _hostingEnvironment = hostingEnvironment; }
publicvoidConfigure(string name, IdentityServerAuthenticationOptions options) { // Only configure the options if this is the correct instance if (name == IdentityServerAuthenticationDefaults.AuthenticationScheme) { // Use the values from strongly-typed IdentitySettings object options.Authority = _identitySettings.ServerFullPath; options.ApiName = _identitySettings.ApiName; } }
// This won't be called, but is required for the IConfigureNamedOptions interface publicvoidConfigure(IdentityServerAuthenticationOptions options) => Configure(Options.DefaultName, options); }