我有一个自定义的MSBuild任务,可以在程序集中查看属性元数据。
Assembly assembly = Assembly.ReflectionOnlyLoadFrom(AssemblyFile)
这是由我们的自动构建/发布过程使用的,并且已经完美地针对类库,控制台应用程序和Web项目使用和引用的程序集。在另一个MSBuild进程编译项目之后调用MSBuild任务。
昨天我添加了一个引用这个特定程序集的WPF项目 - 一个.NET 3.5类库。
System.IO.FileLoadException: API restriction: The assembly 'file:///bogus.dll' has already loaded from a different location.
It cannot be loaded from a new location within the same appdomain.
at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, StackCrawlMark& stackMark)
at System.Reflection.Assembly.ReflectionOnlyLoadFrom(String assemblyFile)
at RadicaLogic.MSBuild.Tasks.GetAssemblyAttribute.Execute()
at Microsoft.Build.BuildEngine.TaskEngine.ExecuteInstantiatedTask(EngineProxy engineProxy, ItemBucket bucket, TaskExecutionMode howToExecuteTask, ITask task, Boolean& taskResult)
我知道它与WPF相关,因为如果我将AssemblyFile更改为指向WPF项目未引用的同一解决方案中的另一个程序集,则不会引发异常。
异常消息提及
... already loaded from a different location.
It cannot be loaded from a new location within the same appdomain.
请注意有关相同应用域的部分。
所以我修改了代码来捕获这个特殊的异常并查看CurrentDomain:
Assembly assembly = null;
try
{
assembly = Assembly.ReflectionOnlyLoadFrom(AssemblyFile);
}
catch (FileLoadException)
{
List<string> searched = new List<string>();
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
{
if (Path.GetFileName(asm.CodeBase).Equals(Path.GetFileName(AssemblyFile),
StringComparison.OrdinalIgnoreCase))
{
message = string.Format("Found assembly {0} in current domain",
asm.CodeBase);
MSBuildHelper.Log(this, message, MessageImportance.High);
assembly = asm;
break;
}
else
{
searched.Add(Path.GetFileName(asm.CodeBase));
}
}
if (assembly == null)
{
message = string.Format(
"Unable to find {0} after looking in current domain assemblies {1}",
Path.GetFileName(AssemblyFile), string.Join(", ", searched.ToArray()));
MSBuildHelper.Log(this, message, MessageImportance.High);
}
}
毫无疑问,有问题的程序集不在当前域中(这可能有意义,因为产生了另一个MSBuild进程进行编译),所以假设错误消息为真,我该如何处理弄清楚它住在哪里?这很令人困惑,因为给我的错误信息表明它应该是CurrentDomain。
或者具有更多WPF经验的人可以解释为什么这个程序集在成功构建后仍然在app域中浮动?
这是来自遇到此异常的其他人的another question。
答案 0 :(得分:1)
我的解决方案是开源:)使用Cecil从Attribute
获取AssemblyFile
:
bool found = false;
string value = string.Empty;
Type attributeType = Type.GetType(Attribute);
AssemblyDefinition assembly = AssemblyFactory.GetAssembly(AssemblyFile);
foreach (CustomAttribute attribute in assembly.CustomAttributes)
{
if (attribute.Constructor.DeclaringType.Name == attributeType.Name)
{
value = attribute.ConstructorParameters[0].ToString();
found = true;
}
}
Jays评论更新:
通常我使用:AssemblyFileVersion
作为Attribute属性值,并让setter逻辑填充缺少的部分:)
以下是属性的定义方式:
string attribute;
[Required]
public string Attribute
{
get { return attribute; }
set
{
string tempValue = value;
if (!tempValue.StartsWith("System.Reflection."))
{
tempValue = "System.Reflection." + tempValue;
}
if (!value.EndsWith("Attribute"))
{
tempValue += "Attribute";
}
attribute = tempValue;
}
}
显示属性属性值的单元测试sans required prefix或suffix:
[Test]
public void Execute_WithoutSystemReflectionPrefixOrAttributeSuffix_ReturnsExpectedResult()
{
string version = getAssemblyFileVersion();
Assert.IsNotNull(version, "Expected AssemblyFileVersionAttribute to contain value");
task.AssemblyFile = assemblyFile;
task.Attribute = "AssemblyFileVersion";
task.Value = "Bogus";
result = task.Execute();
Assert.IsTrue(result, "Expected execute to pass on valid assembly and attribute name");
Assert.AreEqual(task.Value, version,
"Expected task value to match assembly file version attribute value");
}