我在 WCF 服务中引用 c++(非托管)库时遇到了一个非常奇怪的问题。
这里有代码
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using VarSpace.Elaboration.Operations.ManagedWrapper; //Unmanaged c++ namespace
using CustomILException;
namespace ILCommonLibrary.Core
{
/// <summary>
/// Singleton class to obtain a unique-per thread instance of a WPC Core Client.
/// </summary>
public class WPCCoreClient
{
private static readonly Object objLock = new Object();
private readonly Client managedWrapperClient= null; //class coming from C++
private static volatile WPCCoreClient theInstance;
private WPCCoreClient()
{
managedWrapperClient= new VarSpace.Elaboration.Operations.ManagedWrapper.Client();
}
/// <summary>
/// <exception cref="CustomILException.CoreException">Core internal error.</exception>
/// </summary>
/// <returns></returns>
public static WPCCoreClient createInstance()
{
if (theInstance == null) {
try {
lock (objLock)
{
theInstance = new WPCCoreClient(); // HERE I HAVE THE ERROR**
}
} catch (VarSpace.Localization.ManagedWrapper.CoreException ex) {
// error in configuratorView usage
throw ErrorManagement.setException(new CustomILException.CoreException(ex.Message),
ErrorCodes.ERR_CORE_CLIENT_ERROR,
"WPCCoreClient.createInstance",
String.Format("ConfiguratorClient creation error.")
);
}
}
return theInstance;
}
/// <summary>
/// Make an elaboration request to the WPC core system.
/// Manages a request that has any related output data.
/// </summary>
/// <param name="requestXml">xml elaboration input description</param>
/// <param name="timeOut">request timeout in milliseconds.</param>
/// <param name="error">output parameter to obtain a string description of the error.</param>
/// <returns>a boolean to indicate if execution has been correcly executed or not.</returns>
///
public bool sendRequestToWPC(string requestXml, int timeOut, ref string error)
{
bool success;
int idRequest= 0; /* WPC core id of the request elaboration */
error = string.Empty;
DataTable transactionMessages;
using (transactionMessages = new DataTable())
{
try
{
success = this.managedWrapperClient.ExecuteRequest(requestXml, timeOut, ref idRequest, transactionMessages);
if (!success)
{
if (transactionMessages.Rows.Count > 0)
{
error = "Error code: " + transactionMessages.Rows[0].Field<int>("nCode") +
"Error severity: " + transactionMessages.Rows[0].Field<int>("nSeverity") +
"Message: " + transactionMessages.Rows[0].Field<string>("nvcMsgText");
}
else
{
error = SchedulerRequestError.GetErrorMessage(idRequest);
}
}
}
catch (Exception e)
{
error = e.Message;
return false;
}
}
return success;
} // method end
} // class end
} // namespace end
错误如下:
{"'' 的类型初始化器抛出异常。"} {"在导致 C++ 模块加载失败的主要异常之后发生嵌套异常。\n"} 在 System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 错误代码,IntPtr 错误信息) 在 .DoCallBackInDefaultDomain(IntPtr 函数,Void* cookie) 在 .DefaultDomain.Initialize() 在 .LanguageSupport.InitializeDefaultAppDomain(LanguageSupport* ) 在 .LanguageSupport._Initialize(LanguageSupport* ) 在 .LanguageSupport.Initialize(LanguageSupport* )
最奇怪的是,如果我通过控制台应用程序调用这个函数,它可以工作!!!
这是与WCF服务有关的东西,但我真的不知道在哪里验证,我做了很多尝试。
有人能帮我吗?
谢谢!