ASP.NET MVC捆绑包中的500内部服务器错误

时间:2017-08-14 11:48:29

标签: c# asp.net-mvc asynchronous async-await bundle

我将我的JavaScript文件与ASP.NET MVC Bundling捆绑在一起。当我在浏览器中检查并检查网络标签时,当我的软件包想要加载时,我会看到500 Internal Server Error。 响应:

  

在上一次异步操作完成之前,在此上下文中启动了第二个操作。使用'等待'确保在调用此上下文中的另一个方法之前已完成任何异步操作。不保证任何实例成员都是线程安全的。

我了解此错误发生在我的GenerateIdentityAsync方法中。 我使用ASP.NET Identity 2.0,因此我在Owin Startup类的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 个答案:

答案 0 :(得分:2)

我自己不使用StructureMap,但我发现对于MVC,使用StructureMap和MVC的最佳方法是安装StructureMap.MVC5.Update包(这是最新的StructureMap版本)。
检查小intro如何使用它。或this文章。

将所有现有注册码移至Package安装的DefaultRegistry.cs。现在不要使用HybridHttpOrThreadLocalScoped()进行服务注册。