基于 IdentityServer4 的项目升级到 .NET 7 后出现下面的异常,请问如何解决?
System.TypeInitializationException: The type initializer for 'IdentityServer4.EntityFramework.Mappers.ApiResourceMappers' threw an exception.
---> System.ArgumentException: GenericArguments[0], 'System.Char', on 'T MaxFloat[T](System.Collections.Generic.IEnumerable`1[T])' violates the constraint of type 'T'.
---> System.Security.VerificationException: Method System.Linq.Enumerable.MaxFloat: type argument 'System.Char' violates the constraint of type parameter 'T'.
at System.RuntimeMethodHandle.GetStubIfNeeded(RuntimeMethodHandleInternal method, RuntimeType declaringType, RuntimeType[] methodInstantiation)
at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
--- End of inner exception stack trace ---
at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
at AutoMapper.TypeDetails.<>c__DisplayClass30_1.<BuildPublicNoArgExtensionMethods>b__10(MethodInfo extensionMethod)
是 AutoMapper 的问题,详见 https://github.com/AutoMapper/AutoMapper/issues/3988
如果在项目中专门安装最新版的 AutoMapper,错误则变成
System.TypeInitializationException: The type initializer for 'IdentityServer4.EntityFramework.Mappers.ClientMappers' threw an exception.
---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.MissingMethodException: Method not found: '!2 AutoMapper.IMappingExpressionBase`3.ConstructUsing(System.Linq.Expressions.Expression`1<System.Func`2<!0,!1>>)'.
at IdentityServer4.EntityFramework.Mappers.ClientMapperProfile..ctor()
问题总结:IdentityServer4 支持的 AutoMapper 版本(10.0.0-11.0.0)不支持 .NET 7,IdentityServer4 不支持的 AutoMapper 12.0.0 支持 .NET 7。
github 上的相关 issue:https://github.com/IdentityServer/IdentityServer4/issues/5486
最终通过将 IdentityServer4 升级至 .NET 7 解决了问题