我有一个简单的ASP.NET托管的WCF服务,它有几种方法,在使用实体框架时都具有相同的模式...同时调用ASP.NET托管的WCF服务时发生实体框架错误
public void Operation1(string username)
{
using(MyEFContext context = new MyEFContext())
{
UserInfo info = (from ui in context.UserInfos
where ui.User.Equals(username)
select ui).FirstOrDefault<UserInfo >();
info.LastAccess = DateTime.Now;
// Operation specific code, such as getting information using the EF context
context.SaveChanges();
}
}
这是一个简化的版本,以保持简单的例子,但即使这个简单的版本与我的生产代码集有相同的错误。代码首先使用实体框架获取用户信息,更新用户LastAccess字段,然后执行特定于操作的代码。操作特定的代码只是查询信息。最后它调用SaveChanges,这样LastAccess被保存回数据库。现在,这一切都很好,直到我的客户同时打两个电话。
客户端发出两个调用,每个调用不同的操作,但它们都具有相同的代码模式,如上所示。有时两个电话都会成功完成。但其他时候他们中的一个会产生错误和它可能是以下三种中......
System.Data.EntityException: The underlying provider failed on Open. --->
System.InvalidOperationException: The connection was not closed. The connection^s current
state is connecting.
System.Data.EntityCommandExecutionException: An error occurred while executing the command
definition. See the inner exception for details. ---> System.InvalidOperationException:
ExecuteReader requires an open and available Connection. The connection^s current state is
closed.
System.InvalidOperationException: Invalid attempt to read when no data is present.
显然我的EF和ASP.NET的理解是有缺陷的,因为我预计这两个操作的工作,即使在平行。我是否需要锁定每种方法,以便每次只出现一个方法?当然不是。有任何想法吗?
您是否正在使用Per Call服务激活,这意味着您对WCF服务的每次调用是否都会导致创建服务的单独实例? – mclark1129
这是Per-Call的默认值。 –
看看这个问题,它指向在IIS中托管时使用集成安全性的问题。你是否在默认的应用程序池中托管这个应用程序?:http://stackoverflow.com/questions/2475008/the-underlying-provider-failed-on-open – mclark1129