.net Core 6 - 尝试激活时无法解析服务类型

Posted

技术标签:

【中文标题】.net Core 6 - 尝试激活时无法解析服务类型【英文标题】:.net Core 6 - Unable to resolve service for type while attempting to activate 【发布时间】:2021-12-25 18:04:11 【问题描述】:

在我的 ASP.NET Core 应用程序中,我收到以下错误:

InvalidOperationException:无法解析服务类型 尝试激活时出现“mvc_net6.Service.DB_Context” 'mvc_net6.Controllers.StudentController'。

StudentController StudentController 是通过 Scaffolds 创建到视图的,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using mvc_net6.Models;
using mvc_net6.Service;

namespace mvc_net6.Controllers

    public class StudentController : Controller
    
        private readonly DB_Context _context;

        public StudentController(DB_Context context)
        
            _context = context;
        

        // GET: Student
        public async Task<IActionResult> Index()
        
            return View(await _context.Students.ToListAsync());
        

        // GET: Student/Details/5
        public async Task<IActionResult> Details(int? id)
        
            if (id == null)
            
                return NotFound();
            

            var student = await _context.Students
                .FirstOrDefaultAsync(m => m.Id == id);
            if (student == null)
            
                return NotFound();
            

            return View(student);
        

        // GET: Student/Create
        public IActionResult Create()
        
            return View();
        

        // POST: Student/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,Name,CPF,Email")] Student student)
        
            if (ModelState.IsValid)
            
                _context.Add(student);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            
            return View(student);
        

        // GET: Student/Edit/5
        public async Task<IActionResult> Edit(int? id)
        
            if (id == null)
            
                return NotFound();
            

            var student = await _context.Students.FindAsync(id);
            if (student == null)
            
                return NotFound();
            
            return View(student);
        

        // POST: Student/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("Id,Name,CPF,Email")] Student student)
        
            if (id != student.Id)
            
                return NotFound();
            

            if (ModelState.IsValid)
            
                try
                
                    _context.Update(student);
                    await _context.SaveChangesAsync();
                
                catch (DbUpdateConcurrencyException)
                
                    if (!StudentExists(student.Id))
                    
                        return NotFound();
                    
                    else
                    
                        throw;
                    
                
                return RedirectToAction(nameof(Index));
            
            return View(student);
        

        // GET: Student/Delete/5
        public async Task<IActionResult> Delete(int? id)
        
            if (id == null)
            
                return NotFound();
            

            var student = await _context.Students
                .FirstOrDefaultAsync(m => m.Id == id);
            if (student == null)
            
                return NotFound();
            

            return View(student);
        

        // POST: Student/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        
            var student = await _context.Students.FindAsync(id);
            _context.Students.Remove(student);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        

        private bool StudentExists(int id)
        
            return _context.Students.Any(e => e.Id == id);
        
    

我有一个文件 DB_Context.cs,其中包含一个类及其实现,如下所示:

using Microsoft.EntityFrameworkCore;
using mvc_net6.Models;

namespace mvc_net6.Service

    public class DB_Context : DbContext
    
        public DB_Context(DbContextOptions<DB_Context> options) : base(options) 

        public DbSet<mvc_net6.Models.Student> Students get; set;

    


我有一个文件 DB_Factory.cs,其中包含一个类及其实现,如下所示:

namespace mvc_net6


   public class BloggingContextFactory : IDesignTimeDbContextFactory<DB_Context>
   
      public DB_Context CreateDbContext(string[] args)
      
         // Replace with your server version and type.
         // Use 'MariaDbServerVersion' for MariaDB.
         // Alternatively, use 'ServerVersion.AutoDetect(connectionString)'.
         var serverVersion = new MariaDbServerVersion(new Version(10, 5, 12));

         var optionsBuilder = new DbContextOptionsBuilder<DB_Context>();
         optionsBuilder.Usemysql("ConnectionString", serverVersion);


         return new DB_Context(optionsBuilder.Options);
      
   


My Program.cs 包含从模板默认生成的代码。

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();
IConfiguration configuration = app.Configuration;

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())

    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();



