Note that there are some explanatory texts on larger screens.

plurals
  1. POUnity mvc4 and webapi same application
    primarykey
    data
    text
    <p>Hi my enviroment is MVC4RC wepapi and microsoft.practices.unity v2.0</p> <p>I would like to use IoC/DI in mvc controllers (inherits from System.web.mvc.Controller) and in webapi controllers (inherits from System.web.http.ApiController). The problem is i´m not be able to get to work at the same time, i paste some code if some one can help me.</p> <p>In global.asax.cs</p> <pre><code> 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&lt;Site.Web.Data.IDatabaseFactory, Site.Web.Data.DatabaseFactory&gt;(); container.RegisterType&lt;Site.Web.Data.Interfaces.IUnitOfWork, Site.Web.Data.UnitOfWork&gt;(); container.RegisterType&lt;Site.Web.Data.Interfaces.IUserRepository, Site.Web.Data.Repositories.UserRepository&gt;(); container.RegisterType&lt;Site.Web.Data.Interfaces.ISiteRepository, Site.Web.Data.Repositories.SiteRepository&gt;(); container.RegisterType&lt;Site.Web.Data.Interfaces.IInformationRepository, Site.Web.Data.Repositories.InformationRepository&gt;(); container.RegisterType&lt;IUserServices, UserServices&gt;(); container.RegisterType&lt;ISiteServices, SiteServices&gt;(); container.RegisterType&lt;IInformationServices, InformationServices&gt;(); container.RegisterType&lt;IFormsAuthenticationService, FormsAuthenticationService&gt;(); container.RegisterType&lt;IMembershipService, MembershipService&gt;(); //config.DependencyResolver = new Site.Web.IoC.IoCContainer(container); DependencyResolver.SetResolver(new Site.Web.IoC.IoCContainer(container)); //DependencyResolver.SetResolver(new Site.Web.IoC.UnityDependencyResolver(container)); } } </code></pre> <p>the IoC resolver:</p> <pre><code> 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&lt;object&gt; GetServices(Type serviceType) { try { return this.container.ResolveAll(serviceType); } catch (ResolutionFailedException) { return new List&lt;object&gt;(); } } } </code></pre> <p>N.B: System.Web.Mvc.IDependencyResolver and not System.Web.Http.Dependencies.IDependencyResolver</p> <p>Now the controllers, both have the same structure, i use a common class to have the user and site information available (as the style of codeplex/Silk)</p> <p>a mvc controller:</p> <pre><code> 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; } .... } </code></pre> <p>the _AuthorizedController is the one i use to have all the user and site information needed:</p> <pre><code> 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; } ... } </code></pre> <p>and one webapi controller:</p> <pre><code> 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; } </code></pre> <p>.... }</p> <p>and the _AuthorizedApiController i created to preservethe inheritance chain (ApiController):</p> <pre><code> 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; } ..... } </code></pre> <p>well this structure works for mvc controller but i was not able to run on apicontroller. i would try to change the IoC according to <a href="http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver" rel="nofollow">Using the Web Api dependency...</a> but no luck</p> <p>i always have the very expresive error 'Site.Web.webapi.InformationController' does not have a default constructor</p> <p>Any help appreciated Tx.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload