vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
454 stars 72 forks source link

FileLoadException in Navisworks plug-in #223

Closed StepanovGM closed 4 months ago

StepanovGM commented 4 months ago

Hello!

I am using SharpGLTF for my navisworks exporter project and I've ran into an error:

When i try to use SharpGLTF inside navisworks plugin i get fatal error and navisworks crashes. dmp file shows, that my plug-in tries to load System.Numeric.Vectors version 4.1.3.0 and fails. I can't understand why it tries to load this version, considering SharpGLTF uses version 4.1.4.0 of this library, which I added to naviswork dependencies folder.

I found similar issue (https://github.com/vpenades/SharpGLTF/issues/4) and tried everything discussed there, but nothing changed.

I am not sure if it's a bug or an error on my side, and would appreciate any help ☺

vpenades commented 4 months ago

I suspect your target framework is Net47x or Net48, right? because these are the only ones giving problems with System.Numerics.Vectors

I don't fully understand the problem, but basically the conflict happens when multiple libraries try to use different versions of the same package.

Theoretically, the highest version should be picked, but some libraries (not sharpGLTF) explicitly require a specific version, preventing other libraries to work correctly.

The only solution I can suggest you is to clone the repository and compile a custom version of the library with the appropiate changes.

Notice that System.Numerics.Vectors is being loaded indirectly via System.Text.Json through System.Memory (why System.Memory depends on Vectors is alien to me)

Possible changes that might fix the project:

StepanovGM commented 4 months ago

Thank you for your answer!

Yes, you are right, target framework is Net4.7

I will try compiling my own version, hope it will work

StepanovGM commented 1 month ago

Hello, for anyone wondering how to fix simmilar issue; The problem was that i changed config of my app, but not of Navisworks. After i added these lines to Navisworks' Roamer.exe.config everything worked.

<dependentAssembly>
            <assemblyIdentity name="System.Numerics.Vectors" 
                              publicKeyToken="b03f5f7f11d50a3a" 
                              culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.1.3.0" 
                             newVersion="4.1.4.0" />
         </dependentAssembly>

Another (i think more preferable) way is to add ResolveEventHandler in code. I've put it in my class constructor.

public MyClass() 
{
    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    var name = new AssemblyName(args.Name);
    if (name.Name == "System.Numerics.Vectors")
    {
        return typeof(System.Numerics.Vector).Assembly;
    }
    else if (name.Name == "System.Runtime.CompilerServices.Unsafe")
    {
        return typeof(System.Runtime.CompilerServices.Unsafe).Assembly;
    }
    //you can add any conflicting assembly the same way
    return null;
}