app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "controller=Home/action=Index/id?");

app.Run();


//builder.Services.AddDbContext<DB_Context>(options =>
//    options.UseMySql(connection, serverVersion));
 
builder.Services.AddDbContext<DB_Context>(opt =>
          
            opt.UseMySql(connection, serverVersion);
          );

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "controller=Home/action=Index/id?");
    

app.Run();

但是,在运行系统时,我现在收到以下错误:

watch : Started
Unhandled exception. System.InvalidOperationException: Cannot modify ServiceCollection after application is built.
   at Microsoft.AspNetCore.WebApplicationServiceCollection.CheckServicesAccess()
   at Microsoft.AspNetCore.WebApplicationServiceCollection.Add(ServiceDescriptor item)
   at Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.TryAdd(IServiceCollection collection, ServiceDescriptor descriptor)
   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddCoreServices[TContextImplementation](IServiceCollection serviceCollection, Action`2 optionsAction, ServiceLifetime optionsLifetime)
   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext[TContextService,TContextImplementation](IServiceCollection serviceCollection, Action`2 optionsAction, ServiceLifetime contextLifetime, ServiceLifetime optionsLifetime)
   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext[TContextService,TContextImplementation](IServiceCollection serviceCollection, Action`1 optionsAction, ServiceLifetime contextLifetime, ServiceLifetime optionsLifetime)
   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext[TContext](IServiceCollection serviceCollection, Action`1 optionsAction, ServiceLifetime contextLifetime, ServiceLifetime optionsLifetime)
   at Program.<Main>$(String[] args) in /home/r31sr4r/Projects/dotnet_core/mvc_net6/Program.cs:line 31

watch : Exited with error code 134
watch : Waiting for a file to change before restarting dotnet...

【问题讨论】:

【参考方案1】:

在您的 program.cs 中,确保注入 db DB_Context

builder.services.AddDbContext<DB_Context>(opt =>
            
                opt.UseMySql("YOUR_CONNECTION_STRING");
            );

否则,您不能在controller上使用它

【讨论】:

这对我有用。非常感谢!!! 很高兴听到这个消息。您可以将答案标记为正确或投赞成票,以便遇到类似问题的其他人会有所帮助【参考方案2】:

将您的 dbcontext 配置放在此之前:

var app = builder.Build();

【讨论】:

【参考方案3】:

您需要在“var app = builder.Build();”之前进行此注入。

【讨论】:

【参考方案4】:

你会得到错误,Cannot modify ServiceCollection after application is built. 以一种非常偷偷摸摸的方式。

我在ConfigureServices 中有一些代码,如下所示:

    services.AddAuthentication(options =>
        
            // Set up options...
        )
        .AddOpenIdConnect(options =>
        
            // Set up options...
            services.AddHttpClient<IMyTokenClient, MyTokenClient>(httpClient =>
            
                httpClient.BaseAddress = new Uri(authSettings.Authority);
            );
        );

您可以看到服务在嵌套闭包中设置不正确。 我将代码更改为:

    services.AddAuthentication(options =>
        
            // Set up options...
        )
        .AddOpenIdConnect(options =>
        
            // Set up options...
        );            
    services.AddHttpClient<IMyTokenClient, MyTokenClient>(httpClient =>
        
            httpClient.BaseAddress = new Uri(authSettings.Authority);
        );

这解决了错误。

【讨论】:

谢谢@Jess!!

以上是关于.net Core 6 - 尝试激活时无法解析服务类型的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET Core 依赖注入错误:尝试激活时无法解析服务类型 - 调用视图组件时出现错误 [重复]

ASP.NET Core 2 无法解析 Microsoft EntityFrameworkCore DbContext 类型的服务

ASP.NET Core Web API InvalidOperationException:无法解析服务 [重复]

尝试激活服务时无法解析类型“System.Lazy`1[System.Net.Http.IHttpClientFactory]”的服务

尝试将RoleManager注入ASP.NET Core 2.2控制器时出现错误

尝试激活时无法解析服务类型