locked
Programmatically show the WCF Service Dialog - STAThread Exception being thrown RRS feed

  • Question

  • I am trying to programmatically show the WCF Service Dialog in a Visual Studio 2010 add-in, it is exactly like in the third example on this page:

    http://code.msdn.microsoft.com/vssdkwcftools

    The code I am interested in is here in the solution:

    line #126 of Example.ServiceReferenceExplorer\Explorer\ExplorerController.cs

    When I try to call the same code in my add-in I get the following exception:

    Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.

    {System.Threading.ThreadStateException: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. at System.Windows.Forms.ComboBox.set_AutoCompleteMode(AutoCompleteMode value) at Microsoft.VisualStudio.Editors.WCF.UI.AddWebReferenceDialog.InitializeComponent() at Microsoft.VisualStudio.Editors.WCF.UI.AddWebReferenceDialog..ctor() at Microsoft.VisualStudio.Editors.WCF.VsAddWebReferenceDlg3Service.ShowAddWebReferenceDialog(IVsHierarchy pHierarchy, IDiscoverySession pDiscoverySession, ServiceReferenceType referenceTypesAllowed, String pszDialogName, IVsWCFReferenceGroup pExistingReferenceGroup, String pszReferenceConfigContents, IVsAddWebReferenceResult& ppReferenceResult, Int32& pfCancelled)

    private static void Reference2(IVsHierarchy hierarchy)
    {
    
    // Get the global IVsAddWebReferenceDlg3 service.
    IVsAddWebReferenceDlg3 dlgService = (IVsAddWebReferenceDlg3)Package.GetGlobalService(typeof(SVsAddWebReferenceDlg3));
    IVsAddWebReferenceResult result;    //Result of the operation.
    
    try
    {
        int fCanceled;
    
        //Invoke the "Add Service Reference(ASR)" dialog by calling the following method. This
        // returns the result when user clicks OK in the dialog after discovering the service.
        dlgService.ShowAddWebReferenceDialog(
            hierarchy,                  //Associated Project
            null,                       //DiscoverySession
            ServiceReferenceType.SRT_WCFReference,  //Type of service reference to create (old asmx or new WCF service reference)
            null,                       //Title of the dialog (null for default)
            null,                       //Must be null
            null,                       //Reference.Config file (normally null)
            out result,                 //Result
            out fCanceled);             //Boolean value indicating if the user clicked cancel
        //Check if user cancelled or clicked OK.
        if (fCanceled != 0)
        {
    
        }
        else
        {
            //Save the result we obtained from the above method, which includes all the information
            //necessary for the service to add the given service reference to the project, but that
            //does not actually occur until/unless you call Save().
            result.Save();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format(CultureInfo.InvariantCulture, "Uh oh: {0}", ex));
    }
    return;


    I have tried to find the Attrbiute in their example but it does not exist, I have tried to add the attribute to my method but with no luck. Any advice or help would be greatly appreciated <script type="text/javascript"></script> <script type="text/javascript"></script> <script type="text/javascript"></script> <script type="text/javascript"></script>
    Tuesday, March 9, 2010 8:45 AM

Answers

  •  

    Hello, Phill

    The means of the exception message is it needs single-thread to call the ole component, but I don’t think it really matters a lot here.

    Are you sure you are in add-in?

    For add-in, you need to use another way call the service.

    Please reference the following add-in, it is based on your code. It works well on my PC.

            public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)

            {

                handled = false;

                if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)

                {

                    if (commandName == "AddinReferenceDlg.Connect.AddinReferenceDlg")

                    {

                        ServiceProvider sp = new ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)_applicationObject);

                        IVsSolution sln = sp.GetService(typeof(IVsSolution).GUID) as IVsSolution;

                        string path = _applicationObject.Solution.Projects.Item(1).FullName;

                        IVsHierarchy hierarchy;

                        sln.GetProjectOfUniqueName(path, out hierarchy);

                        Reference2(hierarchy);

                        return;

                    }

                }

            }

     

            private void Reference2(IVsHierarchy hierarchy)

            {

                ServiceProvider sp = new ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)_applicationObject);

                IVsAddWebReferenceDlg3 addWebRefDlg = sp.GetService(typeof(SVsAddWebReferenceDlg3).GUID) as IVsAddWebReferenceDlg3;

                IVsSolution sln = sp.GetService(typeof(IVsSolution).GUID) as IVsSolution;

                IVsAddWebReferenceResult result;

                int fCanceled;

     

                addWebRefDlg.ShowAddWebReferenceDialog(

                                         hierarchy,                  //Associated Project

                                         null,                       //DiscoverySession

                                         ServiceReferenceType.SRT_WCFReference,  //Type of service reference to create (old asmx or new WCF service reference)

                                         null,                       //Title of the dialog (null for default)

                                         null,                       //Must be null

                                         null,                       //Reference.Config file (normally null)

                                         out result,                 //Result

                                         out fCanceled);            //Boolean value indicating if the user clicked cancel

            }

     

    Thanks

    Chao

    • Marked as answer by Chao Kuo Monday, March 15, 2010 8:25 AM
    Friday, March 12, 2010 7:06 AM

All replies

  •  

    Hello, Phill

    The means of the exception message is it needs single-thread to call the ole component, but I don’t think it really matters a lot here.

    Are you sure you are in add-in?

    For add-in, you need to use another way call the service.

    Please reference the following add-in, it is based on your code. It works well on my PC.

            public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)

            {

                handled = false;

                if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)

                {

                    if (commandName == "AddinReferenceDlg.Connect.AddinReferenceDlg")

                    {

                        ServiceProvider sp = new ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)_applicationObject);

                        IVsSolution sln = sp.GetService(typeof(IVsSolution).GUID) as IVsSolution;

                        string path = _applicationObject.Solution.Projects.Item(1).FullName;

                        IVsHierarchy hierarchy;

                        sln.GetProjectOfUniqueName(path, out hierarchy);

                        Reference2(hierarchy);

                        return;

                    }

                }

            }

     

            private void Reference2(IVsHierarchy hierarchy)

            {

                ServiceProvider sp = new ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)_applicationObject);

                IVsAddWebReferenceDlg3 addWebRefDlg = sp.GetService(typeof(SVsAddWebReferenceDlg3).GUID) as IVsAddWebReferenceDlg3;

                IVsSolution sln = sp.GetService(typeof(IVsSolution).GUID) as IVsSolution;

                IVsAddWebReferenceResult result;

                int fCanceled;

     

                addWebRefDlg.ShowAddWebReferenceDialog(

                                         hierarchy,                  //Associated Project

                                         null,                       //DiscoverySession

                                         ServiceReferenceType.SRT_WCFReference,  //Type of service reference to create (old asmx or new WCF service reference)

                                         null,                       //Title of the dialog (null for default)

                                         null,                       //Must be null

                                         null,                       //Reference.Config file (normally null)

                                         out result,                 //Result

                                         out fCanceled);            //Boolean value indicating if the user clicked cancel

            }

     

    Thanks

    Chao

    • Marked as answer by Chao Kuo Monday, March 15, 2010 8:25 AM
    Friday, March 12, 2010 7:06 AM
  • Hi Chao,

    You are correct, I should have said that it is a DSL that I am creating, not an Add-in. Will this make more sense for the issue I am having?

    Thanks

    Phill
    BCS Meta Man - coming soon...
    Monday, March 15, 2010 8:49 AM