SideBySide error with bind forwarding in .net Windows Service
I have a Windows.net service that needs to download two different versions of an assembly. It runs on server 2012 R2. I am using anchor redirection in a separate file .config
that is loaded into the main app.config file using this before closing the </configuration>
node ( see MSDN doc ):
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<linkedConfiguration href="file://E:\my-service\runtime.config" />
</assemblyBinding>
My runtime.config contains the actual binding redirect:
<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>
This works fine until the server reboots. We then see this in the event log after starting the machine:
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.
So, I ran the tool sxstrace.exe
(according to this blog ) and get this output:
=================
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.
If I remove linkedConfiguration
from the main app.config the service starts (albeit with different errors). Then I can add it back and the service can be started and stopped until the machine is rebooted.
Does anyone know why this is happening? And is this use correct linkedConfiguration
?
UPDATE
After a long and productive conversation with Microsoft support, I found that use linkedConfiguration
with inline manifest is not supported ( see MSDN doc ).
However, there is an error loading the configuration from an external file. I raised this as a connection error .
source to share
After an update from Microsoft Support and a suggestion from @cmckeegan, we moved away from the external config file for our binding redirection and moved it into code instead. Now we are calling BindingRedirects.Register()
into our composite root .
The class BindingRedirects
looks like this:
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;
};
}
}
source to share