我查看了与此相关的SO问题,但没有找到解决这个问题的任何信息,因此我提出了这个问题。
我有一个构建任务(添加为AfterBuild
目标),用于验证类型名称。这些类型名称是正在构建的silverlight项目中的完全限定类型名称。
为了解析这些类型名称,我使用Type.ReflectionOnlyGetType()
。为了加载依赖项程序集,我处理AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve
事件,从项目输出路径加载特定于项目的程序集和从silverlight安装路径使用Assembly.ReflectionOnlyLoadFrom(filepath)
加载silverlight基本程序集。
在使用VS2010构建项目时,这完全正常工作,但在使用MSBuild构建时失败,即C:\ WINDOWS \ Microsoft.NET \ Framework \ v4.0.30319 \ msbuild.exe“C:\ Root \ branches \ x.x.x \ clients.sln”/t:rebuild /p:Configuration=Debug“/p:Platform=Any CPU”/v:quiet /maxcpucount:1
使用MSBuild构建时,ReflectionOnlyAssemblyResolve
事件将针对"System,版本=2.0.5.0,文化=neutral,PublicKeyToken=7cec85d7bea7798e"
触发,并且该程序集从silverlight安装路径中加载。但是,搜索此系统程序集时会尝试在"C:/WINDOWS/Microsoft.NET/Framework/v4.0.30319/System.DLL"
中进行探测,由于主版本不匹配而失败。
以下是我代码的剥离版本,针对此问题:
public class ValidateTypeTask : Task
{
public override bool Execute()
{
Initialize();
List<string> typeNames = GetTypes();
foreach (var typeName in typeNames)
{
var isResolved = IsResolvable(typeName);
if (!isResolved)
{
return false;
}
}
CleanUp();
return true;
}
private void Initialize()
{
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler(CurrentDomain_ReflectionOnlyAssemblyResolve);
}
Assembly CurrentDomain_ReflectionOnlyAssemblyResolve(object sender, ResolveEventArgs args)
{
Assembly loadedAssembly = null;
Exception loadedException = null;
try
{
loadedAssembly = Assembly.ReflectionOnlyLoad(args.Name);//this will fail always
}
catch (Exception ex)
{
loadedException = ex;
}
if (loadedAssembly != null)
{
return loadedAssembly;
}
string assemblyPath = string.Empty;
var assemblyName = new AssemblyName(args.Name);
if (args.Name.StartsWith("System"))
{
assemblyPath = @"c:\Program Files\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0";
}
else
{
assemblyPath = @"C:\Root\branches\x.x.x\bin\debug";
}
assemblyPath = string.Format(@"{0}\{1}.dll", assemblyPath, assemblyName.Name);
if (File.Exists(assemblyPath))
{
loadedAssembly = Assembly.ReflectionOnlyLoadFrom(assemblyPath);
}
if (loadedAssembly == null)
{
throw loadedException;
}
return loadedAssembly;
}
private void CleanUp()
{
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve -= new ResolveEventHandler(CurrentDomain_ReflectionOnlyAssemblyResolve);
}
private List<string> GetTypes()
{
return new List<string>();
}
private bool IsResolvable(string typeName)
{
Type resolvedType = null;
try
{
resolvedType = Type.ReflectionOnlyGetType(typeName, true, true);
}
catch (Exception ex)
{
}
if (resolvedType != null)
{
return true;
}
return false;
}
}
我收到的异常如下所示:
System.IO.FileLoadException: Could not load file or assembly 'System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, Boolean loadTypeFromPartialName)
at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
at System.Type.ReflectionOnlyGetType(String typeName, Boolean throwIfNotFound, Boolean ignoreCase)
at in c:\root\x.xx.x\BuildTasks\ValidateTypesTask.cs line xxx
Assembly manager loaded from: C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\msbuild.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: User = domain\username
LOG: DisplayName = System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
(Fully-specified)
LOG: Appbase = file:///C:/WINDOWS/Microsoft.NET/Framework/v4.0.30319/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This is an inspection only bind.
LOG: Using application configuration file: C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\msbuild.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Attempting download of new URL file:///C:/WINDOWS/Microsoft.NET/Framework/v4.0.30319/System.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
我也尝试将Silverlight程序集路径包含到AssemblySearchPaths项目属性中,以便让其成为探测URL的一部分,但仍然是相同的情况。