Is it possible to refresh WCF service reference from VS2010 addin?

邮差的信 提交于 2019-12-03 02:49:11

I believe the visual studio command for this is "Project.UpdateServiceReference". So I guess you can try to select the node you're interested in, and run this command, like this:

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

If you're looking for the more programmatic way to do this, you can do something like the following. This approach does not require using the DTE automation layer which will change the user's selection and execute a command. Note that this is within the context of a VSPackage with an IServiceProvider so that it can get instances to the core Visual Studio interfaces, etc...

You may also be able to do this from within an Addin, but you'd need to get an IServiceProvider and add references to (at least) Microsoft.VisualStudio.Shell.Interop.dll and Microsoft.VisualStudio.WCFReference.Interop. Reference assemblies for these binaries are available in the 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);
                    }
                }
            }
        }
    }
}

I'd also recommend looking at the WCF Service Consumption Tools samples for the Visual Studio 2010 SDK.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!