“The underlying provider failed on Open” Error when using EF5 Code First with Unity IoC in Azure Worker Role against SQL Azure

回眸只為那壹抹淺笑 提交于 2019-12-24 09:36:10

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/17532233/the-underlying-provider-failed-on-open-error-when-using-ef5-code-first-with-un

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!