“底层提供商在Open上失败”使用EF5代码首先使用Unity IoC在Azure工作人员角色针对SQL Azure时出错 [英] "The underlying provider failed on Open" Error when using EF5 Code First with Unity IoC in Azure Worker Role against SQL Azure

本文介绍了“底层提供商在Open上失败”使用EF5代码首先使用Unity IoC在Azure工作人员角色针对SQL Azure时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实体框架5代码首先,我使用存储库和工作单元模式,并使我的域模型,存储库和服务层在运行在Windows Azure云服务中的MVC应用程序中正常工作。我使用Unity for IoC,并根据需要注入存储库,控制器和服务类,并使用每个请求的生命周期。它的工作很棒。



然而,当我在Azure Worker角色中为域,存储库和EF5 DbContext使用相同的代码/类库时,指向到与MVC应用程序相同的SQL Azure数据库,我得到了我从MVC应用程序看不到的奇怪的错误。请注意,在这一点上,我只是读取(选择),没有更新事务。下面的错误消息似乎暗示它无法打开连接。



在Worker Role中,我有一个引导程序静态类来构建容器并注册所有服务在它。在工作人员的角色启动我必须做一些工作,所以我打电话给自动登录器注册服务,然后我解决了几个他们马上使用。这些服务注入了存储库,这些存储库又注入了我的DbContext,这些都是由IoC容器构建的。对于DbContext,我在IoC容器中使用Unity的HierarchicalLifeTimeManager。



当我的服务类尝试在运行worker角色时从数据库读取数据时,它会得到以下内容错误:

 底层提供程序在Open上失败。在System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition,DbConnection storeConnectionToOpen,DbConnection originalConnection,String exceptionCode,String attemptsOperation,Boolean& closeStoreConnectionOnFailure)
在System.Data.EntityClient.EntityConnection.Open()
在System.Data.Objects.ObjectContext.EnsureConnection()
在System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
在System.Data.Objects.ObjectQuery`1.System .Collections.Generic.IEnumerable< T> .GetEnumerator()
在System.Collections.Generic.List`1..ctor(IEnumerable`1集合)
在System.Linq.Enumerable.ToList [TSource ](IEnumerable`1 source)
在MyApp.Persistence.EF.RepositoryBase`1.Find(Expression`1 where,Expression`1 [] includeProperties)

这个完全相同的方法调用从MVC应用程序工作正常。我认为DbContext在工作角色的范围有问题,也许与IoC的一生有关,但这只是猜想。



有谁有任何想法可能是这个例外的原因?任何人使用EF,IoC,Repos / UoW和Azure Worker角色?建议?

解决方案

感谢Arthur Vickers对我的问题发表了评论,我将继续介绍这个短暂的错误由于Azure SQL数据库连接。我从来没有能够充分证明这一点,但是,我在Azure上创建了一个SQL虚拟机,并创建了我的数据库,然后将我的云服务应用程序改为指向该数据库。



监视日志,还没有看到相同的问题...



一个潜在的选项,如果我回到Azure SQL Db是滚动我自己的使用Microsoft P& P的瞬态故障处理块重试逻辑,或者使用像Rob Moore的ReliableDbProvider这样的项目( https:// github .com / robdmoore / ReliableDbProvider )。



当EF6出来时,我会研究使用内置的瞬态错误功能。



希望这有助于别人。


I am using Entity Framework 5 Code First, I use the repository and unit of work pattern, and have my domain models, repositories, and service layer working fine within an MVC app running in a Windows Azure cloud service. I'm using Unity for IoC and inject repositories, controllers, and service classes as required, and use a per-request lifetime. It has been working great.

However, when I use the same code/class libraries for the domain, repository, and the EF5 DbContext, in an Azure Worker Role, pointing to the same SQL Azure database as the MVC app, I get odd errors which I don't see from the MVC app. Note that at this point, I am just doing reads (select), no update transactions. The error message below seems to imply that it could not open a connection.

In the Worker Role, I have a bootstrapper static class to build up the container and register all the services in it. In the worker role start up I have to do some work, so I call the bootstrapper to register the services, then I resolve a couple of them to use right away. Those services get injected with repositories, which in turn get injected with my DbContext, all built up by the IoC container. For the DbContext, I am using Unity's HierarchicalLifeTimeManager in the IoC container.

When my service class tries to read data from the database while running the worker role, it gets the following error:

The underlying provider failed on Open.    at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)
   at System.Data.EntityClient.EntityConnection.Open()
   at System.Data.Objects.ObjectContext.EnsureConnection()
   at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at MyApp.Persistence.EF.RepositoryBase`1.Find(Expression`1 where, Expression`1[] includeProperties)

This exact same method call has been working fine from the MVC app. I am thinking there's something wrong with the scope of DbContext in the worker role, perhaps related to the lifetime in the IoC, but that's just conjecture.

Does anyone have any idea what could be the cause of this exception? Anyone using EF, IoC, Repos/UoW, and Azure Worker roles? Suggestions?

解决方案

Thanks to Arthur Vickers who commented on my question, I'll go ahead and chalk this on up to transient errors due to Azure SQL Database connectivity. I was never able to fully prove this, however, I brought up a SQL VM on Azure and created my DB there then changed my cloud service app to point to that DB instead.

Been monitoring the logs, and haven't seen the same issues yet...

One potential option, if I return to Azure SQL Db is to roll my own retry logic using Transient Fault Handling block from Microsoft P&P, or use a project like Rob Moore's ReliableDbProvider (https://github.com/robdmoore/ReliableDbProvider).

When EF6 comes out, I'll look into using the built in capabilities for transient errors.

Hope this helps someone else.

这篇关于“底层提供商在Open上失败”使用EF5代码首先使用Unity IoC在Azure工作人员角色针对SQL Azure时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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