gpt4 book ai didi

c# - 创建 Controller 失败说明 Controller 需要无参数公共(public)构造函数

转载 作者:行者123 更新时间:2023-11-30 23:02:38 25 4
gpt4 key购买 nike

Create Controller Failure of type 'UserController'. Make sure that the controller has a parameterless public constructor

使用 AutoFac,我已经注册了 UnitOfWork 存储库,如下所示,我还粘贴了 Controller 代码和堆栈跟踪。请协助。无法理解为什么 Controller 需要无参数公共(public)构造函数。

private static IContainer RegisterServices(ContainerBuilder builder)
{
//Register your Web API controllers.
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

builder.RegisterType<UserContext>().As<DbContext>().InstancePerRequest();
builder.RegisterType<DBFactory>().As<IDBFactory>().InstancePerRequest();
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerRequest();
builder.RegisterGeneric(typeof(GenericRepo<>)).As(typeof(IGenericRepo<>)).InstancePerRequest();

//Set the dependency resolver to be Autofac.
Container = builder.Build();

return Container;
}

private readonly IUnitOfWork _unitofwork;
public UserController(IUnitOfWork uow)
{
_unitofwork = uow;
}
[HttpGet]
public IEnumerable<UserDetail> UserData()
{
return _unitofwork.userDetail.GetAllUsers();
}

堆栈跟踪

  at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.<Execute>b__0()
at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func`1 creator)
at Autofac.Core.Resolving.InstanceLookup.Execute()
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.Core.Activators.Reflection.AutowiringParameter.<>c__DisplayClass2.<CanSupplyValue>b__0()
at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()
at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Execute()
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)
at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable`1 parameters)
at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType, IEnumerable`1 parameters)
at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType)
at Autofac.Integration.WebApi.AutofacWebApiDependencyScope.GetService(Type serviceType)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)

Controller :

public class UserController : ApiController
{
private readonly IUnitOfWork _unitofwork;
public UserController(IUnitOfWork uow)
{
_unitofwork = uow;
}

[HttpGet]
public IEnumerable<UserDetail> UserData()
{
return _unitofwork.userDetail.GetAllUsers();
}
}

最佳答案

As stated in the documentation, you need set the dependency resolver :

After building your container pass it into a new instance of the AutofacWebApiDependencyResolver class. Attach the new resolver to your HttpConfiguration.DependencyResolver to let Web API know that it should locate services using the AutofacWebApiDependencyResolver. This is Autofac’s implementation of the IDependencyResolver interface.

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

我可以告诉你没有这样做,因为堆栈跟踪明确说明了它使用哪个解析器来创建 Controller :

System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)

关于c# - 创建 Controller 失败说明 Controller 需要无参数公共(public)构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50376079/

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