【问题标题】:Automapper issue with identityserver4 - MissingMethodException: Method not found: '!!0 AutoMapper.IMapper.Map(System.Object)'identityserver4 的 Automapper 问题 - MissingMethodException:找不到方法:'!!0 AutoMapper.IMapper.Map(System.Object)'
【发布时间】:2020-08-21 08:39:24
【问题描述】:

我使用的是最新版本的 IdentityServer 4

问题/重现问题的步骤

  • 创建新的 blazor webassembly 应用程序
  • 我搭建了所有身份文件并自定义了
  • custom ApiAuthorizationDbContext 用于 blazor webassembly
  • 安装Automapper v10AutoMapper.Extensions.Microsoft.DependencyIn v8.0.1

当我运行项目时,浏览器内给我 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。或更新的身份服务器。见nuget.org/packages/IdentityServer4.EntityFramework.Storage
  • @LucianBargaoanu 另一个错误System.TypeLoadException: 'Method 'GetAllAsync' in type 'IdentityServer4.Stores.InMemoryPersistedGrantStore' from assembly 'IdentityServer4, Version=3.0.0.0, Culture=neutral, PublicKeyToken=f294d0afe402bb2b' does not have an implementation.'
  • 您的 nuget 引用之间似乎存在版本不匹配。从一个工作示例从头开始,或者自己构建一个简单的测试项目。
  • 我使用带有默认身份验证的 Blazor Webassembly 个人用户帐户

标签: automapper identityserver4


【解决方案1】:

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

AutoMapper 10.0.0 似乎有一个bug,解决这个问题的方法是将AutoMapper 升级到10.1.1 版本。

这个版本很新,比这个答案的日期早4天发布。

【讨论】:

  • 什么错误? :) 10.1.1 中的Map 签名没有变化。
  • 签名不会变,但是我觉得内部实现变了,所以bug解决了。
  • 我看不出上面的错误信息与 10.1.1 有什么关系。在我看来,smth else also 已更改以使错误消失。
  • 您是否碰巧提到了该错误?我只在 Linux(K8s)环境中运行时看到错误,但在本地,在 Windows 上我无法重现。在我的实例中不是 IdentityServer 特定代码,但该解决方案与 IdentityServer 4 集成,并且在迁移到 IS 4.1.2 后问题开始出现
【解决方案2】:

我认为这与身份服务器无关,但更多的是使用了 2 个不同版本的 AutoMapper。

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

结果,应用程序崩溃了。

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

【讨论】:

    【解决方案3】:

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

    我将解决方案中的所有项目都更改为具有相同的依赖项。 IE。 Automaper(10.1.1) 解决了

    【讨论】:

      【解决方案4】:

      将 AutoMapper 添加到另一个项目后遇到同样的问题。我使用的是相同的版本。 Identity 3.1.8

      Automapper v10 and AutoMapper.Extensions.Microsoft.DependencyIn v8.0.1

      我发现对我来说最好的解决方案是将 Automapper 版本分别回滚到 9 和 DI

      【讨论】:

      • 对 WPF 应用程序有类似的问题。而且,关键是该项目引用了不同版本的Automapper 相关包。为整个解决方案更新 NuGet 包有助于解决问题。
      • 非常感谢。刚刚回滚到 2019 年 8 月 12 日最常用的 7.0.0 版本,现在觉得还不错
      • 在我的情况下,我没有通过 NuGet 显式添加 Automapper,只有 AutoMapper.Extensions.Microsoft.DependencyInjection(最新 8.1.0)依赖于 AutoMapper 10。只需将 DI 包降级到版本 7 (依赖于 AM 9)将所有内容整理出来。
      【解决方案5】:

      最近被这个问题困扰了。原来这是两个消费者之间的 AutoMapper 不匹配。

      ProjA 依赖于 AutoMapper 版本 X 和 ProjB 版本 Y。 ProjB 依赖 AutoMapper 版本 x。

      当 ProjA 更新其 AutoMapper 版本(我们从 9 到 10)时,没有更新匹配的 AutoMapper,到与来自 ProjB 的 nuget 相同的版本,ProjB 无法运行,因为获取的映射器 v10 发生了重大更改v9 dll。

      如果您的项目引用 AutoMapper 在使用程序集绑定重定向已发布项目上的 dll 时,请注意版本之间的制动变化

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-01
        • 2021-06-17
        • 1970-01-01
        • 2015-07-10
        • 2015-08-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多