ASP.NET MVC 包中的 500 内部服务器错误
500 Internal Server Error in ASP.NET MVC Bundles
我确实将我的 JavaScript 文件与 ASP.NET MVC 捆绑在一起。当我检查浏览器中的 Network 选项卡时,当我的包想要加载时,我看到 500 Internal Server Error
。
回应:
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. Any instance members are not guaranteed to be thread safe.
我知道这个错误发生在我的 GenerateIdentityAsync
方法中。
我使用 ASP.NET Identity 2.0,所以我在 Owin Startup class:
中从 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 'await' 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.<GetUserAggregateAsync>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.<GetSecurityStampAsync>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.<CreateAsync>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.<GenerateIdentityAsync>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.<<OnValidateIdentity>b__1>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.<AuthenticateCoreAsync>d__2.MoveNext() +2153
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
Microsoft.Owin.Security.Infrastructure.<BaseInitializeAsync>d__0.MoveNext() +571
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
Microsoft.Owin.Security.Infrastructure.<Invoke>d__0.MoveNext() +255
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
Microsoft.AspNet.Identity.Owin.<Invoke>d__0.MoveNext() +404
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
Microsoft.AspNet.Identity.Owin.<Invoke>d__0.MoveNext() +404
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
Microsoft.AspNet.Identity.Owin.<Invoke>d__0.MoveNext() +404
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
Microsoft.AspNet.Identity.Owin.<Invoke>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.<RunApp>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.<DoFinalWork>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& completedSynchronously) +155
我也测试了 ConfigureAwait(true)
和 ConfigureAwait(false)
并且没有 ConfigureAwait
但没有解决这个问题。
更新:
此外,当我使用 EF Profiler 时,它说 在多个线程中使用单个对象上下文可能是一个错误。
此警报只针对相同的 table:
[Admin].[UserClaims]
[Admin].[UserLogin]
[Admin].[Roles]
[Admin].[UserRoles]
[Admin].[Users]
我该如何解决这个问题?
更多代码在Gist
我自己不使用 StructureMap,但我发现对于 MVC,将 StructureMap 与 MVC 一起使用的最佳方法是安装 StructureMap.MVC5.Update 包(这是最新的 StructureMap 版本)。
查看小 intro how to use it. Or this 文章。
将您现有的所有注册码移至 DefaultRegistry.cs 包安装。现在不要使用 HybridHttpOrThreadLocalScoped()
进行服务注册。
我确实将我的 JavaScript 文件与 ASP.NET MVC 捆绑在一起。当我检查浏览器中的 Network 选项卡时,当我的包想要加载时,我看到 500 Internal Server Error
。
回应:
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. Any instance members are not guaranteed to be thread safe.
我知道这个错误发生在我的 GenerateIdentityAsync
方法中。
我使用 ASP.NET Identity 2.0,所以我在 Owin Startup class:
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 'await' 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.<GetUserAggregateAsync>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.<GetSecurityStampAsync>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.<CreateAsync>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.<GenerateIdentityAsync>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.<<OnValidateIdentity>b__1>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.<AuthenticateCoreAsync>d__2.MoveNext() +2153
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
Microsoft.Owin.Security.Infrastructure.<BaseInitializeAsync>d__0.MoveNext() +571
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
Microsoft.Owin.Security.Infrastructure.<Invoke>d__0.MoveNext() +255
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
Microsoft.AspNet.Identity.Owin.<Invoke>d__0.MoveNext() +404
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
Microsoft.AspNet.Identity.Owin.<Invoke>d__0.MoveNext() +404
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
Microsoft.AspNet.Identity.Owin.<Invoke>d__0.MoveNext() +404
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
Microsoft.AspNet.Identity.Owin.<Invoke>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.<RunApp>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.<DoFinalWork>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& completedSynchronously) +155
我也测试了 ConfigureAwait(true)
和 ConfigureAwait(false)
并且没有 ConfigureAwait
但没有解决这个问题。
更新:
此外,当我使用 EF Profiler 时,它说 在多个线程中使用单个对象上下文可能是一个错误。
此警报只针对相同的 table:
[Admin].[UserClaims]
[Admin].[UserLogin]
[Admin].[Roles]
[Admin].[UserRoles]
[Admin].[Users]
我该如何解决这个问题?
更多代码在Gist
我自己不使用 StructureMap,但我发现对于 MVC,将 StructureMap 与 MVC 一起使用的最佳方法是安装 StructureMap.MVC5.Update 包(这是最新的 StructureMap 版本)。
查看小 intro how to use it. Or this 文章。
将您现有的所有注册码移至 DefaultRegistry.cs 包安装。现在不要使用 HybridHttpOrThreadLocalScoped()
进行服务注册。