gpt4 book ai didi

C# - IoC - WebAPI 2 上的 Autofac 依赖注入(inject)

转载 作者:行者123 更新时间:2023-12-04 15:54:22 31 4
gpt4 key购买 nike

我在使用我的应用程序配置 Autofac 时遇到了一些问题。我将不胜感激你的帮助。下面是有异常(exception)的代码。如果您需要更多详细信息,请随时发表评论。谢谢!

  1. 接口(interface)配置

    public static void Register(HttpConfiguration config)
    {
    // Web API configuration and services
    var setings = config.Formatters.JsonFormatter.SerializerSettings;
    setings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    setings.Formatting = Formatting.Indented;

    // Web API Cors
    var origin = "https://localhost:44381";
    EnableCorsAttribute cors = new EnableCorsAttribute(origin, "*", "*");
    config.EnableCors(cors);

    // DI
    var builder = new ContainerBuilder();

    builder.RegisterType<AlertRepository>()
    .As<IAlertRepository>();
    builder.RegisterType<UnitOfWork>()
    .As<IUnitOfWork>();
    builder.RegisterGeneric(typeof(Repository<>))
    .As(typeof(IRepository<>))
    .InstancePerRequest();
    builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

    var container = builder.Build();
    GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

    // Web API routes
    config.MapHttpAttributeRoutes();
    config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
    );
    }
  2. 工作类单位

    public class UnitOfWork : IUnitOfWork
    {
    private readonly CRNetContext _context;

    public UnitOfWork(CRNetContext context)
    {
    _context = context;
    Alerts = new AlertRepository(_context);
    }

    public IAlertRepository Alerts { get; private set; }

    public int Complete()
    {
    return _context.SaveChanges();
    }

    public void Dispose()
    {
    _context.Dispose();
    }
    }
  3. 异常

{ "message": "An error has occurred.", "exceptionMessage": "An error occurred when trying to create a controller of type 'AlertsController'. Make sure that the controller has a parameterless public constructor.", "exceptionType": "System.InvalidOperationException", "stackTrace": " at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()", "innerException": { "message": "An error has occurred.", "exceptionMessage": "An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = AlertsController (ReflectionActivator), Services = [CRNET.API.Controllers.AlertsController], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope ---> An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = UnitOfWork (ReflectionActivator), Services = [CRNET.DAL.Persistance.IUnitOfWork], Lifetime = Autofac.Core.Lifetime.MatchingScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope ---> None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'CRNET.DAL.Persistance.UnitOfWork' can be invoked with the available services and parameters:\r\nCannot resolve parameter 'CRNET.DAL.Models.CRNetContext context' of constructor 'Void .ctor(CRNET.DAL.Models.CRNetContext)'. (See inner exception for details.) (See inner exception for details.)", "exceptionType": "Autofac.Core.DependencyResolutionException", "stackTrace": " at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Execute()\r\n at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable1 parameters)\r\n at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable1 parameters)\r\n at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable1 parameters)\r\n at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable1 parameters, Object& instance)\r\n at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable1 parameters)\r\n at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType)\r\n at Autofac.Integration.WebApi.AutofacWebApiDependencyScope.GetService(Type serviceType) in C:\projects\autofac-webapi\src\Autofac.Integration.WebApi\AutofacWebApiDependencyScope.cs:line 76\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func1& activator)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)",
"innerException": {
"message": "An error has occurred.",
"exceptionMessage": "An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = UnitOfWork (ReflectionActivator), Services = [CRNET.DAL.Persistance.IUnitOfWork], Lifetime = Autofac.Core.Lifetime.MatchingScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope ---> None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'CRNET.DAL.Persistance.UnitOfWork' can be invoked with the available services and parameters:\r\nCannot resolve parameter 'CRNET.DAL.Models.CRNetContext context' of constructor 'Void .ctor(CRNET.DAL.Models.CRNetContext)'. (See inner exception for details.)",
"exceptionType": "Autofac.Core.DependencyResolutionException",
"stackTrace": " at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable
1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.b__5_0()\r\n at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func1 creator)\r\n at Autofac.Core.Resolving.InstanceLookup.Execute()\r\n at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable1 parameters)\r\n at Autofac.Core.Activators.Reflection.AutowiringParameter.<>c__DisplayClass0_0.<CanSupplyValue>b__0()\r\n at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()\r\n at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 parameters)",
"innerException": {
"message": "An error has occurred.",
"exceptionMessage": "None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'CRNET.DAL.Persistance.UnitOfWork' can be invoked with the available services and parameters:\r\nCannot resolve parameter 'CRNET.DAL.Models.CRNetContext context' of constructor 'Void .ctor(CRNET.DAL.Models.CRNetContext)'.",
"exceptionType": "Autofac.Core.DependencyResolutionException",
"stackTrace": " at Autofac.Core.Activators.Reflection.ReflectionActivator.GetValidConstructorBindings(IComponentContext context, IEnumerable
1 parameters)\r\n at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 parameters)" } } } }

最佳答案

Cannot resolve parameter 'CRNET.DAL.Models.CRNetContext context'...

您永远不会向 Autofac 注册类型 CRNetContext。要修复它,请添加:

builder.RegisterType<CRNET.DAL.Models.CRNetContext>().AsSelf();

关于C# - IoC - WebAPI 2 上的 Autofac 依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52432273/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com