【Visual Studio】在VS2012中使用VSXtra
最近工作中需要把之前为VS 2010写的扩展迁移到VS 2012上。在将工程版本改为VS2012之后,代码没有修改,直接编译通过,也可以安装到VS2012上。
不过,在实际使用的时候,却报错,提示“The framework has not been sited!”。调试后发现,这个错误是我们在IDE开发中用到的VSXtra报出的。错误的报错位置是在SiteManager的GetGlobalService方法中:
// --------------------------------------------------------------------------------------------
/// <summary>
/// Gets a global service with the specified address and behavior type.
/// </summary>
/// <typeparam name="SInterface">Address type of the service</typeparam>
/// <typeparam name="TInterface">Behavior (endpoint) type of the service</typeparam>
/// <returns>The service instance obtained.</returns>
// --------------------------------------------------------------------------------------------
public static TInterface GetGlobalService<SInterface, TInterface>()
where TInterface : class
where SInterface : class
{
if (!HasGlobalServiceProvider)
throw new InvalidOperationException("The framework has not been sited!");
var ti = GlobalServiceProvider.GetService<SInterface, TInterface>();
if (ti == null)
throw new NotSupportedException(typeof(SInterface).FullName);
return ti;
}
从代码可以看到,异常抛出的条件是在GlobalServiceProvider不存在的时候。那么这个HasGlobalServiceProvider属性是什么样的呢:
// --------------------------------------------------------------------------------------------
/// <summary>
/// Gets the flag indicating if the SiteManager has already a global service provider or not.
/// </summary>
// --------------------------------------------------------------------------------------------
public static bool HasGlobalServiceProvider
{
get { return (GlobalServiceProvider != null); }
}
看来是判断GlobalServiceProvider是否为null。那么,是谁负责设置这个GlobalServiceProvider的呢?我们通过Find All References可以发现这个属性是在SuggestGlobalServiceProvider方法中被设置的,这个方法有多个重载,跟踪一下,需要调用到的是这个:
// --------------------------------------------------------------------------------------------
/// <summary>
/// Suggests a DTE2 object as the global service provider for SiteManager.
/// </summary>
/// <param name="dte2">DTE2 object as a global service provider candidate.</param>
// --------------------------------------------------------------------------------------------
public static void SuggestGlobalServiceProvider(DTE2 dte2)
{
SuggestGlobalServiceProvider(dte2 as IOleServiceProvider);
}
而这个SuggestGlobalServiceProvider则是在一个叫做TryToGetServiceProviderFromCurrentProcess的关键方法中被调用到的(部分代码省略):
private static void TryToGetServiceProviderFromCurrentProcess(string vsMoniker)
{
...... string ideMoniker = String.Format(vsMoniker, Process.GetCurrentProcess().Id); ......
while (enumMoniker.Next(, moniker, IntPtr.Zero) == )
{
string displayName;
moniker[].GetDisplayName(ctx, moniker[], out displayName);
if (displayName == ideMoniker)
{
// --- Got the IDE Automation Object
Object oDTE;
rot.GetObject(moniker[], out oDTE);
dte = oDTE as DTE2;
if (dte != null) break;
}
} SuggestGlobalServiceProvider(dte);
}
其大概意思是从当前进程中获取一些信息,跟通过传入的vsMoniker格式化之后的一个字符串进行比对,如果名字是一样的,则通过它获取VS IDE的DTE实例。那么,这个vsMoniker是个什么东东?又是谁调用了这个方法呢?
// --------------------------------------------------------------------------------------------
/// <summary>
/// Monikers of possible DTE objects. Right now VS 2008 and VS 2010 is handled.
/// </summary>
// --------------------------------------------------------------------------------------------
private static readonly List<string> VSMonikers =
new List<string>
{
"!VisualStudio.DTE.9.0:{0}",
"!VisualStudio.DTE.10.0:{0}"
}; // --------------------------------------------------------------------------------------------
/// <summary>
/// The static constructor automatically tries to assign the SiteManager static class to a site
/// that accesses VS IDE global services.
/// </summary>
// --------------------------------------------------------------------------------------------
static SiteManager()
{
foreach (string moniker in VSMonikers)
{
TryToGetServiceProviderFromCurrentProcess(moniker);
if (HasGlobalServiceProvider) return;
}
}
看到这儿,我们也明白了,敢情VSXtra这儿写死了几个字符串{ "!VisualStudio.DTE.9.0:{0}", "!VisualStudio.DTE.10.0:{0}" },跟VS版本相关,而VS2012的版本号是11,这里没有,所以当然找不到DTE Instance了。
最简单的修改版本,添加一个VS2012的版本号:"!VisualStudio.DTE.11.0:{0}",运行测试OK。
不过,对于这种hard-code的写法笔者是看着很不爽,那么,有没动态获取已经安装的VS版本的方法呢?答案是:有,通过注册表。

