SqlServer Management Studio 2008 r2 Addon: New request but ServiceCache is not available

I am trying to create an addon for Sql Server Management Studio 2008 R2 that adds an option to the context menu that I would like to open a new query. I can get the context menu item to work, but the execution fails, complaining about the line:

IScriptFactory scriptFactory = ServiceCache.ScriptFactory;

      

objects that are not created. Looking online, it looks like the cache service has been removed for R2, but I can't find an alternative. How is it done now?

The code looks something like this (mostly hashed from other sources - I'm not sure which ones are needed):

using System;
using System.IO;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using System.Windows.Forms;
using Microsoft.VisualStudio.CommandBars;
using Microsoft.SqlServer.Management.UI.VSIntegration;
using Microsoft.SqlServer.Management;
using Microsoft.SqlServer.Management.QueryExecution;
using Microsoft.SqlServer.Management.SqlMgmt;
using Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer;
using Microsoft.SqlServer.Management.UI.VSIntegration.Editors;

namespace SSMSAddon
{
  public class Connect : IDTExtensibility2, IDTCommandTarget
  {

    public Connect(){}

    public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
      _addInInstance = (AddIn)addInInst;

      _applicationObject = (DTE2)_addInInstance.DTE;
      System.Windows.Forms.MessageBox.Show("Connected");
      CommandBar ocommandbar = ((Microsoft.VisualStudio.CommandBars.CommandBars)(_applicationObject.CommandBars))["SQL Files Editor Context"];
      var commands = (Commands2)(_applicationObject.Commands);
      var contextGUIDS = new object[] { };
      try
      {
        Command command =  commands.AddNamedCommand2(_addInInstance, "Commandname", "Commandname", "desc", true, Type.Missing, ref contextGUIDS,
          (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, 3, vsCommandControlType.vsCommandControlTypeButton);
        command.AddControl(ocommandbar, 1);
      }
      catch (ArgumentException e){}
    }

    public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
    {
      if (neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
      {
        if (commandName == "SSMSAddon.Connect.Commandname")
        {
          status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
          return;
        }
      }
    }

    public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
    {
      handled = false;
      if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
      {
        if (commandName == "SSMSAddon.Connect.Commandname")
        {
          try
          {
            EnvDTE.Document activeDocument = _applicationObject.ActiveDocument;
            if (activeDocument != null)
            {
              IScriptFactory scriptFactory = ServiceCache.ScriptFactory;
              scriptFactory.CreateNewBlankScript(ScriptType.Sql,scriptFactory.CurrentlyActiveWndConnectionInfo.UIConnectionInfo, null);
            }
          }
          catch (Exception e)
          {
            System.Windows.Forms.MessageBox.Show(e.Message);
          }

          handled = true;
          return;
        }
      }
    }

    public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom){}
    public void OnAddInsUpdate(ref Array custom){}
    public void OnStartupComplete(ref Array custom){}
    public void OnBeginShutdown(ref Array custom){}

    private DTE2 _applicationObject;
    private AddIn _addInInstance;
  }
}

      

+3


source to share





All Articles