使用SimpleInjector有没有办法复制RegisterWebApiRequest< T>()与.NET 4/1的WebAPI? [英] Using SimpleInjector Is there a way to replicate RegisterWebApiRequest<T>() with .net 4/webapi 1?

查看:225
本文介绍了使用SimpleInjector有没有办法复制RegisterWebApiRequest< T>()与.NET 4/1的WebAPI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个问题的跟进。我通过例如使用SimpleInjector和的WebAPI的工作。不幸的是,在这里我想使用的WebAPI KB2568167 并的 KB2915689 prevent我从升级到.NET 4.5。所以我坚持使用.NET 4.0和放大器;此刻的WebAPI V1(4.0.30506.0)。

有没有一种方法,以复制 RegisterWebApiRequest< T>?()与旧版本的WebAPI的作用域

虽然我的NU-获得软件包只.NET 4.5的版本,我可以下载code,并得到了一个框架4.0编译没有太多的麻烦。打电话时 VAR容器= request.GetDependencyScope()在我的消息处理一个 SimpleInjectorWebApiDependencyResolver 类返回。试图检索一个实例从容器中像这样:

  VAR POCO =(SimplePOCO)container.GetService(typeof运算(SimplePOCO));

在以下错误:

结果

  

已注册的委托类型SimplePOCO引发了异常。该
  SimplePOCO注册为网络API请求的生活方式,但
  实例请求一个网页API请求的上下文之外。


我只思念在我的配置的东西吗?是否有其他 - 就像创造我自己的消息处理程序


更新

张贴 codePLEX问题后,我又回到了基础。我把一个普通的的mvc的WebAPI项目,引用我的 SimpleInjector SimpleInjector.Integration.WebApi 的编译和 SimpleInjector.Extensions.ExecutionContextScoping

就像@blueling我能得到它在一个消息处理工作。

那么,有什么不同呢?我想过的是,我不工作的项目是光秃秃的骨头 - 只是的WebAPI和超薄的web.config。脚手架和绒毛来与基地项目模板都不是我的解决方案。明天我打算工作例子比较非工作一个参考通过引用而web.config中在一个时间设定。


更新2

那么一点点更多的调试,果然处置()被调用的DependencyResolver实现,而不是由我....


  



解决方案

我能解决这个问题。我并不完全清楚的为什么的处置正在呼吁 SimpleInjectorWebApiDependencyResolver ,但这里是我想通了:

BAD 依赖解析器执行是的一个在这里所示:

 公共密封类SimpleInjectorWebApiDependencyResolver:的IDependencyResolver
{
  私人只读集装箱货柜;  公共SimpleInjectorWebApiDependencyResolver(集装箱货柜)
  {
    this.container =容器;
  }  公共IDependencyScope BeginScope()
  {
    返回此;
  }  公共对象GetService的(类型的serviceType)
  {
    返回((的IServiceProvider)this.container).GetService(的serviceType);
  }  公共IEnumerable的<对象> GetServices(类型的serviceType)
  {
    返回this.container.GetAllInstances(的serviceType);
 } 公共无效的Dispose()
 {
 }
}

我注意到有源$ C ​​$ C我下载<一个有点不同的副本href=\"https://simpleinjector.$c$cplex.com/SourceControl/latest#SimpleServiceLocator/SimpleInjector.Integration.WebApi/SimpleInjectorWebApiDependencyResolver.cs\"相对=nofollow>此处。

 公共密封类SimpleInjectorWebApiDependencyResolver:的IDependencyResolver
{
    私人只读集装箱货柜;
    私人只读范围范围;    公共SimpleInjectorWebApiDependencyResolver(集装箱货柜):这个(集装箱,beginScope:FALSE)
    {
        Requires.IsNotNull(容器,容器);
    }    私人SimpleInjectorWebApiDependencyResolver(集装箱货柜,布尔beginScope)
    {
        this.container =容器;        如果(beginScope)
        {
            this.scope = container.BeginExecutionContextScope();
        }
    }    IDependencyScope IDependencyResolver.BeginScope()
    {
        返回新SimpleInjectorWebApiDependencyResolver(this.container,beginScope:真正的);
    }    对象IDependencyScope.GetService(类型的serviceType)
    {
        如果(serviceType.IsAbstract&安培;!&安培; typeof运算(IHttpController).IsAssignableFrom(的serviceType))
        {
            返回this.container.GetInstance(的serviceType);
        }        返回((的IServiceProvider)this.container).GetService(的serviceType);
    }    IEnumerable的&LT;对象&gt; IDependencyScope.GetServices(类型的serviceType)
    {
        返回this.container.GetAllInstances(的serviceType);
    }    无效IDisposable.Dispose()
    {
        如果(this.scope!= NULL)
        {
            this.scope.Dispose();
        }
    }
}

切换到该版本之后的一切工作。的我仍然有潜在的问题CallContext.LogicalGetData和嵌套执行上下文,因为@Steven还跟在评论中指出。因此,在您自担风险使用此解决方案

Following up on this question. I'm working through example of using SimpleInjector and WebAPI. Unfortunately, where I want to utilize WebAPI KB2568167 and KB2915689 prevent me from upgrading to .net 4.5. So I'm stuck using .net 4.0 & WebAPI v1 (4.0.30506.0) at the moment.

Is there a way to replicate the RegisterWebApiRequest<T>() scoping with the older version of WebAPI?

While I the nu-get packages only contain .net 4.5 versions, I was able to download the code and get a framework 4.0 compile without much trouble. When calling var container = request.GetDependencyScope() in my Message Handler a SimpleInjectorWebApiDependencyResolver class is returned. Attempting to retrieve an instance out of the container like so:

  var poco = (SimplePOCO) container.GetService(typeof(SimplePOCO));

results in the following error::

The registered delegate for type SimplePOCO threw an exception. The SimplePOCO is registered as 'Web API Request' lifestyle, but the instance is requested outside the context of a 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 issue, I went back to basics. I took a plain vanilla Mvc WebApi project, referenced my compiles of SimpleInjector, SimpleInjector.Integration.WebApi, and SimpleInjector.Extensions.ExecutionContextScoping.

Like @blueling I was able to get it working in a message handler.

So what's different? One thought I had is that my non-functioning project is bare bones -- just WebApi and slim web.config. None of the scaffolding and fluff that come with the base project templates are in my solution. Tomorrow I plan to compare the work example to the non-working one reference-by-reference and web.config setting at a time.


UPDATE 2

So a little more debugging, and sure enough Dispose() is being called on the DependencyResolver implementation, but not by me....

解决方案

I was able to resolve this problem. I'm not entirely clear why dispose was being called on SimpleInjectorWebApiDependencyResolver, but here's what I figured out:

The BAD Dependency resolver implementation was a copy of the one listed here:

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()
 {
 }
}

I noticed there is a bit different copy in the source code I downloaded here.

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();
        }
    }
}

After switching over to this version everything worked. I still have potential issues CallContext.LogicalGetData and Nested Execution Contexts, as @Steven was kind enough to point out in the comments. So use this solution at your own risk.

这篇关于使用SimpleInjector有没有办法复制RegisterWebApiRequest&LT; T&GT;()与.NET 4/1的WebAPI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