VSXtra-like implementation of VS SDK's Services Reference Sample

Services Package source code:

using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell;
using VSXtra;

namespace DeepDiver.ServicesReference
{
  [PackageRegistration(UseManagedResourcesOnly = true)]
  [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0")]
  [ProvideService(typeof(SMyGlobalService))]
  [Guid(GuidList.guidServicesPkgString)]
  public sealed class ServicesPackage : PackageBase
  {
  }
}
Global service interfaces:

using System.Runtime.InteropServices;

namespace DeepDiver.ServicesReference
{
  [Guid("ECD7206C-DBA2-407d-B77D-725A0E3251B6")]
  [ComVisible(true)]
  public interface IMyGlobalService
  {
    void GlobalServiceFunction();
    int CallLocalService();
  }

  [Guid("4560D44E-5782-4237-A058-317B3ED801B7")]
  public interface SMyGlobalService
  {
  }
}
Global service implementation:

using VSXtra;

namespace DeepDiver.ServicesReference
{
  [Promote]
  public class MyGlobalService : VsxService<ServicesPackage, SMyGlobalService>,
    IMyGlobalService
  {
    public void GlobalServiceFunction()
    {
      var outputText = " ======================================\n" +
                       "\tGlobalServiceFunction called.\n" +
                       " ======================================\n";
      OutputWindow.General.WriteLine(outputText);
    }

    public int CallLocalService()
    {
      var localService = ServiceProvider.GetService<SMyLocalService, IMyLocalService>();
      if (null == localService)
      {
        OutputWindow.Debug.WriteLine("Can not get the local service from the global one.");
        return -1;
      }
      return localService.LocalServiceFunction();
    }
  }
}
Local service interfaces:

using System.Runtime.InteropServices;

namespace DeepDiver.ServicesReference
{
  [Guid("54DB054C-2AE0-4204-915F-6B7EA96FC63E")]
  [ComVisible(true)]
  public interface IMyLocalService
  {
    int LocalServiceFunction();
  }

  [Guid("E582E3A6-24AD-41d6-9E7C-88255BD60627")]
  public interface SMyLocalService
  {
  }
}
Local service implementation:

using VSXtra;

namespace DeepDiver.ServicesReference
{
  [AutoCreateService]
  public class MyLocalService : VsxService<ServicesPackage, SMyLocalService>,
    IMyLocalService, SMyLocalService
  {
    public int LocalServiceFunction()
    {
      var outputText = " ======================================\n" +
                       "\tLocalServiceFunction called.\n" +
                       " ======================================\n";
      OutputWindow.General.WriteLine(outputText);
      return 0;
    }
  }
}
Client Package source code:

using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell;
using VSXtra;

namespace DeepDiver.ServicesReference
{
  [PackageRegistration(UseManagedResourcesOnly = true)]
  [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0")]
  [InstalledProductRegistration(false, "#110", "#112", "1.0", IconResourceID = 400)]
  [ProvideLoadKey("Standard", "1.0", "ServicesReference", "DeepDiver", 1)]
  [ProvideMenuResource(1000, 1)]
  [Guid(GuidList.guidClientPkgString)]
  public class ClientPackage : PackageBase
  {
  }
}
Command handlers -- Service invocation:

using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell;
using VSXtra;

namespace DeepDiver.ServicesReference
{
  [Guid(GuidList.guidClientCmdSetString)]
  public sealed class ClientCommandGroup : CommandGroup<ClientPackage>
  {
    [CommandId(CmdIDs.cmdidClientGetGlobalService)]
    public sealed class GetGlobalServiceCommand : MenuCommandHandler
    {
      protected override void OnExecute(OleMenuCommand command)
      {
        var service = ServiceProvider.GetService<SMyGlobalService, IMyGlobalService>();
        if (null == service)
        {
          OutputWindow.Debug.WriteLine("Can not get the global service.");
          return;
        }
        service.GlobalServiceFunction();
      }
    }

    [CommandId(CmdIDs.cmdidClientGetLocalService)]
    public sealed class GetLocalServiceCommand : MenuCommandHandler
    {
      protected override void OnExecute(OleMenuCommand command)
      {
        var service = ServiceProvider.GetService<SMyLocalService, IMyLocalService>();
        if (null != service)
        {
          OutputWindow.Debug.WriteLine("GetService for the local service succeeded, but it should fail.");
          return;
        }
        var outputText = " ======================================\n" +
                         "\tGetLocalServiceCallback test succeeded.\n" +
                         " ======================================\n";
        OutputWindow.General.WriteLine(outputText);
      }
    }

    [CommandId(CmdIDs.cmdidClientGetLocalUsingGlobal)]
    public sealed class GetLocalUsinGlobalServiceCommand : MenuCommandHandler
    {
      protected override void OnExecute(OleMenuCommand command)
      {
        var service = ServiceProvider.GetService<SMyGlobalService, IMyGlobalService>();
        if (null == service)
        {
          OutputWindow.General.WriteLine("Can not get the global service.");
          return;
        }
        service.CallLocalService();
      }
    }
  }
}
Screenshots:

ServicesReferenceOutput.png

Last edited Aug 5, 2008 at 8:04 AM by INovak, version 4

Comments

DenisVuyka Sep 19, 2008 at 7:33 PM 
How to ensure this sample is working? Can you give a sample for button declaration?