I'm trying to use an IoC container inside the WebApi template. I've created an empty Web App Framework project, and selected to use WebApi. I've then added the Unity NuGet package and added the following to Application_Start:
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
UnityConfig.Register();
}
The UnityConfig:
public class UnityConfig
{
public static UnityContainer Register()
{
var container = new UnityContainer();
container.RegisterType<IMyClass, MyClass>();
GlobalConfiguration.Configuration.DependencyResolver = new UnityResolver(container);
return container;
}
}
My UnityResolver is here (the code is largely plagiarised, but it seems pretty bog standard):
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()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
container.Dispose();
}
}
When I now run this, I get the following error:
Unity.Exceptions.ResolutionFailedException: 'Resolution of the dependency failed, type = 'System.Web.Http.Hosting.IHostBufferPolicySelector', name = '(none)'. Exception occurred while: while resolving. Exception is: InvalidOperationException - The current type, System.Web.Http.Hosting.IHostBufferPolicySelector, is an interface and cannot be constructed. Are you missing a type mapping? ----------------------------------------------- At the time of the exception, the container was: Resolving System.Web.Http.Hosting.IHostBufferPolicySelector,(none) '
Clearly, after telling it which IoC library I want to use, I need to inject some internal dependencies, or tell it to, or perhaps, register this earlier (or later).
Clearly, after telling it which IoC library I want to use, I need to inject some internal dependencies, or tell it to, or perhaps, register this earlier (or later).
That's true. You need to register all of the types that will be resolved through Unity. The error message indicates you are missing IHostBufferPolicySelector
, so you need to register it.
public static UnityContainer Register()
{
var container = new UnityContainer();
// Register all types with Unity here
container.RegisterType<IHostBufferPolicySelector, OwinBufferPolicySelector>();
GlobalConfiguration.Configuration.DependencyResolver = new UnityResolver(container);
return container;
}
I don't know for sure if OwinBufferPolicySelector
is the one you need because you haven't provided enough info, but you do need to map an implementation for it (and probably several other interfaces) in order to proceed.
As noted in the comments, it would probably be easier if you install one of the premade Unity packages (such as Unity.WebAPI) to supply this implementation.
It will also help considerably if you follow the Dependency Injection in ASP.NET Web API 2 documentation.