简体   繁体   English

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

[英]500 Internal Server Error in ASP.NET MVC Bundles

I do bundle my JavaScript files with ASP.NET MVC Bundling. 我确实将我的JavaScript文件与ASP.NET MVC捆绑在一起。 When I inspect and check Network Tab in my browser, I see 500 Internal Server Error when my bundle wants to load. 当我在浏览器中检查并检查“ 网络”选项卡时,我想加载捆绑软件时看到500 Internal Server Error response: 响应:

A second operation started on this context before a previous asynchronous operation completed. 在先前的异步操作完成之前,第二操作在此上下文上开始。 Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. 使用“ await”来确保在此上下文上调用另一个方法之前,所有异步操作都已完成。 Any instance members are not guaranteed to be thread safe. 不保证任何实例成员都是线程安全的。

I understand that this error happened in my GenerateIdentityAsync method. 我了解此错误发生在我的GenerateIdentityAsync方法中。 I use ASP.NET Identity 2.0 so I use it from OnValidateIdentity in Owin Startup class: 我使用ASP.NET Identity 2.0,因此在Owin Startup类的OnValidateIdentity中使用它:

ApplicationManager.cs 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 startup.cs

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

Stack

[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

also i test ConfigureAwait(true) and ConfigureAwait(false) and without ConfigureAwait but donot solved this issue. 我也测试ConfigureAwait(true)ConfigureAwait(false) ,没有ConfigureAwait但没有解决此问题。

Update : 更新
Also, when I use EF Profiler , it says Using A Single Object Context In Multiple Threads Is Likely A Bug. 另外,当我使用EF Profiler时 ,它说在多个线程中使用单个对象上下文可能是一个错误。

this alert exist just for same table: 此警报仅存在于同一表中:

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

How can I solve this issue? 我该如何解决这个问题?

More Code in Gist 要点中的更多代码

I not use StructureMap by myself, but i found that for MVC the best way to use StructureMap with MVC is to install StructureMap.MVC5.Update package (that's for latest StructureMap version). 我不是一个人使用StructureMap,而是发现对于MVC,将StructureMap与MVC一起使用的最佳方法是安装StructureMap.MVC5.Update软件包(适用于最新的StructureMap版本)。
Check small intro how to use it. 检查小介绍如何使用它。 Or this article. 还是这篇文章。

Move all your existing registration code to DefaultRegistry.cs installed by package. 将所有现有的注册代码移动到软件包安装的DefaultRegistry.cs中。 And now don't use HybridHttpOrThreadLocalScoped() for services registration. 现在不要使用HybridHttpOrThreadLocalScoped()进行服务注册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM