7

I found this demo article on Unity. Looks pretty straightforward but I'm getting the following error:

Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

https://www.tutorialsteacher.com/ioc/register-and-resolve-in-unity-container

using System;
using Unity;

namespace UnityContainerDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var container = new UnityContainer();
            container.RegisterType<ICar, BMW>();// Map ICar with BMW 

            //Resolves dependencies and returns Driver object 
            Driver drv = container.Resolve<Driver>();
            drv.RunCar();
        }
    }

    public interface ICar
    {
        int Run();
    }

    public class BMW : ICar
    {
        private int _miles = 0;

        public int Run()
        {
            return ++_miles;
        }
    }

    public class Ford : ICar
    {
        private int _miles = 0;

        public int Run()
        {
            return ++_miles;
        }
    }

    public class Audi : ICar
    {
        private int _miles = 0;

        public int Run()
        {
            return ++_miles;
        }

    }
    public class Driver
    {
        private ICar _car = null;

        public Driver(ICar car)
        {
            _car = car;
        }

        public void RunCar()
        {
            Console.WriteLine("Running {0} - {1} mile ", _car.GetType().Name, _car.Run());
        }
    }
}
Rod
  • 14,529
  • 31
  • 118
  • 230

4 Answers4

10

Jasen is correct if your NuGet plays nicely with your project, but I wasn't so lucky (when I tried re-adding the package, I got the same error).

What fixes the bug is adding a dependentAssembly entry for the 'missing' assembly inside your app/web config (which is the magic behind the NuGet install, and should happen automatically).

<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.0.4.1" newVersion="4.0.4.1" /> </dependentAssembly> ...

Brad Lucas
  • 175
  • 10
  • Interesting. I was not missing the dependentAssembly in app.config. – Jasen Feb 01 '19 at 01:42
  • 1
    Ya, I'm not sure what happened in my case! I upgraded from a previous version of Unity, so I'm assuming there's something missing from their packaging strategy on-migration... but I've never published a NuGet package, so it's just a guess. – Brad Lucas Feb 01 '19 at 01:53
4

When I attempted to duplicate the problem, the error went away after I ran a NuGet update for System.Runtime.CompilerServices.Unsafe.

Jasen
  • 14,030
  • 3
  • 51
  • 68
  • Do you have any insight you can share that might explain this? I did read Brad's comments below. – Rod Feb 01 '19 at 15:21
  • 2
    It looks like a new dependency with the latest Unity version (v5.9.3) and there is a configuration problem with a version of the dependent package. If you want to know more you can read https://stackoverflow.com/questions/215026/the-located-assemblys-manifest-definition-does-not-match-the-assembly-reference and https://stackoverflow.com/questions/20978519/assemblys-manifest-definition-does-not-match-assembly-reference – Jasen Feb 01 '19 at 18:38
1

In my case I have several projects in my solution. I got that error after I installed Unity with Nuget in one of the projects while I had Unity installed in another project. The problem comes with different versions of the libraries of CompilerServices.Unsafe

What I did is to check app.config and packcages.config files (in special this one) for all the projects with Unity installed. The version should be the same for all. In my case:

<packages>
  <package id="System.Runtime.CompilerServices.Unsafe" version="4.7.0" targetFramework="net48" />
  <package id="Unity" version="5.11.2" targetFramework="net48" />
</packages>

One project was with 4.7.0 and the other with 4.5 I updated to the last version and solved.

You can also right click on Solution - Manage Nuget Packages for solution and install a package selecting what projects will have that package in only one step.

Ramon
  • 94
  • 6
0

You can manually add dependencies in your web.config file, then see it work. Add the below dependency in your web.config file:

<dependentAssembly>
    <assemblyIdentity name="Unity.Abstractions" publicKeyToken="489b6accfaf20ef0" />
    <bindingRedirect oldVersion="0.0.0.0-5.11.6.0" newVersion="5.11.7.0" />
</dependentAssembly>
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • Please review my edits to your answer to see how to answer in the future. Don't use all caps; it is often considered rude, as though you are yelling. More importantly, be sure to format your code for readability; my edit shows you how. – Jeremy Caney Aug 08 '23 at 00:30