【问题标题】:Is it possible to refresh WCF service reference from VS2010 addin?是否可以从 VS2010 插件刷新 WCF 服务参考?
【发布时间】:2011-01-11 16:27:22
【问题描述】:

我想在 VS2010 插件中“模拟”右键单击/更新服务引用命令。我有对包含 (Silverlight...) 项目的引用,我知道服务引用的名称和服务的 url。
我发现了这个:http://dedjo.blogspot.com/2007/03/adding-web-references-to-your-vs.html,但它只适用于 asmx(它使用 System.Web.Services 而不是 System.ServiceModel),而不是 wcf。 是否有任何选择从代码中调用 svcutil?如果是这样,如何? (我使用 svcutil 还是 slsvcutil?如何从插件内部调用它?)
谢谢

【问题讨论】:

  • 没人?拜托,这是我的班级生成插件中缺少的最后一块......

标签: wcf visual-studio-2010 add-in vsx


【解决方案1】:

我相信 Visual Studio 的命令是“Project.UpdateServiceReference”。所以我猜你可以尝试选择你感兴趣的节点,然后运行这个命令,像这样:

envDTE.Windows.Item(vsWindowKindSolutionExplorer).Activate();
envDTE.ActiveWindow.Object.GetItem(@"MyProject\Service References\Proxy").Select(vsUISelectionType.vsUISelectionTypeSelect);
envDTE.ExecuteCommand("Project.UpdateServiceReference");

【讨论】:

  • 天哪,谢谢!我会在星期一的第一件事上尝试一下,看看它是否有效! (奖励您 50...),但有一件事:我可以在解决方案资源管理器中不选择它的情况下刷新服务吗?
  • 好的,我得到了这个,因为我没有指定选择不能改变,所以我写了一个简单的解决方法,所以我可以使用这个更简单的方法! :)
【解决方案2】:

如果您正在寻找更程序化的方式来执行此操作,您可以执行以下操作。这种方法不需要使用 DTE 自动化层,它会改变用户的选择并执行命令。请注意,这是在带有 IServiceProvider 的 VSPackage 的上下文中,因此它可以将实例获取到核心 Visual Studio 接口等...

您也可以在插件中执行此操作,但 you'd need to get an IServiceProvider 并添加对(至少)Microsoft.VisualStudio.Shell.Interop.dll 和 Microsoft.VisualStudio.WCFReference.Interop 的引用。这些二进制文件的参考程序集在Visual Studio 2010 SDK 中提供。

IVsSolution solution = GetService(typeof(SVsSolution)) as IVsSolution;
if (solution != null)
{
    IVsHierarchy solutionHierarchy = solution as IVsHierarchy;
    if (null != solutionHierarchy)
    {
        IEnumHierarchies enumHierarchies;
        Guid nullGuid = Guid.Empty;

        ErrorHandler.ThrowOnFailure(solution.GetProjectEnum((uint)__VSENUMPROJFLAGS.EPF_ALLINSOLUTION, ref nullGuid, out enumHierarchies));
        if (enumHierarchies != null)
        {
            uint fetched;
            IVsHierarchy[] hierarchies = new IVsHierarchy[1];
            IVsWCFReferenceManagerFactory wcfReferenceManagerFactory = GetService(typeof(SVsWCFReferenceManagerFactory)) as IVsWCFReferenceManagerFactory;
            if (wcfReferenceManagerFactory != null)
            {
                while (enumHierarchies.Next(1, hierarchies, out fetched) == 0 && fetched == 1)
                {
                    if (wcfReferenceManagerFactory.IsReferenceManagerSupported(hierarchies[0]) == 1)
                    {
                        IVsWCFReferenceManager referenceManager = wcfReferenceManagerFactory.GetReferenceManager(hierarchies[0]);
                        var referenceGroupCollection = referenceManager.GetReferenceGroupCollection();
                        referenceGroupCollection.UpdateAll(null);
                    }
                }
            }
        }
    }
}

我还建议查看 Visual Studio 2010 SDK 的 WCF Service Consumption Tools samples

【讨论】:

  • 什么类有GetService方法?我已阅读您发送的链接,但我不知道如何从 DTE 获取 IServiceProvider...
  • 我尝试将 DTE 转换为 IServiceProvider(使用转换运算符,因为使用“as”返回 null)我得到了 Unable to cast COM object of type 'System.__ComObject' to interface type 'System.IServiceProvider'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{8F10F540-7F5D-3F37-8D79-1E0AEB074AA0}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
  • 好的,设法获得 IServiceProvider(参见 here ),while 循环被调用四次,然后它抛出异常(Operation is not valid due to the current state of the object.)——这很奇怪,因为我只有一项服务在我的整个解决方案中引用!
猜你喜欢
  • 1970-01-01
  • 2013-03-23
  • 2013-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-29
  • 2011-08-03
  • 2011-11-24
相关资源
最近更新 更多