创建类型为“ UserController”的控制器失败。确保
  控制器具有无参数的公共构造函数


使用AutoFac,我已经如下注册了UnitOfWork信息库,也正在粘贴控制器代码和堆栈跟踪。请协助。无法理解控制器为何需要无参数公共构造函数。

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)


控制器:

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


  构建容器后,将其传递到AutofacWebApiDependencyResolver类的新实例。将新的解析器附加到HttpConfiguration.DependencyResolver上,以使Web API知道它应该使用AutofacWebApiDependencyResolver定位服务。这是Autofac对IDependencyResolver接口的实现。


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


我可以告诉您没有这样做,因为stacktrace专门指出了它用于创建控制器的解析器:


  System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage请求,类型controllerType,Func`1&Activator)

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

10-10 17:01