Skip to content

Recent change to Attribute.GetCustomAttributes is breaking in ASP.NET Core #66496

Closed

Description

@SteveSandersonMS

Description

I know it's possible that this was an intentional breaking change and perhaps the correct resolution is to change things in ASP.NET Core. However it's worth checking.

We're getting a test failure in MapEndpoint_GeneratedDelegateWorks when it runs this logic:

        // Add delegate attributes as metadata
        var attributes = requestDelegate.Method.GetCustomAttributes();


        // This can be null if the delegate is a dynamic method or compiled from an expression tree
        if (attributes != null)

The exception is System.InvalidCastException : Unable to cast object of type 'System.Object[]' to type 'System.Attribute[]'. An example failing build is here.

Note that the exception is coming from runtime code, not our test code, so this may be a real regression that would affect customers.

It looks very likely that the cause is the changes in #65237 from last week. I see that previously, if the delegate was a dynamic method or compiled from an expression tree, then GetCustomAttributes would have returned null, whereas in the newer runtime code, it throws this InvalidCastException.

Is that an intentional change? It seems unlikely because:

  1. It forces user code to use try/catch since there's no TryGetCustomAttributes
  2. Surfacing it as an InvalidCastException really seems to be an internal implementation detail of the runtime. I'd expect a more meaningful exception with a message saying why it's illegal to ask about custom attributes for this specific method.

If we have to change ASP.NET Core to try/catch around this call then technically we can do so, but perhaps other customer code would also be broken by this change, and the use of try/catch for control flow here might be problematic for perf.

Reproduction Steps

See the failing build at https://dev.azure.com/dnceng/public/_build/results?buildId=1657756&view=ms.vss-test-web.build-test-results-tab&runId=45670040&resultId=102702&paneView=debug

Expected behavior

Retain existing behavior of returning null. Or if you don't want to return null, then return an empty array.

Actual behavior

Throws InvalidCastException

Regression?

Yes, this was not happening in the last runtime build ingested by ASP.NET Core.

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. ghost added
    untriaged
    on Mar 11, 2022
  2. stephentoub commented on Mar 11, 2022

    @stephentoub
    Member
  3. madelson commented on Mar 11, 2022

    @madelson
    Contributor

    Probably DynamicMethod needs similar updates to the ones made in my PR (and test coverage for this).

  4. jkotas commented on Mar 11, 2022

    @jkotas
    Member

    We should revert the original change until this is understood and fixed: #66508

  5. buyaa-n commented on Mar 11, 2022

    @buyaa-n
    Contributor

    // This can be null if the delegate is a dynamic method or compiled from an expression tree
    ...
    Retain existing behavior of returning null. Or if you don't want to return null, then return an empty array.

    The API is not supposed to return null, i believe we should return and empty array in this case

  6. added this to the 7.0.0 milestone on Mar 11, 2022
  7. removed
    untriaged
    on Mar 11, 2022
  8. added a commit that references this issue on Mar 13, 2022
    c2396ae
  9. buyaa-n commented on Jul 6, 2022

    @buyaa-n
    Contributor

    @SteveSandersonMS this is fixed in preview 5, could you check, you might need to update your code as it is now return empty array instead of null

  10. added
    needs-author-action
    on Jul 6, 2022
  11. modified the milestones: 7.0.0, Future on Jul 13, 2022
  12. ghost closed this as completedon Aug 11, 2022
  13. ghost locked as resolved and limited conversation to collaborators on Sep 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Reflectionneeds-author-actionno-recent-activity

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions