user3308043
user3308043

Reputation: 827

Assembly Reference Catch-22

I'm trying to integrate MangoChat into an existing .Net project. Mango requires the use of Newtonsoft.Json version 3.5.0.0, however my current version of this assembly is 6.x.

Logically, I thought to uninstall the current version but it has so many dependencies that it tears apart the project. I can't install version 3.5.0.0 beside 6.x because I can't add a second assembly to the .bin folder with the same name.

How could I solve this issue?

Upvotes: 2

Views: 48

Answers (1)

dotnetom
dotnetom

Reputation: 24901

If version 6.x is compatible with version 3.5.0.0, you can add a binding redirect to new version. You should add it to your configuration file:

<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="[enter token here]" culture="neutral" />
                <bindingRedirect oldVersion="3.5.0.0-6.X" newVersion="6.X"/>
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

Replace 6.X with your actual version.

Another option is to add assembly to different folder and use AssemblyResolve event on AppDomain to find it. You can use such code:

//Load assembly from alternative location
Assembly newtonsoftAssembly = Assembly.LoadFrom(@"C:\PathToYourAssembly.dll");
//Handle AssemblyResolve event
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
    //Check if your assembly was requested
    if (args.Name.Contains("Newtonsoft.Json"))
    {
        return newtonsoftAssembly;
    }
    return null;
};

You should run this code once, e.g. during application startup.

Upvotes: 3

Related Questions