尝试创建“ SaleController”类型的控制器时发生错误。确保控制器具有无参数的公共构造函数

时间:2019-10-14 07:19:12

标签: c# unity3d

我一直在关注这篇文章:

https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/dependency-injection#dependency-scope-and-controller-lifetime

我得到的错误是标题:

  

确保控制器具有无参数的公共构造函数。

基本上,Unity代码与Microsoft项目中的代码相同。

    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        var container = new UnityContainer();

        container.RegisterType<ITrace, Trace>(new HierarchicalLifetimeManager());
        container.RegisterType<IUtility, Utility>(new HierarchicalLifetimeManager());
        container.RegisterType<IValidator, Validator>(new HierarchicalLifetimeManager());
        container.RegisterType<IDeliveryService, DeliveryService>(new HierarchicalLifetimeManager());
        container.RegisterType<ISaleService, SaleService>(new HierarchicalLifetimeManager());
        config.DependencyResolver = new UnityResolver(container);


        // Configure Web API to use only bearer token authentication.
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

我有UnityResolver:

public class UnityResolver : IDependencyResolver
{
    protected IUnityContainer container;

    public UnityResolver(IUnityContainer container)
    {
        if (container == null)
        {
            throw new ArgumentNullException("container");
        }
        this.container = container;
    }

    public object GetService(Type serviceType)
    {
        try
        {
            return container.Resolve(serviceType);
        }
        catch (ResolutionFailedException)
        {
            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            return container.ResolveAll(serviceType);
        }
        catch (ResolutionFailedException)
        {
            return new List<object>();
        }
    }

    public IDependencyScope BeginScope()
    {
        var child = container.CreateChildContainer();
        return new UnityResolver(child);
    }

    public void Dispose()
    {
        container.Dispose();
    }

Unity应该使用此实例化控制器。我唯一关心的是数据库上下文。我尝试寻找注册方法,但一无所获。

这里要求的是我的dbcontext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false)
    {
        Configuration.LazyLoadingEnabled = false;
        Configuration.ProxyCreationEnabled = false;
        Configuration.ValidateOnSaveEnabled = false;
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

我是第一次使用Unity。

1 个答案:

答案 0 :(得分:1)

在Unity中,有时具有特定构造函数的自定义类也需要一个额外的无参数构造函数(它可能什么都不做,但必须存在),例如

public UnityResolver(){ }

Afaik,这主要与(反)序列化有关。不知道为什么在我们的特定情况下需要它..但这就是错误试图告诉您的内容;)