Sicos1977 / MsgKit

A .NET library to make MSG files without the need for Outlook
199 stars 55 forks source link

old Version of System.Runtime.Compilerservices.Unsafe.dll #105

Closed Baphomet2710 closed 1 year ago

Baphomet2710 commented 1 year ago

I have a project in vb.NET with .NET Framework 4.8. The version of System.Runtime.Compilerservices.Unsafe.dll originally included is 6.0.21.52210. After including the NuGet-package of MsgKit the System.Runtime.Compilerservices.Unsafe.dll is changed to 4.6.28619.1. While build several warnings are displayed refering to System.Runtime.Compilerservices.Unsafe. My application can't be started.

I tried to install the NuGet-package for System.Runtime.Compilerservices.Unsafe and got the actual version 6.0.21.52210 and my application is starting. When I now try to use methods from the MsgKit (e.g. MsgKit.Converter.ConvertEmlToMsg) I got errors: 'MsgKit' was not declared. Due to the protection level, access may not be possible.

Sicos1977 commented 1 year ago

Just use a binding redirect, that way you just can point it to whatever version you want to use.

https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/bindingredirect-element

Baphomet2710 commented 1 year ago

Thanks for the quick response.

I have to admit that I can't do anything further with this advice. Which DLL must be redirected to which version?

My application has to use System.Runtime.Compilerservices.Unsafe.dll in version 6.0.21.52210 or newer. The MsgKit.dll has to use System.Runtime.Compilerservices.Unsafe.dll in version 4.6.28619.1.

Am I right that in the configuration file of my appliaction (app.config) I should create an bindingredirect-entry? But for which dll? The System.Runtime.Compilerservices.Unsafe.dll? That makes no sense, if a new version is available, then it would have to be adjusted every time :-(

Sicos1977 commented 1 year ago

Thanks for the quick response.

I have to admit that I can't do anything further with this advice. Which DLL must be redirected to which version?

My application has to use System.Runtime.Compilerservices.Unsafe.dll in version 6.0.21.52210 or newer. The MsgKit.dll has to use System.Runtime.Compilerservices.Unsafe.dll in version 4.6.28619.1.

Am I right that in the configuration file of my appliaction (app.config) I should create an bindingredirect-entry? But for which dll? The System.Runtime.Compilerservices.Unsafe.dll? That makes no sense, if a new version is available, then it would have to be adjusted every time :-(

Sorry but that is how it works in .net land ... you can put the binding redirect in your app.config

      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>

You can also connect to the OnAssemblyResolveevent in the app domain and do the redirecting from code.... but in some cases this doesn't work.

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainOnAssemblyResolve;

#region CurrentDomainOnAssemblyResolve
    private Assembly CurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args)
    {
        var assemblyName = new AssemblyName(args.Name);
        var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
        path = path!.Substring(6);
        var assemblyFullName = Path.Combine(path, $"{assemblyName.Name}.dll");

        if (!File.Exists(assemblyFullName)) return null;
        var assembly = Assembly.LoadFile(assemblyFullName);
        _systemLogs.Insert(SettingsServiceName, $"Assembly '{assemblyName.FullName}' could not be resolved automatically, loaded '{assembly.FullName}' from {assemblyFullName}");
        return assembly;
    }
    #endregion