Pablo Galiano : Friday, July 10, 2009 - Posts

Subscriptions

<March 2010>
SuMoTuWeThFrSa
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

News

Subscribe to Pablo Galiano by Email

Post Categories

Friday, July 10, 2009 - Posts

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

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

 

This is the final part of this post series. I will show how to get the available exports.

To get the available exports I will use a command under the Tools menu:

image

The menu callback get the first registered IMessagingService export and calls the display message method:

private void MenuItemCallback(object sender, EventArgs e)
{
    var componentModel = base.GetService(typeof(SComponentModel)) as IComponentModel;
    var messaging = componentModel.GetExtensions<IMessagingService>().FirstOrDefault();

    messaging.DisplayMessage("Hello MEF");
}

 

image

 

Couple of points:

  • The SComponentModel service is used to get the available extensions
  • The GetExtension method calls the GlobalCompositionContainer, which find all availables <TExport> in the list of containers that are maintaned by the VsExportProviderService class

 

Pablo

posted Friday, July 10, 2009 11:09 AM by pga with 1 Comments

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 Friday, July 10, 2009 10:36 AM by pga with 0 Comments