简体   繁体   English

IIS7中的动态DLL加载失败

[英]Dynamic DLL loading in IIS7 fails

We have two web services Running on separate application pools in IIS7. 我们有两个Web服务在IIS7中的单独应用程序池上运行。 The web services are identical, what differs is that one connects to the test Database and the other to the live database. Web服务是相同的,不同之处在于一个连接到测试数据库,另一个连接到实时数据库。

The Web services require a DLL which was written in Delphi to churn out some of the business logic required by the system, which we are loading using Dynamic DLL Loading like so: Web服务需要一个用Delphi编写的DLL,以产生系统所需的一些业务逻辑,我们使用动态DLL加载来加载该逻辑,如下所示:

public static class DynamicLinking
    {
        private static int libHandle;
        private static string dllName;

        public static void init(String pDllName)
        {
            dllName = pDllName;
            libHandle = LoadLibrary(pDllName);
        }

        public static void fini()
        {
            FreeLibrary(libHandle);
        }

        [DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
        static extern int LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);

        [DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
        static extern IntPtr GetProcAddress(int hModule, [MarshalAs(UnmanagedType.LPStr)] string lpProcName);

        [DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
        static extern bool FreeLibrary(int hModule);

        [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        delegate bool QryFunction(string domainString, string qryString,
                                     int outputStringBufferSize, ref string outputStringBuffer);
        public static void ExecuteDLLFunction(string pfunctionName, int bufferSize,
                                               string pDomain, string inputXMLString,
                                                out string outputString)
        {

            if (libHandle == 0)
                throw new Exception(string.Format("Could not load library \"{0}\"", dllName));

            var delphiFunctionAddress = GetProcAddress(libHandle, pfunctionName);
            if (delphiFunctionAddress == IntPtr.Zero)
                throw new Exception(string.Format("Can't find function \"{0}\" in library \"{1}\"", pfunctionName, dllName));

            var queryFunction = (QryFunction)Marshal.GetDelegateForFunctionPointer(delphiFunctionAddress, typeof(QryFunction));

            var outputStringBuffer = new String('\x00', bufferSize);
            var errorMsgBuffer = new String('\x00', bufferSize);

            if (!queryFunction(pDomain, inputXMLString,
                                bufferSize, ref outputStringBuffer))
                throw new Exception(errorMsgBuffer);

            outputString = outputStringBuffer;   

        }
    }

Now the problem lies here: when we start the web the websites, which ever site gets started last will fail to load the dll, while the first one to load will run fine. 现在的问题就在这里:当我们启动网站时,最后启动的站点将无法加载dll,而第一个加载的站点将运行良好。

//edit--------------------------------- What's strange is that when the same DLL is copied in each website this issue arises however, if we link both sites to the same DLL, everything works fine // edit ---------------------------------奇怪的是,当在每个网站中复制相同的DLL时,问题出现了,但是,如果我们将两个站点都链接到同一个DLL,则一切正常

Any insight on this? 有什么见解吗?

您应该将每个站点都放在一个单独的应用程序池中,以便IIS将在其自己的进程中运行每个站点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM