Reference.Services Sample

This sample shows how to be a Visual Studio service provider and how to consume Visual Studio services.

4,963 times
Add to favorites
E-mail Twitter Digg Facebook

Solution explorer

C#, C++
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="../../../../Common/Overview.xsl" type="text/xsl"?>
    <ShortName>VisualBasic Reference.Services</ShortName>
    <LongName>VisualBasic Services Reference Sample</LongName>
    <Phrase>Creating, exposing, and consuming services in Visual Studio using VisualBasic.</Phrase>
      This sample demonstrates how to create and consume services from a VSPackage.
    <Goal>Creating and Proffering services in Visual Studio</Goal>
    <Goal>Consuming services in Visual Studio </Goal>
    <Goal>Local versus Global services</Goal>
    <Image width="400" height="400">Services.jpg</Image>
    <Doc />
      <Keyword>Local service</Keyword>
      <Keyword>Global service</Keyword>
	This sample shows how to create and expose services inside Visual Studio. It creates two services with two 
	different levels of visibility. The service with global visibility is available for any of Visual Studio's other components. The service with 
	local visibility is available only from within the package itself or when the IServiceProvider interface 
	implemented by the package is inside the chain of active providers.
	The sample creates two assemblies, one with the definition of the interfaces used and one with the actual 
	implementation of the packages and services. The assembly with the implementation, created by the Reference.Services 
	project, defines two packages. One package exposes the services and a second package uses them. The services are 
	implemented with two helper classes.
      	The part of interest about how to proffer services is the code inside the ServicesPackage class. Specifically:
	1. In the declaration of the class we use the ProvideServiceAttribute registration attribute defined in the 
	Managed Package Framework to add the information about the global service proffered 
	by the package to the registry.
	2. In the constructor of the package we add the types of the proffered services to the list of the services
	provided by the package. Notice that at this point we don’t create any instance of the service, but we 
	provide a callback function that will be called the first time a client queries for a specific service. 
	We do this to optimize performance. We don’t want to construct something that might never 
	be used.
	3. The callback function used to create a new instance of the services.
      Open the Reference.Services.sln solution.
      Press F5 to build the sample, register it in the experimental instance, and launch Visual Studio from the experimental instance.
        To see the sample's functionality...
        On the <bold>Tools</bold> menu, click <bold>Get VisualBasic Global Service</bold>.
        A message is displayed in the <bold>Output</bold> window indicating that the global service was consumed.
        <bold>NOTE:</bold> You might find it useful to set breakpoints in the ServicesPackage.CreateService method 
        in ServicesPackage.vb to demonstrate that the event handlers in the ClientPackage class are consuming services.
        On the <bold>Tools</bold> menu, click <bold>Get VisualBasic Local Service</bold>.
        A message is displayed in the <bold>Output</bold> window indicating that a test succeeded. The test guarantees that the 
        client VSPackage failed to consume a local service on another VSPackage. This illustrates that the service provider 
        passed to a VSPackage by Visual Studio cannot be used to consume local services.
        On the Tools menu, click <bold>Get VisualBasic Local using Global Service</bold>.
        A message is displayed in the <bold>Output</bold> window indicating that the local service was consumed by a method on the global service.
      <Title>Visual Studio SDK Website</Title>
      Sample builds without any errors and warnings in both Debug and Release configurations.
      Registration is appropriately entered under the experimental instance.
      After registration in the experimental Visual Studio hive by 
      calling 'Devenv /rootsuffix Exp', execute all 3 of the commands on the Tools menu and validate the outputs in the Output window.
      <Description>Created this sample for the Visual Studio 2005 SDK.</Description>