Automapper在使用IdentityServer4时出现问题 - MissingMethodException: 方法未找到:'!!0 AutoMapper.IMapper.Map(System.Object)'

23

我正在使用IdentityServer 4的最新版本。

问题 / 重现步骤

当我运行项目时,在浏览器中给出了500错误:

AuthenticationService.js:44 GET https://localhost:5001/connect/authorize?client_id=Web.Client&redirect_uri=https%3A%2F%2Flocalhost%3A5001%2Fauthentication%2Flogin-callback&response_type=code&scope=Web.ServerAPI%20openid%20profile&state=4b9b0120a48442a786b5bc8260c52f65&code_challenge=ViBkoIwumlfy_kg_y0bAh9uFsEByt2aKDOVUSREfARE&code_challenge_method=S256&prompt=none&response_mode=query 500

在自托管控制台中:

crit: IdentityServer4.Hosting.IdentityServerMiddleware[0]
      Unhandled exception: Method not found: '!!0 AutoMapper.IMapper.Map(System.Object)'.
System.MissingMethodException: Method not found: '!!0 AutoMapper.IMapper.Map(System.Object)'.
   at IdentityServer4.EntityFramework.Mappers.PersistedGrantMappers.ToEntity(PersistedGrant model)
   at IdentityServer4.EntityFramework.Stores.PersistedGrantStore.StoreAsync(PersistedGrant token)
   at IdentityServer4.Stores.DefaultGrantStore`1.StoreItemAsync(String key, T item, String clientId, String subjectId, DateTime created, Nullable`1 expiration)
   at IdentityServer4.Stores.DefaultGrantStore`1.CreateItemAsync(T item, String clientId, String subjectId, DateTime created, Int32 lifetime)
   at IdentityServer4.ResponseHandling.AuthorizeResponseGenerator.CreateCodeFlowResponseAsync(ValidatedAuthorizeRequest request)
   at IdentityServer4.ResponseHandling.AuthorizeResponseGenerator.CreateResponseAsync(ValidatedAuthorizeRequest request)
   at IdentityServer4.Endpoints.AuthorizeEndpointBase.ProcessAuthorizeRequestAsync(NameValueCollection parameters, ClaimsPrincipal user, ConsentResponse consent)
   at IdentityServer4.Endpoints.AuthorizeEndpoint.ProcessAsync(HttpContext context)
   at IdentityServer4.Hosting.IdentityServerMiddleware.Invoke(HttpContext context, IEndpointRouter router, IUserSession session, IEventService events)
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
System.MissingMethodException: Method not found: '!!0 AutoMapper.IMapper.Map(System.Object)'.
   at IdentityServer4.EntityFramework.Mappers.PersistedGrantMappers.ToEntity(PersistedGrant model)
   at IdentityServer4.EntityFramework.Stores.PersistedGrantStore.StoreAsync(PersistedGrant token)
   at IdentityServer4.Stores.DefaultGrantStore`1.StoreItemAsync(String key, T item, String clientId, String subjectId, DateTime created, Nullable`1 expiration)
   at IdentityServer4.Stores.DefaultGrantStore`1.CreateItemAsync(T item, String clientId, String subjectId, DateTime created, Int32 lifetime)
   at IdentityServer4.ResponseHandling.AuthorizeResponseGenerator.CreateCodeFlowResponseAsync(ValidatedAuthorizeRequest request)
   at IdentityServer4.ResponseHandling.AuthorizeResponseGenerator.CreateResponseAsync(ValidatedAuthorizeRequest request)
   at IdentityServer4.Endpoints.AuthorizeEndpointBase.ProcessAuthorizeRequestAsync(NameValueCollection parameters, ClaimsPrincipal user, ConsentResponse consent)
   at IdentityServer4.Endpoints.AuthorizeEndpoint.ProcessAsync(HttpContext context)
   at IdentityServer4.Hosting.IdentityServerMiddleware.Invoke(HttpContext context, IEndpointRouter router, IUserSession session, IEventService events)
   at IdentityServer4.Hosting.IdentityServerMiddleware.Invoke(HttpContext context, IEndpointRouter router, IUserSession session, IEventService events)
   at IdentityServer4.Hosting.MutualTlsTokenEndpointMiddleware.Invoke(HttpContext context, IAuthenticationSchemeProvider schemes)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at IdentityServer4.Hosting.BaseUrlMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

并且在 https://localhost:5001/connect/authorize?client_id=Web.Client&redirect_uri=https%3A%2F%2Flocalhost%3A5001%2Fauthentication%2Flogin-callback&response_type=code&scope=Web.ServerAPI%20openid%20profile&state=4b9b0120a48442a786b5bc8260c52f65&code_challenge=ViBkoIwumlfy_kg_y0bAh9uFsEByt2aKDOVUSREfARE&code_challenge_method=S256&prompt=none&response_mode=query 文件中:

An unhandled exception occurred while processing the request.
MissingMethodException: Method not found: '!!0 AutoMapper.IMapper.Map(System.Object)'.
IdentityServer4.EntityFramework.Mappers.PersistedGrantMappers.ToEntity(PersistedGrant model)

技术栈:

MissingMethodException: Method not found: '!!0 AutoMapper.IMapper.Map(System.Object)'.

IdentityServer4.EntityFramework.Mappers.PersistedGrantMappers.ToEntity(PersistedGrant model)
IdentityServer4.EntityFramework.Stores.PersistedGrantStore.StoreAsync(PersistedGrant token)
IdentityServer4.Stores.DefaultGrantStore<T>.StoreItemAsync(string key, T item, string clientId, string subjectId, DateTime created, Nullable<DateTime> expiration)
IdentityServer4.Stores.DefaultGrantStore<T>.CreateItemAsync(T item, string clientId, string subjectId, DateTime created, int lifetime)
IdentityServer4.ResponseHandling.AuthorizeResponseGenerator.CreateCodeFlowResponseAsync(ValidatedAuthorizeRequest request)
IdentityServer4.ResponseHandling.AuthorizeResponseGenerator.CreateResponseAsync(ValidatedAuthorizeRequest request)
IdentityServer4.Endpoints.AuthorizeEndpointBase.ProcessAuthorizeRequestAsync(NameValueCollection parameters, ClaimsPrincipal user, ConsentResponse consent)
IdentityServer4.Endpoints.AuthorizeEndpoint.ProcessAsync(HttpContext context)
IdentityServer4.Hosting.IdentityServerMiddleware.Invoke(HttpContext context, IEndpointRouter router, IUserSession session, IEventService events)
IdentityServer4.Hosting.IdentityServerMiddleware.Invoke(HttpContext context, IEndpointRouter router, IUserSession session, IEventService events)
IdentityServer4.Hosting.MutualTlsTokenEndpointMiddleware.Invoke(HttpContext context, IAuthenticationSchemeProvider schemes)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
IdentityServer4.Hosting.BaseUrlMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

显示原始异常细节
System.MissingMethodException: Method not found: '!!0 AutoMapper.IMapper.Map(System.Object)'.
   at IdentityServer4.EntityFramework.Mappers.PersistedGrantMappers.ToEntity(PersistedGrant model)
   at IdentityServer4.EntityFramework.Stores.PersistedGrantStore.StoreAsync(PersistedGrant token)
   at IdentityServer4.Stores.DefaultGrantStore`1.StoreItemAsync(String key, T item, String clientId, String subjectId, DateTime created, Nullable`1 expiration)
   at IdentityServer4.Stores.DefaultGrantStore`1.CreateItemAsync(T item, String clientId, String subjectId, DateTime created, Int32 lifetime)
   at IdentityServer4.ResponseHandling.AuthorizeResponseGenerator.CreateCodeFlowResponseAsync(ValidatedAuthorizeRequest request)
   at IdentityServer4.ResponseHandling.AuthorizeResponseGenerator.CreateResponseAsync(ValidatedAuthorizeRequest request)
   at IdentityServer4.Endpoints.AuthorizeEndpointBase.ProcessAuthorizeRequestAsync(NameValueCollection parameters, ClaimsPrincipal user, ConsentResponse consent)
   at IdentityServer4.Endpoints.AuthorizeEndpoint.ProcessAsync(HttpContext context)
   at IdentityServer4.Hosting.IdentityServerMiddleware.Invoke(HttpContext context, IEndpointRouter router, IUserSession session, IEventService events)
   at IdentityServer4.Hosting.IdentityServerMiddleware.Invoke(HttpContext context, IEndpointRouter router, IUserSession session, IEventService events)
   at IdentityServer4.Hosting.MutualTlsTokenEndpointMiddleware.Invoke(HttpContext context, IAuthenticationSchemeProvider schemes)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at IdentityServer4.Hosting.BaseUrlMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

你需要一个旧版本的 AM 或者一个新版本的身份验证服务器。请参考 https://www.nuget.org/packages/IdentityServer4.EntityFramework.Storage。 - Lucian Bargaoanu
@LucianBargaoanu 另一个错误 System.TypeLoadException: '程序集 'IdentityServer4, Version=3.0.0.0, Culture=neutral, PublicKeyToken=f294d0afe402bb2b' 中的类型 'IdentityServer4.Stores.InMemoryPersistedGrantStore' 的方法 'GetAllAsync' 没有实现。' - arman
你的NuGet引用似乎存在版本不匹配的问题。建议从头开始使用一个可工作的示例,或者自己构建一个简单的测试项目。 - Lucian Bargaoanu
我正在使用带有默认身份验证的Blazor Webassembly个人用户帐户。 - arman
5个回答

19

不要后退,不要降级,升级

看起来AutoMapper 10.0.0存在一个bug,解决这个问题的方法是将AutoMapper升级到10.1.1版本。

这个版本很新,发布于本答案日期前4天。


1
什么错误? :) 在10.1.1中,Map签名没有任何更改。 - Lucian Bargaoanu
签名不会改变,但我认为内部实现已经改变了,所以这个错误已经解决了。 - Hakan Fıstık
我不明白上面的错误信息与10.1.1有什么关系。在我看来,似乎还有其他东西改变了,才使错误消失。 - Lucian Bargaoanu
你是否有关于这个 bug 的参考资料?我发现错误只在 Linux(K8s) 环境下运行时出现,但在 Windows 上本地运行时无法复现。在我的实例中,这不是 IdentityServer 特定的代码,但解决方案与 IdentityServer 4 集成,并且在升级到 IS 4.1.2 后开始出现问题。 - Florin Grigoriu

11

我认为这与IdentityServer无关,而更多地是由于使用了两个不同版本的AutoMapper。

我在一个包含几个项目的解决方案中遇到了这个问题,昨天我更新了其中一个项目的AutoMapper,而另一个项目仍使用旧版本。

结果,应用程序出现了故障。

我刚刚更新了另一个项目,现在它已经可以正常工作了。


1
这对我有帮助,我在同一个解决方案中有两个项目,其中一个使用的是旧版本。一旦将其升级,问题就解决了。 - Padmika

11

我曾遇到同样的问题,因为解决方案中的不同项目使用了不同版本的Automapper(9.0.0和10.1.1)

我将解决方案中的所有项目更改为使用相同的依赖项,即Automaper(10.1.1),问题得到了解决。


6
在将AutoMapper添加到另一个项目后,我遇到了同样的问题。 我在使用相同版本:Identity 3.1.8Automapper v10 and AutoMapper.Extensions.Microsoft.DependencyIn v8.0.1。我发现对我最好的解决方案是将AutoMapper版本回滚到9,同时也回滚DI版本。

WPF应用程序也遇到了类似的问题。问题在于项目引用了不同版本的“Automapper”相关包。更新整个解决方案的NuGet包有助于解决此问题。 - kosist
非常感谢。刚刚回滚到了最常用的版本7.0.0,发布日期为2019年8月12日,现在认为一切都很好。 - Bellash
在我的情况下,我没有通过NuGet显式地添加Automapper,只有Automapper.Extensions.Microsoft.DependencyInjection(最新的8.1.0版本),它依赖于Automapper 10。只需将DI软件包降级到版本7(其依赖于AM 9)即可解决所有问题。 - Daz
AutoMapper.Extensions.Microsoft.DependencyInjection 8.1.1版本令人困惑地依赖于主要的AutoMapper库的10版本。因此,如果我不将AutoMapper从8.1.1更新到10版本,就会出现这个错误。 - Rob Sedgwick

2
最近被这个问题困扰了。结果发现是两个使用AutoMapper的消费者之间存在不匹配。
ProjA依赖于AutoMapper版本X,而ProjB则依赖于AutoMapper版本Y。ProjB依赖于AutoMapper版本x。
当ProjA更新其AutoMapper版本(我们从9升级到10),而没有将匹配的AutoMapper版本也更新到来自ProjB的nuget相同版本时,ProjB无法运行,因为获取的v10映射器与v9 dll存在破坏性变化。
如果您的项目引用了AutoMapper,请注意在使用程序集绑定重定向dlls发布项目时,版本之间可能会出现破坏性变化。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接