2017-08-14 60 views
0

我使用ASP.NET MVC Bundling捆綁了我的JavaScript文件。當我檢查並檢查網絡在我的瀏覽器中,我看到500 Internal Server Error當我的包想要加載。 響應:500 ASP.NET MVC包中的內部服務器錯誤

在上一次異步操作完成之前,在此上下文上開始第二個操作。在調用此上下文中的另一個方法之前,請使用'await'來確保任何異步操作已完成。任何實例成員不保證是線程安全的。

我知道這個錯誤發生在我的GenerateIdentityAsync方法中。 我使用ASP.NET 2.0的身份,所以我在Owin啓動類使用它從OnValidateIdentity

ApplicationManager.cs

public class ApplicationManager : IApplicationManager 
{ 
    // more code 
    public Func<CookieValidateIdentityContext, Task> OnValidateIdentity() 
    { 
     return SecurityStampValidator.OnValidateIdentity<AppUserManager, User, Guid> 
     (TimeSpan.FromSeconds(0), GenerateIdentityAsync, identity => Guid.Parse(identity.GetUserId())); 
    } 

    public async Task<ClaimsIdentity> GenerateIdentityAsync(AppUserManager manager, User user) 
    { 
     var identity = 
     await CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie).ConfigureAwait(true); 
    identity.AddEnumClaim("lastSeenMode", user.LastSeenMode); 
    identity.AddClaim(new Claim("emailAddress", user.Email)); 
    identity.AddClaim(new Claim("displayName", user.DisplayName)); 
    identity.AddClaim(new Claim("lastseen", user.LastSeen.ToString())); 
    return identity; 
    } 
} 

startup.cs

app.UseCookieAuthentication(new CookieAuthenticationOptions() 
    { 
     Provider = new CookieAuthenticationProvider() 
     { 
      OnValidateIdentity = SmObjectFactory.Container.GetInstance<IAppUserManager>().OnValidateIdentity(), 
     } 
    }); 

堆棧

[NotSupportedException: A second operation started on this context before a previous asynchronous operation completed. Use &#39;await&#39; to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe.] 
System.Data.Entity.Internal.ThrowingMonitor.EnsureNotEntered() +4906656 
System.Data.Entity.Internal.Linq.InternalSet`1.FindAsync(CancellationToken cancellationToken, Object[] keyValues) +73 
System.Data.Entity.DbSet`1.FindAsync(CancellationToken cancellationToken, Object[] keyValues) +35 
System.Data.Entity.DbSet`1.FindAsync(Object[] keyValues) +26 
Microsoft.AspNet.Identity.EntityFramework.EntityStore`1.GetByIdAsync(Object id) +49 
Microsoft.AspNet.Identity.EntityFramework.&lt;GetUserAggregateAsync&gt;d__6c.MoveNext() +201 
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92 
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 
Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +59 
Microsoft.AspNet.Identity.&lt;GetSecurityStampAsync&gt;d__42.MoveNext() +302 
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92 
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 
Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +59 
Microsoft.AspNet.Identity.&lt;CreateAsync&gt;d__0.MoveNext() +860 
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92 
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 
System.Runtime.CompilerServices.ConfiguredTaskAwaiter.GetResult() +28 
App.ServiceLayer.UserService.&lt;GenerateIdentityAsync&gt;d__16.MoveNext() in C:\Repository\Barnamenevis.ir\App.ServiceLayer\UserService\AppUserManager.cs:127 
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92 
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 
Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +59 
Microsoft.AspNet.Identity.Owin.&lt;&lt;OnValidateIdentity&gt;b__1&gt;d__4.MoveNext() +1581 
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92 
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 
System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) +25 
Microsoft.Owin.Security.Cookies.&lt;AuthenticateCoreAsync&gt;d__2.MoveNext() +2153 
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92 
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 
Microsoft.Owin.Security.Infrastructure.&lt;BaseInitializeAsync&gt;d__0.MoveNext() +571 
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92 
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 
Microsoft.Owin.Security.Infrastructure.&lt;Invoke&gt;d__0.MoveNext() +255 
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92 
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 
Microsoft.AspNet.Identity.Owin.&lt;Invoke&gt;d__0.MoveNext() +404 
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92 
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 
Microsoft.AspNet.Identity.Owin.&lt;Invoke&gt;d__0.MoveNext() +404 
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92 
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 
Microsoft.AspNet.Identity.Owin.&lt;Invoke&gt;d__0.MoveNext() +404 
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92 
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 
Microsoft.AspNet.Identity.Owin.&lt;Invoke&gt;d__0.MoveNext() +404 
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92 
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.&lt;RunApp&gt;d__5.MoveNext() +182 
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92 
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.&lt;DoFinalWork&gt;d__2.MoveNext() +180 
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar) +69 
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar) +64 
System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +380 
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +155 

也我測試ConfigureAwait(true)ConfigureAwait(false)和沒有ConfigureAwait但沒有解決這個問題。

更新
此外,當我使用EF Profiler,它說使用單個對象上下文在多線程可能是一個錯誤。

此警報只是爲了同一個表存在:

[Admin].[UserClaims] 
[Admin].[UserLogin] 
[Admin].[Roles] 
[Admin].[UserRoles] 
[Admin].[Users] 

我怎樣才能解決這個問題呢?

更多的代碼在Gist

+1

您某處在同一時間啓動了兩個對數據庫的異步請求。使用await檢查你錯過的地方。 –

+0

我不會錯過使用'await'。 –

+0

你可以添加post異常調用堆棧嗎? –

回答

2

我不是由我自己使用StructureMap,但我發現,對於MVC使用StructureMap與MVC最好的辦法是安裝StructureMap.MVC5.Update包(這對最新StructureMap版)。
檢查小intro如何使用它。或this文章。

將您現有的所有註冊碼移動到由包安裝的DefaultRegistry.cs中。現在不要使用HybridHttpOrThreadLocalScoped()進行服務註冊。