我有一个.net Windows服务,需要加载两个不同版本的程序集。它运行在服务器2012 R2盒子上。我在单独的.config
文件中使用绑定重定向,在关闭</configuration>
节点(see MSDN doc)之前使用此文件将其加载到主app.config中:
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<linkedConfiguration href="file://E:\my-service\runtime.config" />
</assemblyBinding>
我的runtime.config包含实际的绑定重定向:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="AutoMapper" publicKeyToken="be96cd2c38ef1005" culture="neutral" />
<codeBase version="0.4.0.126" href="AutoMapper.dll" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="AutoMapper" publicKeyToken="be96cd2c38ef1005" culture="neutral" />
<codeBase version="3.2.1.0" href="AutoMapper.3.2.1\AutoMapper.dll" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
这可以正常工作,直到重新启动服务器。然后我们在机器启动后在事件日志中看到这一点:
Activation context generation failed for "Service.exe".
Error in manifest or policy file "Service.exe.Config" on line 71.
The element assemblyBinding appears as a child of element configuration which is not supported by this version of Windows.
所以我运行了sxstrace.exe
工具(根据此blog)并获得此输出:
=================
Begin Activation Context Generation.
Input Parameter:
Flags = 0
ProcessorArchitecture = AMD64
CultureFallBacks = en-US;en
ManifestPath = E:\my-service\Service.exe
AssemblyDirectory = E:\my-service\
Application Config File = E:\my-service\Service.exe.Config
-----------------
INFO: Parsing Application Config File E:\my-service\Service.exe.Config.
ERROR: Line 71: The element assemblyBinding appears as a child of element configuration which is not supported by this version of Windows.
ERROR: Activation Context generation failed.
End Activation Context Generation.
如果我从主app.config中删除linkedConfiguration
,则服务启动(尽管有不同的错误)。然后我可以将其重新添加,服务可以启动并停止,直到机器再次重新启动。
有谁知道为什么会这样?这是正确使用linkedConfiguration
吗?
更新
与Microsoft支持人员进行了长时间且富有成效的对话后,我发现不支持使用带有嵌入式清单的linkedConfiguration
(see MSDN doc)。
但是,从外部文件加载配置似乎存在错误。我把它作为bug on connect提出来了。
答案 0 :(得分:1)
在Microsoft支持的更新和@cmckeegan的建议之后,我们从外部配置文件转移到绑定重定向并将其移动到代码中。我们现在在composition root。{/ p>中致电BindingRedirects.Register()
BindingRedirects
类看起来像这样:
public static class BindingRedirects
{
static readonly Dictionary<string, string> Redirects = new Dictionary<string, string>
{
{ "AutoMapper, Version=3.2.1.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005", @"AutoMapper.3.2.1\AutoMapper.dll"}
};
public static void Register()
{
AppDomain.CurrentDomain.AssemblyResolve += (o, args) =>
{
if (Redirects.ContainsKey(args.Name))
{
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Redirects[args.Name]);
return Assembly.LoadFrom(path);
}
return null;
};
}
}