Visual studio DCom/OPC应用程序在visual studio中运行时无法连接,但.exe可以完美连接

Visual studio DCom/OPC应用程序在visual studio中运行时无法连接,但.exe可以完美连接,visual-studio,visual-studio-2010,dcom,opc,Visual Studio,Visual Studio 2010,Dcom,Opc,我正在编写一个OPC客户端,它连接到远程服务器并读取数据等。我正在使用advosol的BGServer类。问题是,当我在VisualStudio中运行该程序时,在添加组时出现以下错误 来自HRESULT的异常:0x80040202 我的问题类似于http://stackoverflow.com/questions/5978721/opc-server-access-remotely-using-opcda-net-tools但是,我知道DCom设置配置正确,因为当我双击.exe运行相同的代码时,

我正在编写一个OPC客户端,它连接到远程服务器并读取数据等。我正在使用advosol的BGServer类。问题是,当我在VisualStudio中运行该程序时,在添加组时出现以下错误

来自HRESULT的异常:0x80040202

我的问题类似于http://stackoverflow.com/questions/5978721/opc-server-access-remotely-using-opcda-net-tools但是,我知道DCom设置配置正确,因为当我双击.exe运行相同的代码时,我可以连接并添加一个没有问题的组

因此,我猜测visual studio是在某个奇怪的用户/组下运行的,并且主要通过回调来破坏dcom权限

编辑:代码

BGServer server;
    private void Form1_Load(object sender, EventArgs e)
    {
        server = new BGServer(this);
        server.Connect(new OPC.Common.Host() { HostName = "xp-devbox2", UserName = "OPCUser", Password = "OPCUser" }, "FactoryTalk Gateway", null, ServerConnected); 
    }
    void ServerConnected(BGException ex, object tag)
    {
        if (ex != null)
        {
            label1.Text = ex.Message;
        }
        else
        {
            //we've connected to the server.  let's start subscribing to stuff!
            server.AddGroup("Tuner DataGroup", true, 1000, 0, null, null, new OnBGSrvAddGroup(GroupAdded));
        }
    }
    private BGGroup dGroup;
    void GroupAdded(BGException ex, BGGroup group, object tag)
    {

        if (ex != null)
        {
            label1.Text = ex.Message;
        }
        else label1.Text = "Group Added";
    }

您是否使用reg free COM与OPC通信?使用Visual Studio宿主进程时,创建do reg free COM的任何外部.exe.manifest文件都不会携带到vshost.exe进程的清单中。

因此,解决此问题的方法是显而易见的。在项目属性的调试部分,取消选中启用Visual Studio宿主进程。这可以摆脱vshost,让我连接。然而,我几乎不知道这会对调试造成什么影响,而且msdn上的文章也没有那么大的帮助,所以请小心使用。此外,我也不知道如何在wpf应用程序中关闭托管进程。。。就这样。