Pablo Galiano : VS 10 beta 1 Exporting MEF parts from a VS Package (part 2)

Subscriptions

<September 2010>
SuMoTuWeThFrSa
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

News

Subscribe to Pablo Galiano by Email

Post Categories

VS 10 beta 1 Exporting MEF parts from a VS Package (part 2)

***Disclaimer: This information applies to Visual Studio 2010 Beta 1 only.***

 

In part 2 I will create the MEF contracts and parts. Two new projects will be added to the solution:

Let’s add a project for the contracts to the solution and name it MEFMessaging.Interfaces:

image

And declare the contract:

namespace MEFMessaging.Interfaces
{
    public interface IMessagingService
    {
        void DisplayMessage(string message);
    }
}

Now the parts:

For the parts I will use a project template that creates a VSIX MEF component, you can download from here

Let’s add a VSIX MEF component project to the solution and name it MEFMessagingComponent:

image 

And define the parts:

For that we need to add a reference to the MEFMessaging.Interfaces project.

[Export(typeof(IMessagingService))]
[PartCreationPolicy(CreationPolicy.Shared)]
public class MessagingService : IMessagingService
{
    [Import]
    public IVsUIShell UIShell { get; set; }

    public void DisplayMessage(string message)
    {
        int result = 0;
        Guid guid = Guid.Empty;
        UIShell.ShowMessageBox(
            0, ref guid, string.Empty, message,
            string.Empty, 0, OLEMSGBUTTON.OLEMSGBUTTON_OK,
            OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST,
            OLEMSGICON.OLEMSGICON_INFO, 0, out result);
    }
}

Couple of points:

  • We export the contract IMessagingService defined in the MEFMessaging.Interfaces project
  • We import the IVsUIShell service, exported by the VSPackage class in part 1
    • We use the VS Service to display a message box with the message

 

Specifying the catalog name:

To specify the catalog name we need to update VsCatalogName attribute to the AssemblyInfo.cs file

[assembly: VsCatalogName("Foo")] –> [assembly: VsCatalogName("MEFContainer")]

 

The MEF Component content type:

Because we used the MEF VSIX project template, we already have the content type set up.

image 

In the next a final part I will show how to get the available exports (IMessagingService) and show messages.

 

Stay tuned,

Pablo

posted on Friday, July 10, 2009 10:36 AM by pga