AndySavage
AndySavage

Reputation: 1769

Use custom version of assembly for all dependencies

My project uses a modified version of the Newtonsoft.Json library with some changes so that it will play nice with AOT. The modified dll has been added as reference assembly of the project. My project also uses a set of additional libraries (some via Nuget, some via additional project includes), and some of these libraries also have their own dependency on Newtonsoft.Json.

  1. Which version of Newtonsoft.Json will be included in the final build?
  2. Can I map all versions to use the modified library? Maybe via a BindingRedirect?

Upvotes: 1

Views: 737

Answers (1)

pi-di-tredipi
pi-di-tredipi

Reputation: 63

It should be possible to make external libraries load your dll using AppDomain.CurrentDomain.AssemblyResolve

  1. Make your fork dll have a different name than Newtonsoft.Json
  2. Reference it in your project
  3. In the post build event add a command line to delete Newtonsoft.Json.dll from the target dir
  4. as the first code line of your project (inside Program.cs in case of windows applications and inside Global.asax Application_Start in case of web applications) add this code:
AppDomain.CurrentDomain.AssemblyResolve += 
    (s, a) => {
        if (a.Name.Contains("Newtonsoft.Json"))
          return Assembly.LoadFrom(@"PATH TO YOUR DLL HERE");
        else
          return null;
              };

In case of web application you can get the path to your dll using Server.MapPath

Upvotes: 1

Related Questions