【问题标题】:C# Unity Error: Could not load file or assemblyC# Unity 错误:无法加载文件或程序集
【发布时间】:2019-01-31 21:16:32
【问题描述】:

我在 Unity 上找到了这篇演示文章。看起来很简单,但我收到以下错误:

无法加载文件或程序集 'System.Runtime.CompilerServices.Unsafe,版本=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 或其其中之一 依赖关系。定位程序集的清单定义不 匹配程序集引用。 (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());
        }
    }
}

【问题讨论】:

    标签: c# unity-container


    【解决方案1】:

    如果你的 NuGet 与你的项目配合得很好,Jasen 是正确的,但我没那么幸运(当我尝试重新添加包时,我得到了同样的错误)。

    修复该错误的方法是为您的应用程序/Web 配置中的“缺失”程序集添加一个dependentAssembly 条目(这是 NuGet 安装背后的魔力,应该自动发生)。

    <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> ...

    【讨论】:

    • 有趣。我没有错过 app.config 中的dependentAssembly。
    • 是的,我不确定我的情况发生了什么!我从以前版本的 Unity 升级,所以我假设他们的打包策略在迁移时缺少一些东西......但我从未发布过 NuGet 包,所以这只是一个猜测。
    【解决方案2】:

    当我尝试复制问题时,在我为 System.Runtime.CompilerServices.Unsafe 运行 NuGet 更新后错误消失了。

    【讨论】:

    【解决方案3】:

    就我而言,我的解决方案中有几个项目。我在其中一个项目中安装了带有 Nuget 的 Unity 而我在另一个项目中安装了 Unity 后出现了该错误。 问题来自不同版本的 CompilerServices.Unsafe 库

    我所做的是检查所有安装了 Unity 的项目的 app.config 和 packcages.config 文件(特别是这个)。版本应该对所有人都一样。就我而言:

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

    一个项目使用 4.7.0,另一个使用 4.5 我更新到最新版本并解决了。

    您还可以右键单击解决方案 - 管理 Nuget 包以获取解决方案并安装一个包,只需一步即可选择哪些项目将拥有该包。

    【讨论】:

      猜你喜欢
      • 2010-10-07
      • 2023-03-05
      • 1970-01-01
      • 2013-02-23
      • 2015-09-25
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多