如何使用ASP.NET MVC,的WebAPI,和MEF每个请求正常范围成分 [英] How to properly scope composition per request using ASP.NET MVC, WebAPI, and MEF

查看:524
本文介绍了如何使用ASP.NET MVC,的WebAPI,和MEF每个请求正常范围成分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我利用各种资源,包括本SO回答<一个MEF添加到一个MVC /应用的WebAPI href=\"http://stackoverflow.com/questions/13566688/how-to-integrate-mef-with-asp-net-mvc-4-and-asp-net-web-api/19326535#19326535\">How以MEF与ASP.NET MVC 4和ASP.NET Web API集成。虽然这工作了一段时间,我开始收到有关制作到数据库中,最常见的一个是连接间歇性错误:System.InvalidOperationException:。超时时间已超时时间从池中获取这有可能连接之前已过已经发生,因为所有池连接使用,最大池大小达到了。

I recently added MEF to an MVC/WebAPI application using a variety of resources including this SO answer How to integrate MEF with ASP.NET MVC 4 and ASP.NET Web API. While this worked for a time, I started to receive intermittent errors related to making connections to the database, the most frequent one being: "System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached."

我意识到我渗漏连接,但不明白为什么。我所有的资料库中实现了IDisposable,完成后售出其连接。在我的处置方法把断点很快发现,他们从来没有被击中。当我以我的code关闭连接到上面的例子中,我注意到没有任何清理,但作为新MEF和MVC我错误地认为清理工作正在MVC中的/ MEF的依赖管道的地方进行。

I realized I was leaking connections but didn't understand why. All of my repositories implemented IDisposable and disposed of their connections when done. Putting breakpoints in my dispose methods quickly revealed that they were never being hit. When I based my code off of the example linked to above, I noticed the lack of any cleanup, but being new to MEF and MVC I wrongly assumed that cleanup was being done somewhere in MVC's/MEF's dependency pipeline.

我不知道其他人如何使用MEF解决在两个MVC和的WebAPI在适当范围内组成每个请求的依据是什么?

I'm wondering how other people have tackled using MEF to properly scope composition on a per request basis in both MVC and WebAPI?

我已经在这里和那里发现了模糊的指导和它的朝任MVC或全部的WebAPI齿轮。 。的https:MEF codePLEX这里有一个几乎完全MVC中心的解决方案// MEF 。codeplex.com /发行/查看/ 79090 ,但它是基于关闭的MVC的preVIEW版本。我发现了一个解决方案的WebAPI这里: https://github.com/WebApiContrib/WebApiContrib.IoC.Mef 。我滚动我自己的解决方案的时刻,但我很讨厌推倒重来,我想我会问问看是否有人知道一个滚动的身边了。

I have found vague guidance here and there and it's all geared toward either MVC or WebAPI. Mef.codeplex has an almost complete MVC centric solution here: https://mef.codeplex.com/releases/view/79090 but it's based off of a preview version of MVC. I found a WebAPI solution here: https://github.com/WebApiContrib/WebApiContrib.IoC.Mef. I'm rolling my own solution at the moment but as I hate to reinvent the wheel, I thought I'd ask to see if anyone knew of one rolling around already.

推荐答案

我最终没有找到任何东西让我满意后,解决这个问题我自己在节日期间。在codePLEX MEF的contrib有一个良好的开端,但它是没有完成。我用几个修改掺入它,并结合与一些研究和试验和错误。

I ended up tackling this problem myself over the holidays after not finding anything to my satisfaction. MEF contrib on CodePlex had a good start but it was unfinished. I incorporated it with a few modification and combined that with some research and trial and error.

我创建在Github上的一个项目(以下链接,我知道的外部链接是令人难以接受的,但它只是太多code,包括内联)。在这四个项目。第一提供核心组合物和拆卸,两个库把核心分别成MVC和的WebAPI的范围内,而最后仅仅是一个快速采样MVC应用程序与两个控制器的每个取决于所注射的另一个类。一个需要注意的,我考虑的WebAPI项目未完成的,因为它还不包括的WebAPI过滤器供应商设施(也许其他的东西我都没有想到的或需要的还)。

I've created a project on Github (link below, I know external links are frowned upon but it's just too much code to include inline). In it are four projects. The first provides core composition and teardown, the two libraries put the core into the context of MVC and WebAPI respectively, and the last is just a quick sample MVC app with two controllers that each depend on another class which is injected. One caveat, I consider the WebAPI project unfinished as it doesn't yet include facilities for WebAPI filter providers (and maybe other things I haven't thought of or needed yet).

我希望这有助于。

https://github.com/rlvandaveer/Heliar-Web-Composition

这篇关于如何使用ASP.NET MVC,的WebAPI,和MEF每个请求正常范围成分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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