IdentityServer4 dotnet7 produce exception
Asked Answered
W

5

5

when using Automaper 10.1.1, with identityserver4 and .net 7, identityserver4 is the last free version of IdentityServer and doesn't work with a higher version of AutoMapper where this issue is resolved in this link: Issue. Automapper 10 + identity server 4 + dotnet 7 simply when I hit /.well-known/openid-configuration I got this exception

ArgumentException: GenericArguments[0], 'System.Char', on 'T MaxFloat[T](System.Collections.Generic.IEnumerable`1[T])' violates the constraint of type 'T'.
System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
AutoMapper.TypeDetails+<>c__DisplayClass28_1.<BuildPublicNoArgExtensionMethods>b__10(MethodInfo extensionMethod)
System.Linq.Enumerable+WhereSelectArrayIterator<TSource, TResult>.MoveNext()
System.Linq.Enumerable+ConcatIterator<TSource>.MoveNext()
System.Linq.Enumerable.SelectManyIterator<TSource, TCollection, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)+MoveNext()
System.Linq.Enumerable+WhereSelectEnumerableIterator<TSource, TResult>.MoveNext()
System.Collections.Generic.HashSet<T>.UnionWith(IEnumerable<T> other)
System.Linq.Enumerable+UnionIterator<TSource>.FillSet()
System.Linq.Enumerable+UnionIterator<TSource>.ToArray()
AutoMapper.TypeDetails.BuildPublicNoArgExtensionMethods(IEnumerable<MethodInfo> sourceExtensionMethodSearch)
AutoMapper.TypeDetails..ctor(Type type, ProfileMap config)
AutoMapper.ProfileMap.TypeDetailsFactory(Type type)
AutoMapper.Internal.LockingConcurrentDictionary<TKey, TValue>+<>c__DisplayClass2_1.<.ctor>b__1()
System.Lazy<T>.ViaFactory(LazyThreadSafetyMode mode)
System.Lazy<T>.ExecutionAndPublication(LazyHelper executionAndPublication, bool useDefaultConstructor)
System.Lazy<T>.CreateValue()
AutoMapper.Internal.LockingConcurrentDictionary<TKey, TValue>.GetOrAdd(TKey key)
AutoMapper.ProfileMap.CreateTypeDetails(Type type)
AutoMapper.TypeMapFactory.CreateTypeMap(Type sourceType, Type destinationType, ProfileMap options, bool isReverseMap)
AutoMapper.ProfileMap.BuildTypeMap(IConfigurationProvider configurationProvider, ITypeMapConfiguration config)
AutoMapper.ProfileMap.Register(IConfigurationProvider configurationProvider)
AutoMapper.MapperConfiguration.Seal()
AutoMapper.MapperConfiguration..ctor(MapperConfigurationExpression configurationExpression)
AutoMapper.MapperConfiguration..ctor(Action<IMapperConfigurationExpression> configure)
IdentityServer4.EntityFramework.Mappers.IdentityResourceMappers..cctor()
// Put your source/destination types here

I tryed all Higher versions and I get another exception because identity server 4 requires AutoMapper version>=10 and <11, in this case I got this exception:

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()
   at System.RuntimeType.CreateInstanceOfT()
Wheeler answered 8/2, 2023 at 15:57 Comment(1)
You can have your own fork of IdentityServer4.EntityFramework or of AutoMapper.Valorize
S
7

I also got this exception "The type initializer for 'IdentityServer4.EntityFramework.Mappers..." using .NET 8

Installing the latest version of the following packages fixed this issue for me:

Sideburns answered 24/12, 2023 at 9:21 Comment(3)
I had the same issue when upgrading ids4 to .NET 8 and your solution worked perfectly. Here the libraries that I am using: Cnblogs.IdentityServer4.EntityFramework.Storage 4.2.1, AutoMapper 13.0.1Kernite
I had the same issue, this worked for me! thanks!Gibberish
This worked for me. Aside from AutoMapper 13.0.1 and Cnblogs.IdentityServer4.EntityFramework.Storage 4.2.1 I had to update Microsoft.AspNetCore.Identity.EntityFrameworkCore and Npgsql.EntityFrameworkCore.PostgreSQL to 8.x.x versions.Africa
B
6

I found a solution

  1. Just copy/paste these 2 projects and create own libraries:

IdentityServer4.EntityFramework

IdentityServer4.EntityFramework.Storage

  1. Bump Mapper version and fix Nullable reference types in IdentityServer4.EntityFramework.Storage lib.
  2. Remove packages from code and replace with your libraries. enter image description here

.well-known/openid-configuration now works for me.

Baumbaugh answered 7/4, 2023 at 21:37 Comment(2)
it looks good, maybe it would be better if you make it as fork on git. for me I migrated to Keyclock.Wheeler
any other alternative solution for this? I am facing same issue.Wraf
H
3

I also saw this problem yesterday - spent a lot of time looking for a solution but couldnt. I decided to downgrade .net to 6.0 then it worked - though, the requirements of IS4.EF.Storage mention support to .net 7.0

Maybe you should look at Duende.IdentityServer

This is new company by creators of IS4. Probably the issue has been resolved there. Switching from IS4 to Duende shouldnt be difficult due to their similarity

Headmost answered 10/2, 2023 at 12:58 Comment(4)
Thanks, yes I am already using .net6 and everything works fine for me, the problem with Duende is that it's not for free, and I think this will not be solved on IS4 at least in a short term bcs it's out of supportWheeler
@KarimMoghnieh , as far as I know that Duende is free for personal use.Headmost
You will need to pay for Duende if your income more than a 1`000`000$ , seems to be.Headmost
This is the right approach for fixing this issue properly. Or go with DuendeServer. Downgrade solution to .NET 6 and every NuGet package to version 6. And it will work properly.Euryale
S
2

As an update from the answer by @Tapac, you can use this package which has the updated AutoMapper

Also, this is discussed in detail here: https://github.com/IdentityServer/IdentityServer4/issues/5486

Scow answered 22/8, 2023 at 5:23 Comment(0)
S
1

You can install new version of Automapper directly to your top level projects and solution would use them.

Sollows answered 11/10, 2023 at 8:56 Comment(1)
Only in theory. In practice that is what causes the second exception. Presumably it is something that you never actually tried.Oligocene

© 2022 - 2024 — McMap. All rights reserved.