Search code examples
.netassembly-resolution

Redirect referenced assembly to a different version in C# console application


I'm trying to use the log4net RabbitMQAppender in a C# console application, and it seems like this requires RabbitMQ.Client 2.6.1.0 to be referenced. I'm currently using the latest version, 2.7.1.0, which is causing the appender creation to fail with the following error:

Could not create Appender [AmqpAppender] of type [log4net.RabbitMQ.RabbitMQAppender, log4net.RabbitMQ]. Reported error follows. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileNotFoundException: Could not load file or assembly 'RabbitMQ.Client, Version=2.6.1.0, Culture=neutral, PublicKeyToken=89e7d7c5feba84ce' or one of its dependencies. The system cannot find the file specified.

Replacing the RabbitMQ.Client with the previous version fixes the issue, though I'd like to get this working with the latest version.

According to another question here on SO, the following config needs to be added to the app settings, which I've added:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="RabbitMQ.Client" />
            <bindingRedirect oldVersion="2.6.1.0" newVersion="2.7.1.0" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>

However, this doesn't seem to make any difference, and the error still occurs with the appender throwing an error about being unable to load the RabbitMQ.Client file.

Is there anything else that needs to be done to achieve this, or something that could be stopping this assembly redirection from working?


Solution

  • It looks like I may have had a configuration mismatch somewhere. Setting the RabbitMQ.Client.dll file to copy local and adding the following to my app config resolved the issue:

    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="RabbitMQ.Client" publicKeyToken="89e7d7c5feba84ce" culture="neutral" />
                <bindingRedirect oldVersion="2.6.1.0" newVersion="2.7.1.0"/>
            </dependentAssembly>
        </assemblyBinding>
    </runtime>