我正在嘗試在C#(VS2010,.NET 4.0)中編寫Windows服務,它充當長時間運行的OPC客戶端。根據命令行參數,它可以作爲Windows服務運行,也可以作爲具有啓動/停止按鈕的WinForms應用程序來模擬服務(但更易於調試)。C#COMException作爲Windows服務,但WinForms好
我目前使用Interop.OPCAutomation來調用COM。當我作爲WinForms應用程序運行時,一切按預期運行,沒有錯誤。但是,當我作爲Windows服務運行時,出現以下異常:
[System.Runtime.InteropServices.COMException:「沒有這樣的接口支持」 錯誤碼:-2147220990 在OPCAutomation.IOPCAutoServer.Connect(字符串進程id ,對象節點) at C:\ CompanyName \ Projects \ Applications \ Project \ Project.Opc \ Collector1.cs中的Project.Opc.Collector..ctor(String serverName,IEnumerable`1 tagNames,String machineName,Boolean異步) 41
下面是有問題的代碼:
_server = new OPCServer(); //this works OK
try {
_server.Connect(serverName, machineName); // this is where it fails
} catch (Exception exc) {
string message = string.Format("Unable to connect to OPC server {0} on {1}", serverName, machineName);
OpcException exception = new OpcException(message, exc);
Logger.Log(message, LogLevel.Exception, exception);
throw exception;
}
我以SYSTEM的身份運行Windows服務器版本,並且它不適用於允許訪問桌面的開啓或關閉。由於此代碼在WinForms中運行良好,因此可能不是OPC問題。但我不知道爲什麼相同的代碼不能用作服務。
A related article也有類似的問題,但它仍然沒有答案。是否有其他人看到和/或解決了這個問題?
並非每個COM都以Windows服務模式運行。你必須要求供應商確認。 –