Unity mvc4和webapi相同的应用程序

时间:2012-07-20 05:53:44

标签: asp.net-mvc inversion-of-control unity-container asp.net-web-api

您好我的环境是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>();
        }
    }
}

N.B: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;
    }

....     }

和我创建的_AuthorizedApiController来保护继承链(ApiController):

    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上运行。我会尝试根据Using the Web Api dependency...改变IoC,但没有运气

我总是有非常明显的错误'Site.Web.webapi.InformationController'没有默认构造函数

任何帮助都赞赏Tx。

2 个答案:

答案 0 :(得分:1)

根据Tx3注释

对于webapi,DependencyResolver没有实现,因此您需要它。

nuget Unity.WebAPI已经得到它,但“不幸的是”你需要安装(不再工作)nuget Unity.Mvc3(适用于MVC4Rc)

所有流程:mvc4 webapi

答案 1 :(得分:1)

不是Unity特定的,但请查看Haack先生的标题下的解决方案:Two DependencyResolvers @ http://haacked.com/archive/2012/03/11/itrsquos-the-little-things-about-asp-net-mvc-4.aspx