C# SSMS 2012 Add-ObjectExplorerService在SSMSAddinDenali中不可用
此问题涉及此处找到的SSMSAddinDenali SQL Server Management Studio 2012加载项。我无法在尝试构建SSMS 2012加载项时使用.Net 4.0/4.5实现IObjectExplorerService。我想知道是否有人可以公开更多关于连接ObjectExplorerService的信息;如何在数据库表上下文菜单中创建菜单项。我收到此错误(无法将类型Microsoft.SqlServer.Management.SqlStudio.Object.ObjectExplorerService强制转换为类型Microsoft.SqlServer.Management.SqlStudio.Object.ContextService)C# SSMS 2012 Add-ObjectExplorerService在SSMSAddinDenali中不可用,c#,visual-studio-2010,ssms,ssms-2012,ssms-addin,C#,Visual Studio 2010,Ssms,Ssms 2012,Ssms Addin,此问题涉及此处找到的SSMSAddinDenali SQL Server Management Studio 2012加载项。我无法在尝试构建SSMS 2012加载项时使用.Net 4.0/4.5实现IObjectExplorerService。我想知道是否有人可以公开更多关于连接ObjectExplorerService的信息;如何在数据库表上下文菜单中创建菜单项。我收到此错误(无法将类型Microsoft.SqlServer.Management.SqlStudio.Object.Objec
我需要更新的重要参考资料,以便进行构建: Microsoft.SqlServer.Management.SDK.SqlStudio
C:\Program Files(x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\Microsoft.SqlServer.Management.Sdk.SqlStudio.dll版本11.0.0.0 Microsoft.SqlServer.Management.SqlStudio.Explorer
C:\ProgramFiles(x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\Extensions\Application\Microsoft.SqlServer.Management.SqlStudio.Explorer.dll11.0.0版 SqlPackageBase
C:\ProgramFiles(x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\SqlPackageBase.dll11.0.0.0版 SqlManagerUI
C:\ProgramFiles(x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\SQLManagerUI.dll11.0.0.0版 SqlWorkbench.Interfaces
C:\ProgramFiles(x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\SqlWorkbench.Interfaces.dll11.0.0.0版
在我构建我的加载项并在SSMS 2012中运行它之后,我的try-catch出现了这个错误,代码如下所示 任何进一步的帮助都将不胜感激
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
//debug_message("OnConnection::");
//_applicationDTE = (DTE)application;
//_applicationObject = (DTE2)application;
//_addInInstance = (AddIn)addInInst;
//return;
//if (connectMode == ext_ConnectMode.ext_cm_UISetup)
//{
// object[] contextGUIDS = new object[] { };
// Commands2 commands = (Commands2)_applicationObject.Commands;
// string toolsMenuName = "Tools";
// //Place the command on the tools menu.
// //Find the MenuBar command bar, which is the top-level command bar holding all the main menu items:
// Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];
// //Find the Tools command bar on the MenuBar command bar:
// CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
// CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;
// //This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in,
// // just make sure you also update the QueryStatus/Exec method to include the new command names.
// try
// {
// //Add a command to the Commands collection:
// // add + (int)vsCommandStatus.vsCommandStatusEnabled if we want the default state to be enabled
// Command command = commands.AddNamedCommand2(_addInInstance, "FormatSQL", "Format SQL", "Executes the command for SSMSAddinDenali", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
// //Add a control for the command to the tools menu:
// if ((command != null) && (toolsPopup != null))
// {
// command.AddControl(toolsPopup.CommandBar, 1);
// }
// }
// catch (System.ArgumentException)
// {
// //If we are here, then the exception is probably because a command with that name
// // already exists. If so there is no need to recreate the command and we can
// // safely ignore the exception.
// }
//}
#region Object Explorer Context Event
try
{
/* Microsoft.SqlServer.Management.UI.VSIntegration.ServiceCache
* is from SqlPackageBase.dll and not from Microsoft.SqlServer.SqlTools.VSIntegration.dll
* the code below just throws null exception if you have wrong reference */
ObjectExplorerService objExplorerService;
ContextService cs;
NavigationService ns;
objExplorerService = (ObjectExplorerService)ServiceCache.ServiceProvider.GetService(typeof(Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.IObjectExplorerService));
cs = (ContextService)objExplorerService.Container.Components[0];
//cs.ObjectExplorerContext.CurrentContextChanged += new NodesChangedEventHandler(ObjectExplorerContext_CurrentContextChanged);
//cs.UtilityExplorerContext.CurrentContextChanged += new NodesChangedEventHandler(UtilityExplorerContext_CurrentContextChanged);
ns = (NavigationService)objExplorerService.Container.Components[1];
//objExploreS = (ObjectExplorerService)objExplorerService.Container.Components[2];
//sqlStudioExplorer = new SqlStudioExplorer();
//ObjectExplorerService objExplorerService = (ObjectExplorerService)ServiceCache.ServiceProvider.GetService(typeof(IObjectExplorerService));
//INodeInformation node = objExplorerService.FindNode(cs.Context);
//if (node.Parent.InvariantName.Equals("Databases"))
//{
// if (_databaseMenu == null)
// {
// string value = typeof(IMenuHandler).ToString();
// _databaseMenu = (HierarchyObject)node.GetService(typeof(IMenuHandler));
// String wtf = _databaseMenu.ToString();
// RestoreMenuItem rst = new RestoreMenuItem();
// _databaseMenu.AddChild(string.Empty, rst);
// }
//}
//ContextService cs = (ContextService)objExplorerService.Container.Components[0];
//cs.ObjectExplorerContext.CurrentContextChanged += new NodesChangedEventHandler(Provider_SelectionChanged);
// for some reason calling GetSelectedNodes forces to add ContextService to ObjectExplorerService.Container.Components
//int count = objExplorerService.Container.Components.Count;
//int nodeCount; INodeInformation[] nodes;
//objExplorerService.GetSelectedNodes(out nodeCount, out nodes);
//count = nodeCount; count = nodes.Length;
//count = objExplorerService.Container.Components.Count;
//ContextService contextService = (ContextService)objExplorerService.Container.Components[1];
//INavigationContextProvider provider = contextService.ObjectExplorerContext;
//provider.CurrentContextChanged += new NodesChangedEventHandler(Provider_SelectionChanged);
}
catch (Exception ex)
{
MessageBox.Show("OnConnection::ERROR " + ex.Message);
}
#endregion
}
试着这样做:
_contextService = (ContextService) ServiceCache.ServiceProvider.GetService(typeof (IContextService));
_contextService.ActionContext.CurrentContextChanged += ActionContextOnCurrentContextChanged;
我从坟墓里知道这个。您的问题是因为在数组中,objExplorerService.Container.Components ContextService并不总是数组中的第一个元素。在我的工作站上,您拥有的代码可以工作,但当我将我的ssms加载项提供给其他人时,它无法工作,因为NavigationServer位于元素[0]。当然,在下面的示例中,您需要删除我的日志记录和其他内容,但如果您一年前没有放弃,这应该会让您继续
objExplorerService = (ObjectExplorerService)ServiceCache.ServiceProvider.GetService(typeof(IObjectExplorerService));
//cs = (ContextService)objExplorerService.Container.Components[0];
//cs.ObjectExplorerContext.CurrentContextChanged += new NodesChangedEventHandler(ObjectExplorerContext_CurrentContextChanged);
// cs.UtilityExplorerContext.CurrentContextChanged += new NodesChangedEventHandler(UtilityExplorerContext_CurrentContextChanged);
int count = objExplorerService.Container.Components.Count;
int nodeCount; INodeInformation[] nodes;
objExplorerService.GetSelectedNodes(out nodeCount, out nodes);
count = nodeCount; count = nodes.Length;
count = objExplorerService.Container.Components.Count;
ContextService contextService;
try
{
contextService = (ContextService)objExplorerService.Container.Components[1];
}
catch (Exception ex)
{
using (StreamWriter w = File.AppendText("c:\\temp\\log.txt"))
{
Log(ex.Message, w);
Log("Failed Adding Event ", w);
}
contextService = (ContextService)objExplorerService.Container.Components[0];
}
INavigationContextProvider provider = contextService.ObjectExplorerContext;
provider.CurrentContextChanged += new NodesChangedEventHandler(ObjectExplorerContext_CurrentContextChanged);
尝试将代码放在ext_ConnectMode.ext_cm_ui设置中
if (connectMode == ext_ConnectMode.ext_cm_UISetup)
{
// < Your code here >
}
else if (connectMode == ext_ConnectMode.ext_cm_AfterStartup)
{
...
}
if(connectMode==ext\u connectMode.ext\u cm\u ui设置)
{
//<您的代码在这里>
}
else if(connectMode==ext\u connectMode.ext\u cm\u afterstart)
{
...
}
这可能会为您指明正确的方向。我有一个类似的问题,这帮助我解决了它。