最近工作中需要把之前为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;
}
}

附:修改后的VSXtra

【Visual Studio】在VS2012中使用VSXtra的更多相关文章

  1. 在Visual Studio for Mac中使用fastlane管理iOS的provision

    Xamarin开发中,最烦的就是provision的管理了. 全手工的话,要先创建一个key,上传后生成cert文件,再创建provision.如果在手机上调试,还要把手机加到provision中去. ...

  2. Visual Studio 2019 preview中体验C# 8.0新语法

    准备工作: Visual Studio 2019 Preview版本中并没有包含所有的C# 8.0的新功能,但目前也有一些可以试用了.在开始之前,需要进行入两项设置: 将Framework设置为.ne ...

  3. 在 Visual Studio for Mac 中编译和生成

    使用Visual Studio将C#生成DLL文件的方法 https://www.cnblogs.com/AaronBlogs/p/6840283.html Visual Studio 开发 - Vi ...

  4. 记录visual Studio使用过程中的两个问题

    Visual Studio是Windows平台下进行项目管理和开发的终极利器.除了微软自家的技术外,新版的VS不但支持Javascript, Python的开发调试,甚至还支持了Android, iO ...

  5. 在Visual Studio 2015 Preview 中使用Github 版本控制

    打开Visual Studio,新建项目,右下角勾选,如下图: 点击‘OK’后,出现下图窗口,选择'Git' : 如果是现有项目可以在‘文件’菜单下找到‘Add to Source Control’ ...

  6. 如何通过PowerShell在Visual Studio的Post-build中预热SharePoint站点

    问题现象 Visual Studio在开发SharePoint的时候,发布部署包后,首次打开及调试站点页面的时候会非常的慢 解决方案 使用PowerShell脚本,加载SharePoint插件后遍历所 ...

  7. Visual Studio 2015 RC中的ASP.NET新特性和问题修正

    (此文章同时发表在本人微信公众号"dotNET每日精华文章") 微软在Build大会上发布了Visual Studio 2015 RC,这也预示着Visual Studio 201 ...

  8. 在Visual Studio 2010/2012中 找不到创建WebService的项目模板

    参考文章: http://blog.sina.com.cn/s/blog_6d545999010152wb.html 在 Visual Studio 2010 或者2012的新建 Web 应用程序或者 ...

  9. 在 Visual Studio 调试器中指定符号 (.pdb) 和源文件

    查找并指定符号文件和源文件:指定符号加载行为.使用符号和源服务器上:加载符号自动或在要求.   内容 查找符号 (.pdb) 文件 查找源文件   查找符号 (.pdb) 文件 说明 在之前的 Vis ...

  10. Visual Studio属性配置中使用宏

    在学习C语言的时候,我们曾经遇到过一个宏的概念.宏的作用机理本质上是宏的展开,C语言中的宏的用法也有很多种(水其实很深...),不过从感觉上来讲,人们大致上会在以下的场景中,利用宏来解决一些窘境:一是 ...

随机推荐

  1. matchesSelector及低版本IE中对该方法的实现

    matchesSelector用来匹配dom元素是否匹配某css selector.它为一些高级方法的实现提供了基础支持,比如事件代理,parent, closest等. W3C在2006年就提出了该 ...

  2. Hadoop Yarn core concepts

    The fundamental idea of YARN is to split the two major responsibilities of the JobTracker—that is, r ...

  3. 杂谈SharpDx中的WIC组件——我们需要WIC的图片编码功能么?

    在前文 SharpDX之Direct2D教程II——加载位图文件和保存位图文件 中,发现在VB2010中不能很好的运用SharpDx中的WIC组件进行图片的编码工作.可能是我的设置问题,也可能是Sha ...

  4. poj1459 Power Network (多源多汇最大流)

    Description A power network consists of nodes (power stations, consumers and dispatchers) connected ...

  5. POJ 2773 Happy 2006【GCD/欧拉函数】

    根据欧几里德算法,gcd(a,b)=gcd(a+b*t,b) 如果a和b互质,则a+b*t和b也互质,即与a互质的数对a取模具有周期性. 所以只要求出小于n且与n互质的元素即可. #include&l ...

  6. [反编译U3D]Decompile Unity Resources

    工具说明 反编译unity project资源文件,包括ios,android,pc等平台资源,仅供学习使用! 以下工具任选其一,未有特殊说明是可以同时提取unity3.x,unity4.x,unit ...

  7. cnblog code syntaxhighlighter view

    wlw代码插件 测试多款 wlw插入代码插件 在博客园的代码高亮效果 1.Code Snippet 1: public override void Update() 2: { 3: base.Upda ...

  8. Flash Builder快捷键

    代码助手:Ctrl+Space(简体中文操作系统是Alt+/)快速修正:Ctrl+1单词补全:Alt+/打开外部Java文档:Shift+F2 显示搜索对话框:Ctrl+H快速Outline:Ctrl ...

  9. vmware12安装vmtools

    一. 1. 机器要开启支持BIOS的选项. 二. 1. 点击vmware 应用 vmware install 2. 虚拟机: tar  xvf vmtools-distb.tgr.gz 3. 虚拟机: ...

  10. c# 扩展方法奇思妙用

    # 扩展方法出来已久,介绍扩展方法的文章也很多,但都是笼统的.本人最近一直在思考扩展方法的应用,也悟出了一些,准备将这最近一段时间对扩展方法的思考,写成一个系列文章.每个文章只介绍一个应用方面,篇幅不 ...