Poorly Explained Metadata in MEF Preview 6

Jul 21, 2009 at 12:30 PM
Edited Jul 21, 2009 at 1:11 PM

There are posts all over the place citing the fantastic new feature of being able to combine the metadata and export declaration into a single attribute now, by way of inheriting from ExportAttribute. The problem is, nowhere does it explain how to get access to the metadata.

Here's my code, as specified by the examples:

	[MetadataAttribute]
	[AttributeUsage(AttributeTargets.Class)]
	public class ExportControllerAttribute : ExportAttribute
	{
		public string ControllerName { get; private set; }

		public ExportControllerAttribute(string controllerName)
			: base(typeof(IController))
		{
			ControllerName = controllerName;
		}
	}
Here's a bit of code making use of it:
var controllerExports = _app.Container.GetExports<IController, ExportControllerAttribute>();
Here's a nasty exception that occurs as a result:
The Type 'ExportControllerAttribute' supplied is not a valid Metadata View.

I cannot find ANY information ANYWHERE about why this would occur. The only code samples I could find state that I should implement my Metdata requirements as I have done so and then provide no information as to how I should then get access to that metadata. Digging through the code is hairy but it seems to want me to play with dictionaries, yet if this is the case, why is this not mentioned in any way in the examples?

Jul 21, 2009 at 6:22 PM
Edited Jul 21, 2009 at 6:33 PM

Hi Nathan, thanks for the feedback, we have more proper documentation coming with FX4, and will incorporate this.

 

The problem with the code below, is you are supplying the attribute itself, when the method expects a metadata view. We do have a topic on this in our wiki, here. Look under the section on “Using Strongly-Typed metadata”.

 

To get your code below working, create an interface like this

 

public interface IExportController {

  string ControllerName {get;}

}

 

then pass that interface for the view.

 

var controllerExports = _app.Container.GetExports<IController, IExportController>();

 

HTH

Glenn

 

From: NathanRidley [mailto:notifications@codeplex.com]
Sent: Tuesday, July 21, 2009 4:31 AM
To: Glenn Block
Subject: Poorly Explained Metadata in MEF Preview 6 [MEF:63064]

 

From: NathanRidley

Here's my code:

        [MetadataAttribute]
        [AttributeUsage(AttributeTargets.Class)]
        public class ExportControllerAttribute : ExportAttribute
        {
               public string ControllerName { get; private set; }
 
               public ExportControllerAttribute(string controllerName)
                       : base(typeof(IController))
               {
                       ControllerName = controllerName;
               }
        }
Here's a bit of code making use of it:
var controllerExports = _app.Container.GetExports<IController, ExportControllerAttribute>();
 
Here's a nasty exception that occurs as a result:
The Type 'ExportControllerAttribute' supplied is not a valid Metadata View.

I cannot find ANY information ANYWHERE about why this would occur. The only code samples I could find state that I should implement my Metdata requirements as I have done so and then provide no information as to how I should then get access to that metadata.

Read the full discussion online.

To add a post to this discussion, reply to this email (MEF@discussions.codeplex.com)

To start a new discussion for this project, email MEF@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Jul 21, 2009 at 6:34 PM

Oops, the last snippet i posted was wrong in that I passed in ExportControllerAttribute again :-) I have updated it now to IExportController.

Jul 21, 2009 at 6:35 PM

Ah yep, got it working now. Actually I completely overlooked re-reading the guide; it was left in a pre-preview-5 state for so long I didn't even think to look at it. Thanks.

Jul 21, 2009 at 6:39 PM

Touche J

From: NathanRidley [mailto:notifications@codeplex.com]
Sent: Tuesday, July 21, 2009 10:36 AM
To: Glenn Block
Subject: Re: Poorly Explained Metadata in MEF Preview 6 [MEF:63064]

From: NathanRidley

Ah yep, got it working now. Actually I completely overlooked re-reading the guide; it was left in a pre-preview-5 state for so long I didn't even think to look at it. Thanks.

Read the full discussion online.

To add a post to this discussion, reply to this email (MEF@discussions.codeplex.com)

To start a new discussion for this project, email MEF@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Coordinator
Jul 21, 2009 at 6:59 PM

Also, we've updated the exception message to add: MetadataViews do not support non-public interfaces, and interfaces that contain members that are not properties.