于是最后的代码修改如下:
// --------------------------------------------------------------------------------------------
/// <summary>
/// Monikers of possible DTE objects. Right now VS 2008 and VS 2010 is handled.
/// </summary>
// --------------------------------------------------------------------------------------------
private static readonly IEnumerable<string> VSMonikers; // --------------------------------------------------------------------------------------------
/// <summary>
/// The static constructor automatically tries to assign the SiteManager static class to a site
/// that accesses VS IDE global services.
/// </summary>
// --------------------------------------------------------------------------------------------
static SiteManager()
{
var vsRegKey = Registry.CurrentUser
.OpenSubKey("Software")
.OpenSubKey("Microsoft")
.OpenSubKey("VisualStudio");
var installedNames = vsRegKey.GetSubKeyNames().Where(x => !x.Contains("Exp") && !x.Contains("_Config"));
VSMonikers = installedNames.Select(x => "!VisualStudio.DTE." + x + ":{0}").ToArray(); foreach (string moniker in VSMonikers)
{
TryToGetServiceProviderFromCurrentProcess(moniker);
if (HasGlobalServiceProvider) return;
}
}
【Visual Studio】在VS2012中使用VSXtra的更多相关文章
- 在Visual Studio for Mac中使用fastlane管理iOS的provision
Xamarin开发中,最烦的就是provision的管理了. 全手工的话,要先创建一个key,上传后生成cert文件,再创建provision.如果在手机上调试,还要把手机加到provision中去. ...
- Visual Studio 2019 preview中体验C# 8.0新语法
准备工作: Visual Studio 2019 Preview版本中并没有包含所有的C# 8.0的新功能,但目前也有一些可以试用了.在开始之前,需要进行入两项设置: 将Framework设置为.ne ...
- 在 Visual Studio for Mac 中编译和生成
使用Visual Studio将C#生成DLL文件的方法 https://www.cnblogs.com/AaronBlogs/p/6840283.html Visual Studio 开发 - Vi ...
- 记录visual Studio使用过程中的两个问题
Visual Studio是Windows平台下进行项目管理和开发的终极利器.除了微软自家的技术外,新版的VS不但支持Javascript, Python的开发调试,甚至还支持了Android, iO ...
- 在Visual Studio 2015 Preview 中使用Github 版本控制
打开Visual Studio,新建项目,右下角勾选,如下图: 点击‘OK’后,出现下图窗口,选择'Git' : 如果是现有项目可以在‘文件’菜单下找到‘Add to Source Control’ ...
- 如何通过PowerShell在Visual Studio的Post-build中预热SharePoint站点
问题现象 Visual Studio在开发SharePoint的时候,发布部署包后,首次打开及调试站点页面的时候会非常的慢 解决方案 使用PowerShell脚本,加载SharePoint插件后遍历所 ...
- Visual Studio 2015 RC中的ASP.NET新特性和问题修正
(此文章同时发表在本人微信公众号"dotNET每日精华文章") 微软在Build大会上发布了Visual Studio 2015 RC,这也预示着Visual Studio 201 ...
- 在Visual Studio 2010/2012中 找不到创建WebService的项目模板
参考文章: http://blog.sina.com.cn/s/blog_6d545999010152wb.html 在 Visual Studio 2010 或者2012的新建 Web 应用程序或者 ...
- 在 Visual Studio 调试器中指定符号 (.pdb) 和源文件
查找并指定符号文件和源文件:指定符号加载行为.使用符号和源服务器上:加载符号自动或在要求. 内容 查找符号 (.pdb) 文件 查找源文件 查找符号 (.pdb) 文件 说明 在之前的 Vis ...
- Visual Studio属性配置中使用宏
在学习C语言的时候,我们曾经遇到过一个宏的概念.宏的作用机理本质上是宏的展开,C语言中的宏的用法也有很多种(水其实很深...),不过从感觉上来讲,人们大致上会在以下的场景中,利用宏来解决一些窘境:一是 ...
随机推荐
- 不用synchronized块的话如何实现一个原子的i++?
上周被问到这个问题,没想出来,后来提示说concurrent包里的原子类.回来学习一下. 一.何谓Atomic? Atomic一词跟原子有点关系,后者曾被人认为是最小物质的单位.计算机中的Atomic ...
- Tip和菜单的实现方式
Tip和菜单有类似的功能,即鼠标光标移上去的时候显示指定元素,鼠标光标离开的时候隐藏该元素.如下 示例1:下拉菜单(鼠标移动到“客户服务”上时出现,离开则隐藏) 示例2:水平菜单(鼠标移动到“餐饮美食 ...
- iptables 详解
一:前言 防火墙,其实说白了讲,就是用于实现Linux下访问控制的功能的,它分为硬件的或者软件的防火墙两种.无论是在哪个网络中,防火墙工作的地方一定是在网络的边缘.而我们的任务就是需要去定义到底防 ...
- Java基础の乱弹琴二:break关键字
Java中的break一般用于 跳出一个switch或者循环. 跳出switch基本不用赘述. break跳出循环一般是跳出当前一层循环. 如若需要跳出多层循环可以在break后加标签,然后把标签标注 ...
- Start cluster zookeeper in shell script
cat start-zookeeper.sh #!bin/sh for node in namenode01 datanode01 datanode02 do echo "s ...
- c++字符串互相转换
1.string vs char* //string to char* string str; const char* cch = str.c_str(); ]; strcpy(ch,cch); // ...
- 记忆化搜索 codevs 2241 排序二叉树
codevs 2241 排序二叉树 ★ 输入文件:bstree.in 输出文件:bstree.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 一个边长为n的正三 ...
- 广搜+输出路径 POJ 3414 Pots
POJ 3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13547 Accepted: 5718 ...
- Dubbo架构设计详解(转自shiyanjun.cn)
Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合).从服务模型的角度来看,Dubbo采用的是一种非常简单的模 ...
- codeforces 709C C. Letters Cyclic Shift(贪心)
题目链接: C. Letters Cyclic Shift 题意: 现在一串小写的英文字符,每个字符可以变成它前边的字符即b-a,c-a,a-z这样,选一个字串变换,使得得到的字符串字典序最小; 思路 ...