I have a custom MSBuild task that peeks into an assembly to get some attribute metadata.
Assembly assembly = Assembly.ReflectionOnlyLoadFrom(AssemblyFile)
This is used by our automated build / release process and works great against builds used and referencing class libraries, console applications, and web projects. The MSBuild task is called after another MSBuild process compiles the projects.
It stopped working yesterday when I added a WPF project that referenced this particular assembly - the .NET 3.5 class library.
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)
I know this is related to WPF because an exception does not occur if I modify the AssemblyFile to point to a different assembly in the same solution that the WPF project is not referring to.
,
... 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 , ?
-, .