System.InvalidOperationException with ASP.NET MVC, EF6 and npgsql 3 rc1

Hi

I am getting an exception when trying to use the version 3 rc1 EF library, because of a call to an async void method. The method in question is NpgsqlConnectorPool.ReleaseConnector.

To reproduce the error I started with the ASP.NET MVC template, set it up to use npgsql, added a new model, and scaffolded a controller and views for the model. I see the exception when I navigate to the index view for the model.

I tested the same code with an edited version of npgsql, where I had changed the NpgsqlConnectorPool.ReleaseConnector method to not be async. This worked with no exception.

The versions of the libraries I am using are

  • EntityFramework 6.1.3
  • Npgsql 3.0.0-rc0001
  • EntityFramework6.Npgsql 3.0.0-rc0001

The full details of the exception are

InvalidOperationException: An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it.
   System.Web.AspNetSynchronizationContext.OperationStarted() +4962894
   System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Create() +180
   Npgsql.NpgsqlConnectorPool.ReleaseConnector(NpgsqlConnection connection, NpgsqlConnector connector) +225
   Npgsql.NpgsqlConnection.ReallyClose() +391
   Npgsql.NpgsqlConnection.Close() +242
   Npgsql.NpgsqlConnection.Dispose(Boolean disposing) +95
   System.ComponentModel.Component.Dispose() +27
   System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<Dispose>b__c(DbConnection t, DbConnectionInterceptionContext c) +102
   System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) +269
   System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Dispose(DbConnection connection, DbInterceptionContext interceptionContext) +653
   System.Data.Entity.Internal.RepositoryBase.DisposeConnection(DbConnection connection) +169
   System.Data.Entity.Migrations.History.HistoryRepository.QueryExists(String contextKey) +1680
   System.Data.Entity.Migrations.History.HistoryRepository.Exists(String contextKey) +137
   System.Data.Entity.Migrations.History.HistoryRepository.GetLastModel(String& migrationId, String& productVersion, String contextKey) +167
   System.Data.Entity.Internal.InternalContext.QueryForModel(DatabaseExistenceState existenceState) +113
   System.Data.Entity.Internal.ModelCompatibilityChecker.CompatibleWithModel(InternalContext internalContext, ModelHashCalculator modelHashCalculator, Boolean throwIfNoMetadata, DatabaseExistenceState existenceState) +177
   System.Data.Entity.Internal.InternalContext.CompatibleWithModel(Boolean throwIfNoMetadata, DatabaseExistenceState existenceState) +171
   System.Data.Entity.Database.CompatibleWithModel(Boolean throwIfNoMetadata, DatabaseExistenceState existenceState) +83
   System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context) +362
   System.Data.Entity.Internal.<>c__DisplayClassf`1.<CreateInitializationAction>b__e() +222
   System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +116
   System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +748
   System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) +48
   System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) +238
   System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) +424
   System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() +165
   System.Data.Entity.Internal.InternalContext.Initialize() +70
   System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +45
   System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +155
   System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() +50
   System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() +160
   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +446
   System.Linq.Enumerable.ToList(IEnumerable`1 source) +80
   Npgsql3Test.Controllers.DummiesController.Index() in c:\Users\ValourAndArms\Documents\Visual Studio 2013\Projects\Npgsql3Test\Npgsql3Test\Controllers\DummiesController.cs:20
   lambda_method(Closure , ControllerBase , Object[] ) +113
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +81
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +752
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +93
   System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod() +90
   System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +45
   System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +211
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +196
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +141
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +65
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +189
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +717
   System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +66
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +71
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +196
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +141
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +65
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +103
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +330
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +71
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +196
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +73
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +58
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +90
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +188
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +196
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +73
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +50
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +68
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +60
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +85
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +196
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +73
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +50
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +58
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +93
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +188
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +196
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +73
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +50
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +58
 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +59
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288