Using SimpleInjector Is there a way to replicate RegisterWebApiRequest <T> () with .net 4 / webapi 1?
Follow this question . I am working on an example using SimpleInjector and WebAPI. Unfortunately, when I want to use the WebAPI KB2568167 and KB2915689 to prevent me from upgrading to .net 4.5. So I am stuck using .net 4.0 and WebAPI v1 (4.0.30506.0) at the moment.
Is there a way to replicate a scope RegisterWebApiRequest<T>()with an older version of WebAPI?
While I only contained .net 4.5 versions in nu-get packages, I was able to download the code and get compilation of framework 4.0 without any problems. When called var container = request.GetDependencyScope()in my message handler, a class is returned SimpleInjectorWebApiDependencyResolver. Trying to retrieve an instance from a container as follows:
var poco = (SimplePOCO) container.GetService(typeof(SimplePOCO));
leads to the following error:
A registered delegate for type SimplePOCO throws an exception. SimplePOCO is registered as a Web API Request style, but an instance is requested outside the context of the Web API request.
Am I just missing something in my config? Is there an alternative - like creating my own message handler?
UPDATE
After posting the codeplex question, I went back to the basics. I accepted the simple Vanilla Mvc WebApi project, citing my compilations SimpleInjector, SimpleInjector.Integration.WebApiand SimpleInjector.Extensions.ExecutionContextScoping.
@blueling, .
? , - - WebApi slim web.config. , , . - web.config .
2
, , Dispose() DependencyResolver, ....
. , dispose SimpleInjectorWebApiDependencyResolver, :
public sealed class SimpleInjectorWebApiDependencyResolver : IDependencyResolver
{
private readonly Container container;
public SimpleInjectorWebApiDependencyResolver(Container container)
{
this.container = container;
}
public IDependencyScope BeginScope()
{
return this;
}
public object GetService(Type serviceType)
{
return ((IServiceProvider)this.container).GetService(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return this.container.GetAllInstances(serviceType);
}
public void Dispose()
{
}
}
public sealed class SimpleInjectorWebApiDependencyResolver : IDependencyResolver
{
private readonly Container container;
private readonly Scope scope;
public SimpleInjectorWebApiDependencyResolver(Container container) : this(container, beginScope: false)
{
Requires.IsNotNull(container, "container");
}
private SimpleInjectorWebApiDependencyResolver(Container container, bool beginScope)
{
this.container = container;
if (beginScope)
{
this.scope = container.BeginExecutionContextScope();
}
}
IDependencyScope IDependencyResolver.BeginScope()
{
return new SimpleInjectorWebApiDependencyResolver(this.container, beginScope: true);
}
object IDependencyScope.GetService(Type serviceType)
{
if (!serviceType.IsAbstract && typeof(IHttpController).IsAssignableFrom(serviceType))
{
return this.container.GetInstance(serviceType);
}
return ((IServiceProvider)this.container).GetService(serviceType);
}
IEnumerable<object> IDependencyScope.GetServices(Type serviceType)
{
return this.container.GetAllInstances(serviceType);
}
void IDisposable.Dispose()
{
if (this.scope != null)
{
this.scope.Dispose();
}
}
}
. CallContext.LogicalGetData , @Steven , . .
