Configuração do .NET AssemblyBinding Ignored in Machine.config
-
10-07-2019 - |
Pergunta
Tenho uma situação em que preciso ser capaz de carregar montagens no GAC com base em seus nomes parciais. Para fazer isso, adicionei o seguinte ao meu arquivo app.config:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<qualifyAssembly partialName="MyAssembly"
fullName= "MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0123456789abcdef"/>
</assemblyBinding>
</runtime>
Isso funciona exatamente da maneira que eu quero. No entanto, se eu colocar o mesmo elemento no meu arquivo Machine.Config, ele parece ser ignorado e recebo fileNotFoundExceptions ao tentar carregar o MyAssembly.
A seguir, o registro de ligação à montagem quando o elemento está no meu app.config, e o bind é bem -sucedido:
LOG: This bind starts in default load context. LOG: Using application configuration file: C:\Documents and Settings\jon_scheiding\My Documents\Source\Testing\Test Projects 1\Cmd\bin\Debug\Testers.Cmd.vshost.exe.config LOG: Using machine configuration file from C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config. LOG: Partial reference qualified from config file. New reference: MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0123456789abcdef. LOG: Post-policy reference: MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0123456789abcdef LOG: Found assembly by looking in the GAC. LOG: Binding succeeds. Returns assembly from C:\WINDOWS\assembly\GAC_MSIL\MyAssembly\1.0.0.0__b20f4683c1030dbd\MyAssembly.dll. LOG: Assembly is loaded in default load context.
Contraste isso com o log quando minha configuração está no Machine.config, e o Bind falha:
LOG: This bind starts in default load context. LOG: Using application configuration file: C:\Documents and Settings\jon_scheiding\My Documents\Source\Testing\Test Projects 1\Cmd\bin\Debug\Testers.Cmd.vshost.exe.config LOG: Using machine configuration file from C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config. LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind). LOG: Attempting download of new URL file:///C:/Documents and Settings/jon_scheiding/My Documents/Source/Testing/Test Projects 1/Cmd/bin/Debug/MyAssembly.DLL. LOG: Attempting download of new URL file:///C:/Documents and Settings/jon_scheiding/My Documents/Source/Testing/Test Projects 1/Cmd/bin/Debug/MyAssembly/MyAssembly.DLL. LOG: Attempting download of new URL file:///C:/Documents and Settings/jon_scheiding/My Documents/Source/Testing/Test Projects 1/Cmd/bin/Debug/MyAssembly.EXE. LOG: Attempting download of new URL file:///C:/Documents and Settings/jon_scheiding/My Documents/Source/Testing/Test Projects 1/Cmd/bin/Debug/MyAssembly/MyAssembly.EXE. LOG: All probing URLs attempted and failed.
O problema parece ser a quarta linha, "Política não está sendo aplicada para referência neste momento". No entanto, posso encontrar muito pouca documentação sobre o que essa mensagem significa ou como resolvê -la.
Como posso obter a estrutura para reconhecer meuu003Cruntime> elemento?
Desde já, obrigado!
Solução
A estrutura não lê a configuração qualifySseBMly do MACHING.CONFIG, ele apenas o lê do arquivo de configuração do aplicativo.
A estrutura reconhece o seu elemento Runtim, no entanto, não reconhece o elemento QualifyAssEmbly.
Outras dicas
Voltei a essa necessidade muito mais tarde, pois temos algumas assembléias no GAC e coisas como Nibernate precisavam resolvê -las. Em vez de adicionar todos esses elementos qualificados a cada app.config, o código a seguir nos permite adicioná -los ao nosso máquina.config.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.XPath;
namespace MyNamespace {
/// <summary>
/// Implements <see cref="IDisposable"/> to provide a scope for resolving
/// assemblies described in the machine.config file with
/// <qualifyAssembly> elements.
/// </summary>
/// <remarks>
/// Because the framework only respects <qualifyAssembly> at the
/// application configuration level, this class provides similar
/// functionality for this element at the machine configuration level.
/// You can wrap a new instance of this class in a <b>using</b> statement
/// to get resolution within a specific scope; or, you can call the
/// <see cref="AssemblyResolver.Attach()"/> method to get
/// resolution for the lifetime of the current <see cref="AppDomain"/>.
/// </remarks>
public sealed class AssemblyResolver : IDisposable {
#region Private fields and implementation
private static Dictionary<string, string> _qualifiedNames;
private static Assembly ResolveAssembly(object sender, ResolveEventArgs args) {
if(_qualifiedNames == null) {
//
// Lazily initialize short/long name mappings.
//
_qualifiedNames = BuildQualifiedNameList();
}
if(!_qualifiedNames.ContainsKey(args.Name)) {
return null;
}
try {
return Assembly.Load(_qualifiedNames[args.Name]);
}
catch(FileNotFoundException) {
//
// TODO: Should this exception be propogated?
// It probably should not be hidden from the consumer
// since it likely indicates a configuration error.
//
return null;
}
}
private static Dictionary<string, string> BuildQualifiedNameList() {
var dict = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
//
// Get runtime XML data
//
var xml = GetConfigXml(GetMachineRuntimeSection());
if(xml == null) {
return dict;
}
//
// Iterate the qualifyAssembly elements and register in the list
//
var navigator = xml.CreateNavigator();
foreach(XPathNavigator qualifyAssembly in navigator.Select("runtime/asm:assemblyBinding/asm:qualifyAssembly", CreateNamespaceManager())) {
dict.Add(
qualifyAssembly.GetAttribute("partialName", string.Empty),
qualifyAssembly.GetAttribute("fullName", string.Empty));
}
return dict;
}
private static ConfigurationSection GetMachineRuntimeSection() {
System.Configuration.Configuration machineConfig = ConfigurationManager.OpenMachineConfiguration();
return machineConfig.GetSection("runtime") as ConfigurationSection;
}
private static IXPathNavigable GetConfigXml(ConfigurationSection runtimeSection) {
var ignoreSection = runtimeSection as IgnoreSection;
if(ignoreSection == null) {
return null;
}
//
// Cheat via Reflection to get the XML content of the config
// section.
//
FieldInfo field = typeof(IgnoreSection).GetField("_rawXml", BindingFlags.Instance | BindingFlags.NonPublic);
string rawXml = (string)field.GetValue(ignoreSection);
return new XPathDocument(new StringReader(rawXml));
}
private static IXmlNamespaceResolver CreateNamespaceManager() {
var nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("asm", "urn:schemas-microsoft-com:asm.v1");
return nsmgr;
}
#endregion
/// <summary>
/// Creates a new <see cref="AssemblyResolver"/>.
/// </summary>
public AssemblyResolver() {
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ResolveAssembly);
}
/// <summary>
/// Removes the current <see cref="AssemblyResolver"/>
/// from the <see cref="AppDomain"/>.
/// </summary>
public void Dispose() {
AppDomain.CurrentDomain.AssemblyResolve -= new ResolveEventHandler(ResolveAssembly);
GC.SuppressFinalize(this);
}
/// <summary>
/// Provides qualified assembly resolution for the lifetime of the
/// current <see cref="AppDomain"/>.
/// </summary>
public static void Attach() {
Attach(AppDomain.CurrentDomain);
}
/// <summary>
/// Provides qualified assembly resolution for the lifetime of an
/// <see cref="AppDomain"/>.
/// </summary>
/// <param name="appDomain">
/// The <see cref="AppDomain"/> to service.
/// </param>
public static void Attach(AppDomain appDomain) {
appDomain.AssemblyResolve += new ResolveEventHandler(ResolveAssembly);
}
}
}
Na maioria dos casos, limitamos o escopo, usando isso assim:
using(new AssemblyResolver()) {
//
// NHibernate initialization or whatever
//
}
Também pode ser usado no código de inicialização de aplicativos ou global.asax, e persistir durante toda a vida do aplicativo, assim:
AssemblyResolver.Attach();
//
// or
//
AssemblyResolver.Attach(AppDomain.CurrentDomain);