简体   繁体   English

NServiceBus和实体框架导致异常-基础提供程序在打开时失败

[英]NServiceBus and Entity Framework causes exception - The underlying provider failed on Open

I am having trouble understanding and troubleshooting the following exception in an endpoint. 我无法理解端点中的以下异常并对其进行故障排除。 I have seen many posts on this error on SO, yet no question reflected the specific scenario in our application. 我在SO上看到过很多关于此错误的文章,但毫无疑问反映了我们应用程序中的特定情况。

This happens intermittently , and I previously attempted to resolve this by increasing both the connection and command timeout for Entity Framework. 这是间歇性发生的 ,我之前曾尝试通过增加Entity Framework的连接和命令超时来解决此问题。

The endpoint and database are running on the same machine . 端点和数据库在同一台计算机上运行 The error does not happen very often, only maybe for larger updates or deletes. 该错误不会经常发生,仅可能发生于较大的更新或删除。

As a side note, I routinely get the "The underlying provider failed on Open" while debugging . 附带说明一下, 在调试时 ,我通常会得到“底层提供程序在Open上失败”的信息。 I always believed this was because I spent too much time on a breakpoint, and the connection was closed when I continue. 我一直认为这是因为我在断点上花费了太多时间,并且当我继续时连接已关闭。

I wanted to ask what the best practice is for using/injecting an Entity Framework DbContext in an NServiceBus endpoint ? 我想问一下在NServiceBus端点中使用/注入Entity Framework DbContext最佳实践是什么? Is it best to inject the context via NSB's dependency injection? 是否最好通过NSB的依赖项注入来注入上下文? Like so: 像这样:

public class ConfigureDependencyInjection : INeedInitialization
{
    public void Customize( BusConfiguration configuration )
    {
        configuration.RegisterComponents( reg =>
        {
            reg.ConfigureComponent<MyDbContext>( DependencyLifecycle.InstancePerCall );
        } );
    }
}

or should I not be using this, but instead instantiate the context as needed: 还是我不应该使用它,而是根据需要实例化上下文:

using (var context = new MyDbContext()) { ... }

The message that is handled here is one single physical message, very straight forward -- which is why I opted for DependencyLifecycle.InstancePerCall. 此处处理的消息是一条单独的物理消息,非常简单明了-这就是为什么我选择DependencyLifecycle.InstancePerCall的原因。

But when is DependencyLifecycle.InstancePerUnitOfWork the correct option when using Entity Framework? 但是,在使用实体框架时,DependencyLifecycle.InstancePerUnitOfWork何时是正确的选项?

What can I do to better debug this error? 我该怎么做才能更好地调试此错误?

Here is the exception, and stack: 这是例外,还有堆栈:

The underlying provider failed on Open.
The operation is not valid for the state of the transaction.

at System.Transactions.TransactionState.EnlistPromotableSinglePhase(InternalTransaction tx, IPromotableSinglePhaseNotification promotableSinglePhaseNotification, Transaction atomicTransaction, Guid promoterType)
at System.Transactions.Transaction.EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification promotableSinglePhaseNotification, Guid promoterType)
at System.Transactions.Transaction.EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification promotableSinglePhaseNotification)
at System.Data.SqlClient.SqlInternalConnection.EnlistNonNull(Transaction tx)
at System.Data.SqlClient.SqlInternalConnection.Enlist(Transaction tx)
at System.Data.ProviderBase.DbConnectionInternal.ActivateConnection(Transaction transaction)
at System.Data.ProviderBase.DbConnectionPool.PrepareConnection(DbConnection owningObject, DbConnectionInternal obj, Transaction transaction)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
--- End of inner exception stack trace ---
at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection(Boolean shouldMonitorTransactions)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
at System.Data.Entity.Core.Objects.DataClasses.EntityCollection`1.Load(List`1 collection, MergeOption mergeOption)
at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.DeferredLoad()
at System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.LoadProperty[TItem](TItem propertyValue, String relationshipName, String targetRoleName, Boolean mustBeNull, Object wrapperObject)
at System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.<>c__DisplayClass7`2.<GetInterceptorDelegate>b__1(TProxy proxy, TItem item)
at System.Data.Entity.DynamicProxies.JournalTransaction_4CAE8D09C8D2614F98562EAA87E63CE3B6D4E9A6DEC760505C0C7C7C42295ECE.get_TransactionItems()

This is for NSB 5.2.14, though earlier versions show the same behaviour. 这适用于NSB 5.2.14,尽管早期版本显示相同的行为。

Thanks for your help. 谢谢你的帮助。

John, this error can also occur when the connection has been opened and closed already, perhaps this is the problem. 约翰,当连接已经打开和关闭时,也会发生此错误,也许这就是问题所在。 But as it seems it tries to enlist in a distributed transaction and is failing there. 但是,它似乎试图加入分布式事务,但在此失败。 And if it is while debugging, how long are you debugging? 如果是在调试时,您要调试多长时间? It might be that the connection timed out and that it retries to open a closed connection? 可能是连接超时并且重试打开了关闭的连接? What is your connection timeout? 您的连接超时是多少? What transport are you using? 您正在使用什么交通工具? Is MSDTC on? MSDTC是否打开? And do you have any idea when it starts failing? 并且您有什么想法,当它开始失败? Is it a different message or the same? 它是不同的消息还是相同的?

Not sure how you're MyDbContext behaves, but mine always look like this: 不确定您的MyDbContext行为如何,但是我的总是这样:

public class MyDbContext : DbContext
{
  public MyDbContext : base("MyConnectionString")
  {
  }
}

That way I make sure it picks a specific connectionstring and I know I won't forget it, and what not. 这样,我可以确保它选择了一个特定的连接字符串,而且我知道我不会忘记它,不会。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架异常“底层提供程序打开失败” - Entity Framework Exception “The underlying provider failed on Open” 实体框架给出异常:“底层提供程序在打开时失败。” - Entity Framework giving exception : “The underlying provider failed on Open.” 基础提供程序在实体框架中打开时失败 - The underlying provider failed on Open in entity framework connection 基础提供程序无法打开-WPF和实体框架 - The underlying provider failed to open - WPF and Entity FrameWork 实体框架底层提供程序在打开时失败 - Entity Framework The underlying provider failed on Open 使用WEB API和Entity Framework Project的IIS本地计算机上的“基础提供程序无法打开”异常 - “The underlying provider failed to Open” exception on IIS Local Machine using WEB API and Entity Framework Project 具有SQLite异常的实体框架:底层提供程序在Commit上失败 - Entity Framework with SQLite exception: The underlying provider failed on Commit 在实体框架上下文中使用单例模式-基础提供程序在打开时失败 - Using singleton pattern with Entity Framework context - The underlying provider failed on open 实体框架和SQL Server CE-基础提供程序在打开时失败 - Entity Framework & SQL Server CE - Underlying Provider Failed On Open SQLite&Entity Framework 6“基础数据提供程序无法打开” - SQLite & Entity Framework 6 “The underlying data provider failed to open”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM