Serve it Up!

If you've read any of the introductory material to VSIP (see below), you've undoubtedly come across the concept of services. A package can offer a service to itself and to other packages running inside the Visual Studio shell. If you're using the VSIP Extras Helper Classes, proffering a service is a piece of <insert favorite dessert item />. Craig Skibo had a brief blog about this a few days ago, but this should provide some more detail. Here are the simple steps you need to follow:

  1. Create your basic C# package using the Package Wizard in VSIP Extras
  2. Add a new C# file to your project and define your interface
  3. Add another C# file to your project and implement the interface you just defined. This will be your “service” class.
  4. Assign a System.Runtime.InteropServices.GuidAttribute to your service class and generate a new GUID for it.
  5. Add a ProvideServiceAttribute to your package class. You should pass in a string containing the GUID of the service you just implemented. For example, I added this attribute to my package class:

[VSIP.Helper.ProvideService("8034A9F4-A5D5-4d81-B19C-6DB171C8D054", ServiceName="AaronService")]


      (What this does is put some metadata into the assembly that’s generated when you build your package. If you look at the post-build step that the wizard generated in your C# project, you’ll notice that a tool called “VSIPRegPkg.exe” is called on your package assembly. When VSIPRegPkg.exe examines your assembly, it sees the ProvideService attribute and automatically puts the correct values into the registry for you in order to have your package load and offer a service in Visual Studio. Cool!)

  1. In your package's initialize method, you need to actually proffer the service. This means that you are telling the shell, “If someone requests this service, give this to them”. The stuff in the previous step with registration simply lets Visual Studio know which assembly it can find your service in. Actually adding it to your “service container” in code will actually allow your package to proffer this service at load-time. The Package base class that you derive from implements IServiceContainer. This means that all you need to do is cast your package to IServiceContainer, and call AddService to proffer the service to the shell. You can do this like so:

    //Instantiate the service
    IAaron service = new AaronService();
    //Proffer the service to the shell ((IServiceContainer)this).AddService(service.GetType(), service, true);

    The true in the last argument is so your service gets promoted. This means that not only can your package call this service, but other packages that know about it can call it too.
  2. Build your package and run VSIPRegPkg.exe on it. (Running VSIPRegPkg.exe is done automatically if you copied BasicPkg or used the wizard to create your project).

Now you should be able to call your service from any other package that you write! To load the package and access the service, simply do something like this from your package that's calling your service:

IAaron service = (IAaron)GetService(typeof(AaronService));

I can now call any of the methods in the IAaron interface that I implemented in the AaronService class. And that, my friends, is how you proffer a service in managed code. J


As a final note to this [long] entry, AllenD wrote a blog entry about a month ago that describes how you go about accessing services provided by the VS shell from within a package. I would also recommend checking that out.

Skip to main content