我正在编写一个测试来检查我的 Unity 配置中的所有控制器是否已正确注册。作为测试的一部分,我使用反射来检查所有继承自System.Web.Mvc.Controller
. 但是,我始终如一地得到一个System.Reflection.ReflectionTypeLoadException
. 我觉得这很奇怪,原因有两个:
- 我的文件顶部有一个 using 语句,它应该会导致这个特定的程序集在测试运行时自动加载。
- 我还尝试预加载程序集。
我的代码是:
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Unity;
namespace Presentation.Tests
{
[TestClass]
public class UnityTests
{
[TestMethod]
public void UnityRegistration_ControllersInitialize()
{
// Initialize the container the same way we do in the code
// but make sure that the container is disconnected from the application
// so we don't accidentally change runtime behavior through testing
var container = UnityRegistration.Initialize(new UnityContainer());
// Get all the controllers in the presentation layer so we can check whether or not
// they can all be constructed at runtime
Assembly.Load("System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
var assembly = Assembly.ReflectionOnlyLoad("Presentation, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");
var ctrls = assembly.GetTypes()
.Where(t => t.IsAssignableFrom(typeof (Controller)) && !t.IsAbstract && t.IsClass);
var builder = new StringBuilder();
foreach (var ctrl in ctrls)
{
// Check resolution on each type. If resolution fails or
// the result is null then we should record the error
bool isErr;
try
{
var obj = container.Resolve(ctrl);
isErr = obj == null;
}
catch
{
isErr = true;
}
if (isErr)
{
builder.AppendLine(string.Format("Controller of type {0} could not be resolved.", ctrl.Name));
}
}
string errors = builder.ToString();
if (!string.IsNullOrWhiteSpace(errors))
{
Assert.Fail(errors);
}
}
}
}
请注意,Controller
此处的引用特指System.Web.Mvc.Controller
.
抛出的异常没有内部异常和大量的loader异常,都说明Cannot resolve dependency to assembly 'System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.
有人可以解释为什么预加载似乎没有以这种方式工作,以及我将如何正确确保加载正确的程序集?