Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to "simulate" the Right click/Update service reference command in a VS2010 addin. I have a reference to the containing (Silverlight...) project, I know the name of the service reference and the url of the service.
I've found this: http://dedjo.blogspot.com/2007/03/adding-web-references-to-your-vs.html , but it only works for asmx (it uses System.Web.Services instead of System.ServiceModel), not wcf. Is there any choice but call svcutil from code? if so, how? (do I use svcutil or slsvcutil? How do I call it from inside the addin?)
thanks

like image 396
TDaver Avatar asked Jan 11 '11 16:01

TDaver


People also ask

How do you update a service Reference?

To update a service reference In Solution Explorer, right-click the service reference and then click Update Service Reference. A progress dialog box displays while the reference is updated from its original location, and the service client is regenerated to reflect any changes in the metadata.

How do I add an existing service Reference?

Open Microsoft Visual Studio. Open your project or create a new one. In the Solution Explorer right-click on YourSolution/YourProject/References. Select "Add Service Reference..." from the context menu.

How do I update a Web service Reference in Visual Studio 2015?

In Solution Explorer, open your project's App_WebReferences folder and click the node for the Web reference you want to update. Right-click the reference and click Update Web Reference.


2 Answers

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");
like image 81
Simon Mourier Avatar answered Oct 06 '22 16:10

Simon Mourier


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.

like image 45
Aaron Marten Avatar answered Oct 06 '22 16:10

Aaron Marten