使用Unity与网页API 2给出了错误没有默认构造函数 [英] Using Unity with Web Api 2 gives error does not have a default constructor
问题描述
我有ASP.NET MVC5 Web应用程序,我也有同样的应用程序的Web API。我uisng统一(版本4)DI。
我对APP开始配置统一容器如下
I have ASP.NET MVC5 web application and i also have Web API in the same application. I am uisng Unity (version 4) for DI. I am configuring the Unity container on APP start as below
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();
}
}
然而,当我打电话像
However when i call the url like
localhost://lookup/getdata
我得到错误
键入LookupController'没有默认的构造函数
Type 'LookupController' does not have a default constructor
我要如何解决这个问题呢?我是否需要每个控制器我使用Unity或Unity自动注册所有MVC控制器?
How do i solve this issue? Do i need to register each controller i create with Unity or Unity automatically registers all MVC controllers?
推荐答案
我倾向于使用 Unity.Mvc -package。
I tend to use the Unity.Mvc-package.
您不需要注册控制器,但你需要的WebAPI注册统一。
You do not need to register the controllers, but you need to register Unity with WebAPI.
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:
You also need a 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();
}
}
您也可以看看这个similiar问题,除了Owin部分。 <一href=\"http://stackoverflow.com/questions/34936675/unity-webapi-make-sure-that-the-controller-has-a-parameterless-public-construc/34991456#34991456\">Unity.WebApi |确保控制器具有一个无参数的公共构造
You can also take a look at this similiar question, except for the Owin-part. Unity.WebApi | Make sure that the controller has a parameterless public constructor
这篇关于使用Unity与网页API 2给出了错误没有默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!