Ho riscontrato un problema dopo aver integrato un'applicazione di nucleo 2.0 ASP.Net esistente in elementi di progetto di autenticazione appena creati basati anche su ASP.Net Core 2.0.
dopo alcuni aggiustamenti e poi una build di successo, nel browser ho ricevuto l'errore successivo:
Si è verificata un'eccezione non gestita durante l'elaborazione della richiesta.
InvalidOperationException: impossibile risolvere il servizio per tipo "SpaServices.ApplicationDbContext" durante il tentativo di attivazione
L'autenticazione deve utilizzare un database separato che non è ancora impalcato
My Startup.cs è il seguente:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<SchoolContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc()
.AddRazorPagesOptions(options =>
{
options.Conventions.AuthorizeFolder("/Account/Manage");
options.Conventions.AuthorizePage("/Account/Logout");
});
// Register no-op EmailSender used by account confirmation and password reset during development
// For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=532713
services.AddSingleton<IEmailSender, EmailSender>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
#region webpack-middleware-registration
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebpackDevMiddleware();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
// Call UseWebpackDevMiddleware before UseStaticFiles
app.UseStaticFiles();
#endregion
#region mvc-routing-table
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
});
#endregion
}
E il mio costruttore è il seguente:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
Ho aggiornato il costruttore per prevedere esplicitamente un tipo che il contenitore sa come risolvere:
public ApplicationDbContext(DbContextOptions options)
: base(options)
{
}
Senza successo, qualche pensiero?
È necessario includere quanto segue nel metodo ConfigureServices ():
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("[Your Connection String Identifier]")));
Al momento, hai aggiunto solo un DbContext "SchoolContext"