所以我试图使用NDepend API为我的代码上的某些指标建立一个项目(效果很好),但是,当我尝试在其上运行测试框架(NUnit)时,我遇到了TypeInitializationExceptions抛出.
So I'm trying to set up a project using the NDepend API for some metrics on my code (Which works nicely), however, when I attempt to run a testing framework (NUnit) over it, I'm getting TypeInitializationExceptions thrown.
这里有一些代码可以重现我得到的错误:
Here is some code to reproduce the errors I'm getting:
-
创建一个类库项目,并在$NDependInstallPath$\lib\NDependAPI处引用NDepend API dll,并将本地副本设置为false.创建一个类,如下所示:
Create a class library project, and reference the NDepend API dll at $NDependInstallPath$\lib\NDependAPI, setting copy local to false. Create a class as follows:
在解决方案中创建第二个类库项目,该项目将成为您的Test类.引用NUnit和您创建的引用NDependAPI的项目
Create a second class library project in the solution that will be your Test class. Reference NUnit and the project you created that references the NDependAPI
[TestFixture] public class NDependProjectLoader_Tests { [Test] public void I_can_load_an_depend_project() { new NDependProjectLoader().LoadAnNDependProject(); } }
使用您选择的测试运行器构建并运行测试(我已经尝试过Resharper的测试运行器和NUnit GUI).
Build, and run the test using your test runner of choice (I've tried Resharper's test runner and the NUnit GUI).
您将在var provider = new NDependServicesProvider();行上获得TypeInitializationException.
You will get a TypeInitializationException on the line var provider = new NDependServicesProvider();
在TypeInitializationException内部查看,表明根异常的消息是:
Looking inside that TypeInitializationException shows that the root exception's message is:
"{"Could not load file or assembly 'NDepend.Platform.DotNet' or one of its dependencies. The system cannot find the file specified.":"NDepend.Platform.DotNet"}".
-
从NDepend.PowerTools示例项目中添加AssemblyResolverHelper并按照入门指南中的说明进行调用不会改变行为.
Adding the AssemblyResolverHelper from the NDepend.PowerTools sample project and calling it as described in the getting started guide doesn't change the behaviour.
我/假设/问题与 www.ndepend/api/NDepend.API_gettingstarted.html
程序可执行程序集必须生成到$ NDependInstallPath $ \"中
-
如果代码是从测试运行程序启动的,则可执行文件将不在ndepend安装路径中.
In the case where the code is started from a test runner the executable will not be in the ndepend install path.
从在$ NDependInstallPath $ \ lib \中生成.exe的控制台应用程序调用NDependProjectLoader.LoadAnNDependProject()(请注意,该.exe似乎需要在\ lib \子文件夹中生成,而不是在$ NDependInstallPath中)入门指南中所述的$ \)不会产生异常,这进一步指出了原因.
Calling NDependProjectLoader.LoadAnNDependProject() from a console application whose .exe is generated in $NDependInstallPath$\lib\ (note that the .exe seems to need to be generated in the \lib\ subfolder not in $NDependInstallPath$\ as stated in the getting started guide) does not produce the exception, which further points to this being the cause.
因此,问题是,除了控制台应用程序以外,其他方法如何与NDepend API一起使用?例如,在这里我想通过测试跑步者来做.另一个显而易见的候选人是IIS应用程序.我缺少明显的东西吗?
So, the question is, how does one work with the NDepend API from anything other than a console application? For example, here I want to do so from a test runner. Another obvious candidate is an IIS application. Am I missing something obvious?
推荐答案实际上,该问题来自程序集解析. 您必须告诉CLR在哪里可以找到NDepend程序集(即在$ NDependInstallPath $ \ Lib目录中) 您有2个选择可以执行此操作.假设$ NDependInstallPath $是"C:\ NDepend",例如:
Indeed the problem comes from assembly resolution. You must tell the CLR where to find NDepend assemblies (i.e in the dir $NDependInstallPath$\Lib) You have 2 choices to do this. Suppose $NDependInstallPath$ is "C:\NDepend" just for example:
-
可以编辑AssemblyResolverHelper来解析"C:\ NDepend \ Lib"中的程序集
Either edit AssemblyResolverHelper to resolve assemblies in "C:\NDepend\Lib"
为您的exe创建一个App.Config文件,添加引用"C:\ NDepend \ Lib"的<probing>元素.当CLR无法解析程序集时,它将在此类元素中查看dir
Either create a App.Config file for your exe, add a <probing> element that refers to "C:\NDepend\Lib". When the CLR won't resolve an assembly, then it'll look at dir in such element
-
更多推荐
使用NUnit测试时,NDepend TypeInitializationExceptions
发布评论