的DllImport - 适用于测试环境,而不是在ASP.NET MVC [英] DllImport - Works in test context, but not in ASP.NET MVC

查看:142
本文介绍了的DllImport - 适用于测试环境,而不是在ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个ASP.NET MVC应用程序,通过的DllImport 召唤出一个第三方库。

最初,测试如何的DllImport 使用MSTest的工作时,我写了一些简单的测试,以确保我在正确使用它。我能够使用整个API没有问题。由于抽象是进口围绕建成,试验继续表明,进口量按预期工作,我能够与外部API进行通信。

现在我开始访问库的ASP.NET MVC(而不是MSTest的)的情况下。每当我拨打电话的进口,我会遇到以下异常:

  System.AccessViolationException:尝试读取或写入受保护的内存。这通常是指示其他内存已损坏。

有什么我应该要使用的DllImport 在ASP.NET MVC?

下面你可以找到初始化进口的一个例子。

 函数[DllImport(pcrsrv32.dll,入口点=PCRSInitSrv,CallingConvention = CallingConvention.Cdecl)
公共静态外部INT InitSrv(字符串callerName,串INIFILE);

当谈到这是什么样子的第三方库,我们只提供什么C函数的声明如下所示:

  HRESULT _PCRSFN PCRSInitSrv(为const char *名,为const char * INIFILE);


解决方案

使用进程监视器来分析访问在MSTest的外部code VS ASP.NET MVC之间的差异后,我发现,ASP.NET MVC无法发现许多外部code依赖于文件。当寻找文件,包括在我的路径下的所有文件夹进行了检查,但不是我提供的一个 INI 文件发送到外部软件。当通过MSTest的运行时,所有文件都立刻发现没有问题。

在最后,我能够通过添加文件夹路径到我的 PATH 变量'修复'的问题。我不是修复完全满意,但现在的工作。

I am working on an ASP.NET MVC application that calls out to a 3rd party library through DllImport.

Originally, when testing out how DllImport worked using MSTest, I wrote some simple tests to ensure I was using it properly. I was able to use the entire API without an issue. As abstractions were built around the imports, the tests continued to show that the imports were working as expected and I was able to communicate with the external API.

Now I am starting to access that library in the context of ASP.NET MVC (instead of MSTest). Whenever I make a call to the import, I encounter the following exception:

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Is there anything I should be doing in order to use DllImport in ASP.NET MVC?

Below you can find an example of the initialization import.

[DllImport("pcrsrv32.dll", EntryPoint = "PCRSInitSrv", CallingConvention = CallingConvention.Cdecl)]
public static extern int InitSrv(string callerName, string iniFile);

When it comes to what this looks like in the 3rd party library, we are only provided the following for what the C function declaration looks like:

HRESULT _PCRSFN PCRSInitSrv(const char *name, const char *iniFile);

解决方案

After using Process Monitor to profile the differences between accessing the external code in MSTest vs ASP.NET MVC, I found that ASP.NET MVC was unable to find many of the files that the external code relied on. When looking for the files, all folders included in my PATH were checked, but not the one I provided in the ini file sent to the external software. When running through MSTest, all files were immediately found without an issue.

In the end, I was able to 'fix' the issue by adding the folder path to my PATH variable. I'm not entirely happy with the fix, but it works now.

这篇关于的DllImport - 适用于测试环境,而不是在ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