简体   繁体   English

.NET 6 API System.InvalidOperationException

[英].NET 6 API System.InvalidOperationException

I have Autofac module like this on one class;我在一个 class 上有这样的 Autofac 模块;

  public class AutofacBusinessModule:Module
  {
    protected override void Load(ContainerBuilder builder)
    {
      builder.RegisterType<ProductManager>().As<IProductService>();
      builder.RegisterType<EfProductDal>().As<IProductDal>();

      builder.RegisterType<CategoryManager>().As<ICategoryService>();
      builder.RegisterType<EfCategoryDal>().As<ICategoryDal>();

      builder.RegisterType<CustomerManager>().As<ICustomerService>();
      builder.RegisterType<EfCustomerDal>().As<ICustomerDal>();

      builder.RegisterType<RegionManager>().As<IRegionService>();
      builder.RegisterType<EfRegionDal>().As<IRegionDal>();

      builder.RegisterType<TerritoryManager>().As<ITerritoryService>();
      builder.RegisterType<EfTerritoryDal>().As<ITerritoryDal>();

      builder.RegisterType<ShipperManager>().As<IShipperService>();
      builder.RegisterType<EfShipperDal>().As<IShipperDal>();

      builder.RegisterType<EmployeeManager>().As<IEmployeeDal>();
      builder.RegisterType<EfEmployeeDal>().As<IEmployeeDal>();



      var assembly = System.Reflection.Assembly.GetExecutingAssembly();
      builder.RegisterAssemblyTypes(assembly).AsImplementedInterfaces()
        .EnableInterfaceInterceptors(new ProxyGenerationOptions()
        {
          Selector = new AspectInterceptorSelector()
        }).SingleInstance();
    }

my api code its there;我的 api 代码在那里;

  [Route("api/[controller]")]
  [ApiController]
  public class ProductsController : Controller
  {

    private readonly IProductService _productService;
    public ProductsController(IProductService productService)
    {
      _productService = productService;
    }

    [HttpGet("getall")]
    public IActionResult GetAllProducts()
    {
      var result = _productService.GetAll();
      if (result.Success)
      {
        return Ok(result);
      }

      return BadRequest(result);
    }
  }
}

I add this on program.cs but didn't work, if lower I use .net 6 i fix it like that but i dont know how to fix .net 6 because startup doesnt exists anymore.我在 program.cs 上添加了这个但没有用,如果较低我使用 .net 6 我这样修复它但我不知道如何修复 .net 6 因为启动不再存在。

IHostBuilder CreateHostBuilder(string[] args) =>
  Host.CreateDefaultBuilder(args)
    .UseServiceProviderFactory(new AutofacServiceProviderFactory())
    .ConfigureContainer<ContainerBuilder>(builder =>
    {
      builder.RegisterModule(new AutofacBusinessModule());
    });

How can I fix this?我怎样才能解决这个问题?

it gives me that error它给了我那个错误

System.InvalidOperationException: Unable to resolve service for type 'TAO.Business.Abstract.IProductService' while attempting to activate 'TAO.WebApi.Controllers.ProductsController'. System.InvalidOperationException:在尝试激活“TAO.WebApi.Controllers.ProductsController”时无法解析类型“TAO.Business.Abstract.IProductService”的服务。 at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired) at lambda_method3(Closure, IServiceProvider, Object[] ) at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass7_0.b__0(ControllerContext controllerContext) at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass6_0.g__CreateController|0(ControllerContext controllerContext) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, 88116134347在 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired) 在 lambda_method3(Closure, IServiceProvider, Object[] ) 在 Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass7_0.b__0 (ControllerContext controllerContext) 在 Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass6_0.g__CreateController|0(ControllerContext controllerContext) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state is ) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() --- 上一个位置的堆栈跟踪结束 --- 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|20_0(ResourceInvoker 调用程序,任务 lastTask,State接下来,Scope scope,Object state,88116134347 688 isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) 688 已完成)在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker 调用程序、任务任务、IDisposable 范围)在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker 调用程序、任务任务、IDisposable 范围) 在 Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(终结点端点、任务请求任务、ILogger 记录器) 在 Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext 上下文) ) 在 Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) 在 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext 上下文)

HEADERS
=======
Accept: */*
Connection: keep-alive
Host: localhost:7019
User-Agent: PostmanRuntime/7.30.0
Accept-Encoding: gzip, deflate, br
Postman-Token: fbff062e-ca49-4f37-afc5-e8785afbd43c

Try the following code from migration doc :迁移文档中尝试以下代码:

builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());

// Register services directly with Autofac here. Don't
// call builder.Populate(), that happens in AutofacServiceProviderFactory.
builder.Host.ConfigureContainer<ContainerBuilder>(builder => builder.RegisterModule(new AutofacBusinessModule()));

Note that the "old" generic hosting model still works in .NET 6, there is no requirement to use the new minimal hosting model (but you need to copy not only CreateHostBuilder method but also contents of old Program.Main into the new Program top-level statement file).请注意,“旧”通用主机 model 仍然适用于 .NET 6,没有要求使用新的最小主机 model(但您不仅需要复制CreateHostBuilder方法,还需要将旧Program.Main的内容复制到新Program中 top-级语句文件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM