Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to specify interface type to GetCustomAttributes #7190

Closed
mconnew opened this issue Jan 4, 2017 · 2 comments
Closed

Unable to specify interface type to GetCustomAttributes #7190

mconnew opened this issue Jan 4, 2017 · 2 comments

Comments

@mconnew
Copy link
Member

mconnew commented Jan 4, 2017

On the full framework, WCF looks for any attributes declared on a type where the attribute type derives from IServiceBehavior. This works on the full framework, but on .Net Core an ArgumentException is thrown with the message "Type passed in must be derived from System.Attribute or System.Attribute itself." Here is a simple repro which runs to completion on the full framework but throws on .Net Core.

using System.Reflection;
using System.ServiceModel;
using System.ServiceModel.Description;

    class Program
    {
        static void Main(string[] args)
        {
            var ti = typeof(MyService).GetTypeInfo();
            var attrs = ti.GetCustomAttributes(typeof(IServiceBehavior), false);
        }

        [ServiceContract]
        public interface IMyService
        {
            [OperationContract]
            string Hello(string hello);
        }

        public class MyService : IMyService
        {
            public string Hello(string hello) { return hello; }
        }
    }
@ghost ghost self-assigned this Jan 6, 2017
@ghost
Copy link

ghost commented Jan 6, 2017

It looks like the desktop has a mismatch in behavior between

MemberInfo.GetCustomAttributes(attributeType, inherit)  (accepts interface on desktop)

and

CustomAttributeExtensions.GetCustomAttributes(this memberInfo, attributeType, inherit) (does NOT accept interface on desktop.)

(This is probably because CustomAttributeExtensions.GetCustomAttribute() is statically defined to return IEnumerable rather than object[], so it has to ensure everything that passes the filter is assignable to Attribute.)

On the desktop, the C# expression "ti.GetCustomAttributes()" resolves to MemberInfo.GetCustomAttributes().

On .NetCore 1.0, MemberInfo.GetCustomAttributes() doesn't exist, so the expression "ti.GetCustomAttributes()" resolves to the more restrictive CustomAttributeExtensions version.

With .NS2.0, MemberInfo.GetCustomAttributes() is returning, so this won't be such a problem.

However, as part of investigating this, I realized that corert gets MemberInfo.GetCustomAttribute() wrong so there's an issue to fix there.

@ghost
Copy link

ghost commented Apr 12, 2017

With .NS2.0, MemberInfo.GetCustomAttributes() is returning, so this won't be such a problem.

@ghost ghost closed this as completed Apr 12, 2017
@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@msftgits msftgits added this to the 2.0.0 milestone Jan 31, 2020
CoffeeFlux added a commit to CoffeeFlux/runtime that referenced this issue Mar 25, 2020
Looking at dotnet#7190, the .NET Core behavior here is strange and my changes in dotnet@0844cb7a6152 with the type checking may have been a mistake. I think this should be fine with the interface special-casing, but if deemed too great a concern I can revert that subset of my old changes. Additionally, if people come up with other scenarios where this might fail, I'll just revert until we can migrate to use a shared CustomAttribute implementation rather than try to play whack-a-mole.
CoffeeFlux added a commit that referenced this issue Mar 26, 2020
…33942)

Looking at #7190, the .NET Core behavior here is strange and my changes in 0844cb7a6152 with the type checking may have been a mistake. I think this should be fine with the interface special-casing, but if deemed too great a concern I can revert that subset of my old changes. Additionally, if people come up with other scenarios where this might fail, I'll just revert until we can migrate to use a shared CustomAttribute implementation rather than try to play whack-a-mole.
@ghost ghost locked as resolved and limited conversation to collaborators Dec 26, 2020
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants