ActiveX control cannot be instantiated because the current thread is not in a single-threaded apartment - from WebBrowser
<p>I want to call the following code from within a unit test in Resharper Test runner.</p>

<pre><code>    /// &lt;summary&gt;
    /// Function to return the OAuth code
    /// &lt;/summary&gt;
    /// &lt;param name="config"&gt;Contains the API configuration such as ClientId and Redirect URL&lt;/param&gt;
    /// &lt;returns&gt;OAuth code&lt;/returns&gt;
    /// &lt;remarks&gt;&lt;/remarks&gt;
    [STAThread]
    public static string GetAuthorizationCode(IApiConfiguration config)
    {
        //Format the URL so  User can login to OAuth server
        string url =
            $"{CsOAuthServer}?client_id={config.ClientId}&amp;redirect_uri={HttpUtility.UrlEncode(config.RedirectUrl)}&amp;scope={CsOAuthScope}&amp;response_type=code";

        // Create a new form with a web browser to display OAuth login page
        var frm = new Form();
        var webB = new WebBrowser();
        frm.Controls.Add(webB);
        webB.Dock = DockStyle.Fill;

        // Add a handler for the web browser to capture content change 
        webB.DocumentTitleChanged += WebBDocumentTitleChanged;

        // navigat to url and display form
        webB.Navigate(url);
        frm.Size = new Size(800, 600);
        frm.ShowDialog();

        //Retrieve the code from the returned HTML
        return ExtractSubstring(webB.DocumentText, "code=", "&lt;");
    }
</code></pre>

<p>When I do I get the following error</p>

<pre><code>System.Threading.ThreadStateException : ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.
   at System.Windows.Forms.WebBrowserBase..ctor(String clsidString)
   at System.Windows.Forms.WebBrowser..ctor()
</code></pre>