Skip to content

Conversation

@stephentoub
Copy link
Contributor

GetPublicNoArgExtensionMethods tries to instantiate arbitrary generic methods with arbitrary type arguments, which can fail if those arguments don't meet any constraints defined on the generic parameters. Automapper isn't doing the required checking for constraint validation today, resulting in exceptions escaping; multiple existing Automapper unit tests fail on .NET 7 as a result. This works around it by eating the ArgumentException that comes from MakeGenericMethod (there's no exposed compatibility-checking function, and so while not usually recommended, the most reliable thing here is to simply catch the errors stemming from the validation MakeGenericMethod performs).

Fixes #3988

GetPublicNoArgExtensionMethods tries to instantiate arbitrary generic methods with arbitrary type arguments, which can fail if those arguments don't meet any constraints defined on the generic parameters. Automapper isn't doing the required checking for constraint validation today, resulting in exceptions escaping; multiple existing Automapper unit tests fail on .NET 7 as a result.  This works around it by eating the ArgumentException that comes from MakeGenericMethod (there's no exposed compatibility-checking function, and so while not usually recommended, the most reliable thing here is to simply catch the errors stemming from the validation MakeGenericMethod performs).
@jbogard
Copy link
Contributor

jbogard commented Jun 8, 2022

Related feature request to not have to do this exception silliness:

dotnet/runtime#28033

le sigh

@lbargaoanu
Copy link
Contributor

Maybe we should simply skip all methods with generic constraints.

@jbogard
Copy link
Contributor

jbogard commented Jun 8, 2022

I think that might be more confusing...

@lbargaoanu
Copy link
Contributor

I guess it's easy to skip them in user code.

@jbogard jbogard merged commit aec0698 into LuckyPennySoftware:master Jun 8, 2022
@stephentoub stephentoub deleted the fixgenericconstraints branch June 8, 2022 15:59
@lbargaoanu lbargaoanu added this to the 11.0.2 milestone Jun 8, 2022
@lbargaoanu lbargaoanu added the Bug label Jun 8, 2022
@PriyaPurkayastha
Copy link

@jbogard @lbargaoanu we (.NET team) would like to collaborate with you on this issue. We recognize that AutoMapper library is used by many applications, and it is important for us that these applications continue to work with .NET 7. Can you please contact us on [email protected]? Thanks!

@lbargaoanu
Copy link
Contributor

I'm on vacation for the next two weeks :)
@jbogard If someone is interested in speeding up AM configuration, the first thing to do is to disable the expensive stuff. That's the configuration I've profiled until now. But it's on by default. So I've tried to minimize the damage (in my branch). Mainly by calling MakeGenericMethod only when the extension method actually matches a destination name.

@PriyaPurkayastha
Copy link

Thanks @lbargaoanu. We wanted to get some information as follows:

  1. When is the planned release date for AutoMapper 11.0.2 which contains this fix that Stephen submitted?
  2. We found that several apps in our AppCompat lab use older versions of AutoMapper. Can you please let us know what your Servicing plans look like? Do you normally service previous AutoMapper major/minor versions, or would your customers need to pick up the latest version when they use .NET 7? If you do service previous releases, what can we do to get the fix in those releases as well?

.NET 7 GA is this November, so we have a couple of weeks to evaluate the options. Have a great vacation! We look forward to hearing back from you.

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MaxInteger[T](System.Collections.Generic.IEnumerable`1[T])' violates the constraint of type parameter 'T' exception

4 participants