Could not load file or assembly 'System.Text.Json

Could not load file or assembly 'System.Text.Json

francois.lozes
Contributor Contributor
7,931 Views
3 Replies
Message 1 of 4

Could not load file or assembly 'System.Text.Json

francois.lozes
Contributor
Contributor

Anyone came across an error of this kind (and has an idea on how to fix it):

 

    Could not load file or assembly 'System.Text.Json, Version=5.0.0.0, Culture=neutral,   PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.

 

It seems like Revit is trying to look for a version of System.Text.Json that is different from the one in the DLL I'm distributing with my adding (v5.0.2).

This kind of error appeared after updating my socketio-client to v3 which has by default System.Text.Json as a dependency instead of Newtonsoft.Json.

0 Likes
Accepted solutions (2)
7,932 Views
3 Replies
Replies (3)
Message 2 of 4

jeremy_tammik
Alumni
Alumni
Accepted solution

Check whether Revit itself is already loading the version you need, or, God forbid, a different one.

 

You can control which version you load yourself and where from by implementing an appropriate assembly resolver:

 

https://thebuildingcoder.typepad.com/blog/2014/05/rvtva3c-assembly-resolver.html

 

More examples of using this technique:

 

 

Good luck, and beware of DLL hell. Here are some other discussions on resolving DLL conflicts:

 

 

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 4

francois.lozes
Contributor
Contributor
Accepted solution

Thanks a lot Jeremy, just for potential future visitors I solved my issue with:

 

Assembly OnResolveAssembly(object sender, ResolveEventArgs args)
{
  string directoryDLLs = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

 

  // Fix for "Could not load file or assembly 'System.Text.Json" (maybe conflict with Revit)
  if (args.Name.Contains("System.Text.Json"))
  {
    string pathJson = Path.Combine(directoryDLLs, "System.Text.Json.dll");
    return Assembly.LoadFrom(pathJson);
  }

 

  // other assemblies

  string pathAssembly = Path.Combine(directoryDLLs, args.Name);
  if (File.Exists(pathAssembly))
  {
    return Assembly.LoadFrom(pathAssembly);
  }

 

  // assembly cannot be resolved
  return null;
  }

 

public Result OnStartup(UIControlledApplication application)
{

  // attach load assembly event handler
  AppDomain currentDomain = AppDomain.CurrentDomain;
  currentDomain.AssemblyResolve += OnResolveAssembly;

Message 4 of 4

Mr.CSurvey
Contributor
Contributor

I'm having this same issue but I don't know anything about adding code to where its needed. Can you explain how I would go about adding the code?