c# - EF6 和 azure 辅助角色 : underlying provider fail on open

标签 c# entity-framework azure entity-framework-6 azure-worker-roles

我正在努力使用 EntityFramework 6 和 PaaS 架构。 我有一个存储库项目,它调用 DAL 项目来执行一些 EF6 导入的存储过程。直到最近,我们一直在寻求 IaaS 架构,但由于某些原因,我们转向了 PaaS。 WCF 服务已成功使用同一存储库。此 WCF 服务已转换为 Web 角色,并且工作起来就像一个魅力。我现在在辅助角色中使用相同的存储库来使服务总线出队并处理数据(由 Web 角色入队)。 但后来我在第一次通过 EF6 调用存储过程(获取请求)时使用存储库时遇到错误

Schedule worker error with inner exception : A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) The underlying provider failed on Open. 
at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass45`1.b__43()
at System.Data.Entity.Infrastructure.DbExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteFunction[TElement](String functionName, ExecutionOptions executionOptions, ObjectParameter[] parameters)
at XXX.DBContext.XXXEntities.GetTrades(Nullable`1 id, Nullable`1 entityBuy, Nullable`1 entitySell, Nullable`1 sessionId, Nullable`1 orderBuy, Nullable`1 orderSell)
at XXX.RepositoryServices.MarketPlaceService.GetTradeInstances(Nullable`1 EntityBuy, Nullable`1 EntitySell, Nullable`1 SessionId, Nullable`1 OrderBuyId, Nullable`1 OrderSellId)
at WorkerRole1.WorkerRole.Run()

(XXX 和 YYY 是命名空间,但由于政策原因我无法显示它们) 我尝试为 Db(托管在 IaaS 中)上的 azure 设置 IP 从 0.0.0.0 到 0.0.0.4 的防火墙异常(exception)。 我添加了一个继承自 DbConfiguration 的配置类,它在 ctor 中的配置如下:

this.SetExecutionStrategy("System.Data.SqlClient", () => SuspendExecutionStrategy
? (IDbExecutionStrategy)new DefaultExecutionStrategy()
:new System.Data.Entity.SqlServer.SqlAzureExecutionStrategy(5,TimeSpan.FromMilliseconds(25)));

(使用 SuspendExecutionStrategy = true) 我确保将正确版本的 EntityFramework 和 EntityFramework.SqlServer dll 复制到 cspkg 中。

我的连接字符串也很好(里面有凭据)。我确信最后一部分是因为我可以在我的辅助角色 Run 方法以及使用此存储库的类中成功使用 ADO.NET sql 查询。 我尝试使用最新版本的 EF6(即 6.1),但它不起作用。 我尝试将我的工作人员置于与 Web 角色相同的子网中(不起作用)。 我尝试在连接字符串中使用 SqlServer 的 IP 地址,但没有成功。 ADO.NET 和 EF6 使用相同的连接字符串。

    <add name="XXXEntities" connectionString="metadata=res://*/XXXContext.csdl|res://*/XXXContext.ssdl|res://*/XXXConte xt.msl;provider=System.Data.SqlClient;provider connection string=&quot;data  source=negobdd1.YYY.com;initial catalog=XXX;user id=[User];password= [Password];MultipleActiveResultSets=True;App=EntityFramework&quot;"  providerName="System.Data.EntityClient" />

(很抱歉所有繁文缛节,但这是客户要求。而且所有 XXX 都是完全相同的字符串) 我尝试从托管辅助角色的 Azure VM 计算机进行连接,以使用地址“negobdd1.YYY.com”以及连接字符串的用户名和密码成功连接到带有 .udl 文件的数据库。 我也可以成功 ping SqlServer 机器。

编辑

上下文是这样创建的

    public partial class NegotiationsPlatformEntities : DbContext
    {
        public NegotiationsPlatformEntities()
        : base("name=NegotiationsPlatformEntities")
        {
        }

    // auto-generated methods here

    }

通过这个实例

     internal NegotiationsPlatform.DBContext.NegotiationsPlatformEntities db = new NegotiationsPlatform.DBContext.NegotiationsPlatformEntities();

除了连接字符串的名称之外,我没有设置任何特殊参数。

重新编辑

查看 DbContext.Database.Connection.Datasource 后,我发现显然 EF6 的目标是本地临时数据库服务器,而不是 Azure IaaS SqlServer。 我会调查并回复。

任何帮助将不胜感激。

谢谢。

最佳答案

事实证明,辅助角色处理连接字符串文件的方式与 Web 角色不同。 因为有多个部署环境,所以我们有多个名为“connectionStrings”+[target]+“.config”的文件。

在 Web 角色的 OnStart 方法中,我们使用 .bat 文件删除任何不必要的配置文件,并将所需文件重命名为“connectionStrings.config”。这样,在部署在 Azure 上的 webrole 中,它只保留 PaaS 配置文件,然后使用它。 但显然,它在 worker 角色中的工作方式并不相同。

执行清理“.bat”文件,只留下一个包含正确内容的配置文件,但使用的是默认配置文件中的内容。所以我猜工作人员在调用 OnStart 方法之前加载配置文件,因此任何更改都无关紧要。 (我没有尝试终止工作进程以查看重新启动后是否加载第一次部署中剩余的唯一好文件)

所以这是我的解决方案:删除工作项目中除 PaaS 之外的所有配置文件,并且不要依赖 OnStart 期间使用的“.bat”文件。

非常感谢 Dean Ward 让我走上了正确的道路:) 总而言之,这“只是”连接字符串的问题。

关于c# - EF6 和 azure 辅助角色 : underlying provider fail on open,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24124633/

相关文章:

c# - 反射获取 FieldInfo 对象的类型?

c# - 使用 Moq 测试对不同类的调用

asp.net - 从 GridView 中的 2 列动态生成超链接

azure - KQL Kusto 使用一个项目重命名重命名多个列

c# - nHibernate 不解析所有字段的属性

c# - .NET 中的 WebRequest 异常

c# - Owin 授权超时后 EF 抛出 AspNetUsers 错误

c# - 使用 Entity Framework 将 MySQL 记录写入数组

c# - Firefox 没有 Access-Control-Allow-Origin,但适用于 Chrome 和 Safari

powershell - 通过 Powershell 在 CosmosDb 中创建集合