4

I have a simple ASP.NET hosted WCF Service that has several methods that all have the same pattern when it comes to using the Entity Framework...

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();
    }
}

This is a cut down version to keep the example simple but even this simple version has the same error as my production set of code. The code starts by getting the users information using the Entity Framework, updates the users LastAccess field and then performs the operation specific code. The operation specific code just makes queries for information. At the end it calls SaveChanges so the LastAccess is saved back to the database. Now this all works perfectly fine until my client makes two calls in parallel.

The client makes two calls, each to a different operation but they both have the same pattern of code as seen above. Sometimes both calls complete with success. But other times one of them will produce as error and it could be any of the following three...

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.

Clearly my understanding of EF and ASP.NET is flawed because I expected the two operations to work even in parallel. Do I need to put a lock around each method so they only occur one at a time? Surely not. Any ideas?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Phil Wright
  • 22,580
  • 14
  • 83
  • 137
  • Are you using Per Call service activation, meaning does each call to your WCF service result in a separate instance of the service being created? – mclark1129 Aug 28 '12 at 00:28
  • It is the default of Per-Call. – Phil Wright Aug 28 '12 at 00:34
  • Take a look at this question, in it they point to issues using integrated security while hosting in IIS. Are you hosting this app in the default app pool?: http://stackoverflow.com/questions/2475008/the-underlying-provider-failed-on-open – mclark1129 Aug 28 '12 at 00:38
  • 1
    Found the answer here http://stackoverflow.com/questions/2575485/managing-entityconnection-lifetime. Turns out that reusing the same EntityConnection instance for all my contexts was the issue. So now I create a new one each time. – Phil Wright Aug 28 '12 at 01:02

1 Answers1

2

Found the answer here Managing EntityConnection lifetime. Turns out that reusing the same EntityConnection instance for all my contexts was the issue. So now I create a new one each time.

Community
  • 1
  • 1
Phil Wright
  • 22,580
  • 14
  • 83
  • 137