【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语言中的宏的用法也有很多种(水其实很深...),不过从感觉上来讲,人们大致上会在以下的场景中,利用宏来解决一些窘境:一是 ...
随机推荐
- redis AOF保存机制
网上说AOF有三种保存方式,不自动保存.每秒自动保存.每命令自动保存. 其中每秒自动保存这个看起来很美好,但是可能会被各种IO的时间所延迟,所以究竟是怎么判断每秒保存的,并不是太明白,故有此文. AO ...
- 最完整PHP.INI中文版
;;;;;;;;;;;;;;;;;;; 关于php.ini ;;;;;;;;;;;;;;;;;;;; 这个文件必须命名为'php.ini'并放置在httpd.conf中PHPINIDir指令指定的目录 ...
- tmpFile.renameTo(classFile) failed 错误
突然的出现了这个tmpFile.renameTo(classFile) failed 错误. 也许是我删掉了tomcat里面的webapps 中的项目,而通过debug部署,而出现了这个问题. 一开始 ...
- Gulp使用入门操作十一步压缩JS
前提需要安装nodejs 一. 全局安装Gulp npm install -g gulp 二.新建一个 gulpfile.js 文件 chapter2└── gulpfile.js 三.在 gulpf ...
- Linux Strip
一.简介 strip经常用来去除目标文件中的一些符号表.调试符号表信息,以减小程序的大小. 二.语法 https://sourceware.org/binutils/docs/binutils/str ...
- Unity3d内置浏览器
uWebKit是一个Unity3d插件,个人认为比较强大,值得收藏啊 有图有真相: 安装以及破解说明: 1.导入资源包 2.将破解目录里的Editor复制到工程项目的Assets目录下进行覆盖 3.打 ...
- ZOJ 3232 It's not Floyd Algorithm --强连通分量+Floyd
题意:给你一个传递闭包的矩阵,mp[u][v] = 1表示u可以到达v,为0代表不可到达,问你至少需要多少条边组成的传递闭包符合这个矩阵给出的关系 分析:考虑一个强连通分量,如果这个分量有n个节点,那 ...
- 2016百度之星-初赛(Astar Round2A)AII X
Problem Description F(x,m) 代表一个全是由数字x组成的m位数字.请计算,以下式子是否成立: F(x,m) mod k ≡ c Input 第一行一个整数T,表示T组数据. 每 ...
- 一款免费好用的正则表达式工具:Regex Match Tracer
推荐分享:一款免费好用的正则表达式工具:Regex Match Tracer v2.1.5 free version 下载地址:Regex Match Tracer
- PNG文件
png格式主要由六大块组成:文件头.IHDR块.PLTE块.tRNS块.IDAT块.文件尾文件头一般是 8950 4E47 0D0A 1A0A而本题提示中的IHDR块是png中用来描述图片的基本信息, ...