在WCF服务中引用c ++非托管库

时间:2013-08-07 10:31:31

标签: c# c++ .net wcf unmanaged

我在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 

错误如下:

{"The type initializer for '' threw an exception."}

{"A nested exception occurred after the primary exception that caused the C++ module to fail to load.\n"}

   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at .DoCallBackInDefaultDomain(IntPtr function, Void* cookie)
   at .DefaultDomain.Initialize()
   at .LanguageSupport.InitializeDefaultAppDomain(LanguageSupport* )
   at .LanguageSupport._Initialize(LanguageSupport* )
   at .LanguageSupport.Initialize(LanguageSupport* )

最奇怪的是,如果我通过控制台应用程序调用此函数,它就可以工作!!!

这与WCF服务有关,但我真的不知道在哪里验证,我做了很多尝试。

有人可以帮助我吗?

谢谢!

0 个答案:

没有答案