【问题标题】:.net Core 6 - Unable to resolve service for type while attempting to activate.net Core 6 - 尝试激活时无法解析服务类型
【发布时间】: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...

【问题讨论】:

    标签: c# asp.net-mvc .net-core


    【解决方案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!!
        猜你喜欢
        • 2020-07-01
        • 2018-11-04
        • 1970-01-01
        • 2018-04-06
        • 2021-09-23
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多