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:
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:
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.
In the next a final part I will show how to get the available exports (IMessagingService) and show messages.
Stay tuned,
Pablo