我已经完成了与此相关的所有问题,并且找不到任何问题,因此,我在问这个问题。
我有一个构建任务(添加为AfterBuild
目标),以验证类型名称。这些类型名称是正在构建的silverlight项目中的完全限定类型名称。
要解析这些类型名称,请使用Type.ReflectionOnlyGetType()
。要加载从属程序集,我处理AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve
事件以使用Assembly.ReflectionOnlyLoadFrom(filepath)
从Silverlight安装路径加载项目输出路径和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, Version=2.0.5.0, Culture=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的一部分,并且仍然是相同的。
答案 0 :(得分:1)
我确信这不是我上面发布的问题的答案。但是,这就是我解决它的方式。
我浏览过论坛和文档,发现了DEVENV和MSBUILD之间的基本区别。使用MSBuild进行构建时,装配分辨率的严格程度与Devenv相同。
另外,我已经完成了Assembly.LoadFrom
和Assembly.ReflectionOnlyLoadFrom
之间的区别。 Assembly.RefelectionOnlyLoadFrom
仅考虑文件名,不检查版本等。这是MSBuild从.Net 4.0堆栈加载System.dll的地方,因为它必须从silverlight安装路径加载System.dll。
考虑到所有这些,我尝试使用Assembly.LoadFrom
加载程序集并使用Type.GetType
来解析类型。这次,我遇到了StackOverflowException
,因为所有已加载的程序集都会重复触发AssemblyResolve
事件。
这时我想到将程序集加载到一个单独的appdomain中,这样我也可以卸载已加载的程序集。我发现this文章非常有用,并遵循类似的模式来解决问题。
虽然我手头已经解决了问题,但我不想为这个答案获得积分。我仍然觉得我没有为MSBuild的问题提供答案。
希望这可能对其他人有所帮助。
答案 1 :(得分:0)
考虑使用AppDomainIsolatedTask作为基类,然后重试。这使得处理程序集更容易,因为程序集不会加载到msbuild AppDomain中。
这也将您的事件调用隔离到您自己的AppDomain。所以MsBuild也不能打扰你。