我的问题是我正在尝试使用C#开发Web API,但一切正常,但是我的项目需要从dll创建一个对象,我会在下面列出一些错误。
我想要执行的代码如下:
namespace AccessControl.Objects
{
public class DUMMY
{
private static AxSBXPCLib.AxSBXPC _Machine;
public DUMMY(int machineNumber, string ip, int port, string name)
{
initObject(machineNumber, ip, port, name);
_Machine.ConnectTcpip(machineNumber, ref ip, port, 0);
}
public static void initObject(int machineNumber, string ip, int port, string name)
{
_Machine = new AxSBXPCLib.AxSBXPC();
_Machine.CreateControl();
}
}
}
当我尝试创建类DUMMY的对象时,它会正常工作,直到我得到如下错误:
'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/5/ROOT-1-130601790500037206): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms.resources\v4.0_4.0.0.0_pt_b77a5c561934e089\System.Windows.Forms.resources.dll'. Module was built without symbols.
A first chance exception of type 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll
A first chance exception of type 'System.InvalidOperationException' occurred in System.Web.Http.dll
我尝试过STAThread,创建一个新线程,然而其他解决方案都没有。
有什么想法吗?
PS:我尝试自己处理对象,它在另一个项目(控制台应用程序)中工作正常。我能够将[STAThread]放在程序的Main函数中,但是在这种情况下我似乎无法使它工作......
编辑:
我使用非AxHost对象进行了测试:
namespace AccessControl.Objects
{
public class DUMMY
{
private static SBXPCLib.SBXPC _Machine;
public DUMMY(int machineNumber, string ip, int port, string name)
{
initObject(machineNumber, ip, port, name);
_Machine.ConnectTcpip(machineNumber, ref ip, port, 0);
}
public static void initObject(int machineNumber, string ip, int port, string name)
{
_Machine = new SBXPCLib.SBXPC();
}
}
}
它给了我另一个错误:
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll
A first chance exception of type 'System.InvalidOperationException' occurred in System.Web.Http.dll
在调试时,“_ Machach”获取COM对象,如图所示: