嗨,我的环境是 MVC4RC wepapi 和 microsoft.practices.unity v2.0
我想在 mvc 控制器(继承自 System.web.mvc.Controller)和 webapi 控制器(继承自 System.web.http.ApiController)中使用 IoC/DI。问题是我无法同时上班,如果有人可以帮助我,我会粘贴一些代码。
在 global.asax.cs 中
public class MvcApplication : System.Web.HttpApplication
{
private static IUnityContainer container;
protected void Application_Start()
{
....
InitializeDependencyInjectionContainer(GlobalConfiguration.Configuration);
}
private static void InitializeDependencyInjectionContainer(HttpConfiguration config)
{
container = new UnityContainer();
container.RegisterType<Site.Web.Data.IDatabaseFactory, Site.Web.Data.DatabaseFactory>();
container.RegisterType<Site.Web.Data.Interfaces.IUnitOfWork, Site.Web.Data.UnitOfWork>();
container.RegisterType<Site.Web.Data.Interfaces.IUserRepository, Site.Web.Data.Repositories.UserRepository>();
container.RegisterType<Site.Web.Data.Interfaces.ISiteRepository, Site.Web.Data.Repositories.SiteRepository>();
container.RegisterType<Site.Web.Data.Interfaces.IInformationRepository, Site.Web.Data.Repositories.InformationRepository>();
container.RegisterType<IUserServices, UserServices>();
container.RegisterType<ISiteServices, SiteServices>();
container.RegisterType<IInformationServices, InformationServices>();
container.RegisterType<IFormsAuthenticationService, FormsAuthenticationService>();
container.RegisterType<IMembershipService, MembershipService>();
//config.DependencyResolver = new Site.Web.IoC.IoCContainer(container);
DependencyResolver.SetResolver(new Site.Web.IoC.IoCContainer(container));
//DependencyResolver.SetResolver(new Site.Web.IoC.UnityDependencyResolver(container));
}
}
IoC 解析器:
public class IoCContainer : System.Web.Mvc.IDependencyResolver
{
private readonly IUnityContainer container;
public IoCContainer(IUnityContainer container)
{
this.container = container;
}
public object GetService(Type serviceType)
{
try
{
return this.container.Resolve(serviceType);
}
catch (ResolutionFailedException)
{
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return this.container.ResolveAll(serviceType);
}
catch (ResolutionFailedException)
{
return new List<object>();
}
}
}
注意: System.Web.Mvc.IDependencyResolver 而不是 System.Web.Http.Dependencies.IDependencyResolver
现在两个控制器都具有相同的结构,我使用一个通用类来提供用户和站点信息(作为 codeplex/Silk 的样式)
一个 mvc 控制器:
public class SiteController : _AuthorizedController
{
ISiteServices _siteServices;
public SiteController(Site.Web.Domain.Services.IUserServices userServices,
Site.Web.Domain.Services.ISiteServices siteServices)
: base(userServices)
{
this._siteServices = siteServices;
}
....
}
_AuthorizedController 是我用来获取所需的所有用户和站点信息的控制器:
public class _AuthorizedController : Controller
{
protected readonly Site.Web.Domain.Services.IUserServices _userServices;
public _AuthorizedController(Site.Web.Domain.Services.IUserServices userServices)
{
if (userServices == null) throw new ArgumentNullException("userServices");
this._userServices = userServices;
}
...
}
和一个 webapi 控制器:
public class InformationController : _AuthorizedApiController
{
private ISiteServices _siteServices;
private IUserServices _userServices;
public InformationController(Site.Web.Domain.Services.IUserServices userServices,
Site.Web.Domain.Services.ISiteServices siteServices)
: base(userServices)
{
this._siteServices = siteServices;
this._userServices = userServices;
}
....}
以及我为保留继承链(ApiController)而创建的 _AuthorizedApiController:
public class _AuthorizedApiController : ApiController
{
protected readonly Site.Web.Domain.Services.IUserServices _userServices;
public _AuthorizedApiController(Site.Web.Domain.Services.IUserServices userServices)
{
if (userServices == null) throw new ArgumentNullException("userServices");
this._userServices = userServices;
}
.....
}
好吧,这种结构适用于 mvc 控制器,但我无法在 apicontroller 上运行。我会尝试根据使用 Web Api 依赖项更改 IoC ......但没有运气
我总是有非常明显的错误'Site.Web.webapi.InformationController'没有默认构造函数
任何帮助感谢 Tx。