Hi my enviroment is MVC4RC wepapi and microsoft.practices.unity v2.0
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.
In 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));
}
}
the IoC resolver:
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 and not System.Web.Http.Dependencies.IDependencyResolver
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)
a mvc controller:
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;
}
....
}
the _AuthorizedController is the one i use to have all the user and site information needed:
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;
}
...
}
and one webapi controller:
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;
}
.... }
and the _AuthorizedApiController i created to preservethe inheritance chain (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;
}
.....
}
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 Using the Web Api dependency... but no luck
i always have the very expresive error 'Site.Web.webapi.InformationController' does not have a default constructor
Any help appreciated Tx.