使用Unity与Web Api 2给出错误没有默认构造函数

时间:2016-05-08 07:07:55

标签: asp.net-mvc dependency-injection asp.net-web-api2 unity-container

我有ASP.NET MVC5 Web应用程序,我也在同一个应用程序中使用Web API。我是用于DI的Unity(版本4)。 我在APP启动时配置Unity容器,如下所示

        public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                UnityConfiguration.Config();
            }
        }

        public class UnityConfiguration()
        {
         public void Config()
         {
                UnityContainer container = new UnityContainer();
                container.RegisterType<IMyService, Myservice>();
                container.RegisterType<IGenericRepository, GenericRepository>();
                container.RegisterType<DbContext, MyEntities>();            
         }
        }

        public class GenericRepository:IGenericRepository
        {
           private DbContext _dbcontext;
           public GenericRepository(DbContext dbcontext)
           {
               _dbcontext = dbcontext;
           }
        }
        public class MyService:IMyService
        {
           private IGenericRepository _repo;
           publi void MyService(IGenericRepository repository)
           {
              _repo = repository;
           }
        }


        public class MyApiController:ApiController
        {
           provate IMyService _service;
           MyApiController(IMyService myservice)
           {
              _service = myservice;
           }

           public IEnumerable<MyModel> GetData()
           {
             var result = _service.GetData();
             return result.ConvertToMyModel();
           }
        }

然而,当我打电话给网址时

localhost://lookup/getdata

我收到错误

  

类型'LookupController'没有默认构造函数

我该如何解决这个问题?我是否需要注册我使用Unity创建的每个控制器或Unity自动注册所有MVC控制器?

2 个答案:

答案 0 :(得分:3)

我倾向于使用Unity.Mvc - 包。

您无需注册控制器,但需要向WebAPI注册Unity。

   public class UnityConfiguration()
   {
       public IUnityContainer Config()
       {
            IUnityContainer container = new UnityContainer();
            container.RegisterType<IMyService, Myservice>();
            container.RegisterType<IGenericRepository, GenericRepository>();
            container.RegisterType<DbContext, MyEntities>(); 

            // return the container so it can be used for the dependencyresolver.  
            return container;         
       }
    }

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Your routes...

            // Register Unity with Web API.
            var container = UnityConfiguration.Config();
            config.DependencyResolver = new UnityResolver(container);

            // Maybe some formatters?
        }
    }

您还需要DependencyResolver:

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();
    }
}

除了Owin-part之外,你还可以看看这个类似的问题。 Unity.WebApi | Make sure that the controller has a parameterless public constructor

答案 1 :(得分:2)

我有同样的错误,在我的情况下问题是,我忘了注册一个依赖项,其中一个类,我已注册依赖注入,注入构造函数。

在您的示例中,您是否可能在MyEntities中注入了一些您忘记注册的内容?