C# Unity错误:无法加载文件或程序集

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

无法加载文件或程序集'System.Runtime.CompilerServices.Unsafe, Version=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());
        }
    }
}
4个回答

10

Jasen是对的,如果您的NuGet与项目协同工作良好,但我没有那么幸运(当我尝试重新添加包时,我得到了相同的错误)。

修复错误的方法是在您的应用程序/ 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。 - Jasen
1
是的,我不确定我的情况发生了什么事! 我从之前版本的Unity升级,所以我猜在迁移时缺少了他们的打包策略中的某些东西...但我从未发布过NuGet包,这只是一个猜测。 - Brad Lucas

4

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


你有什么见解可以分享一下,可能能够解释这个问题吗?我确实读了布拉德在下面的评论。 - Rod
2
看起来最新的Unity版本(v5.9.3)有一个新的依赖关系,并且依赖包的某个版本存在配置问题。如果您想了解更多信息,可以阅读https://dev59.com/EXVC5IYBdhLWcg3wrDRd和https://dev59.com/42Ei5IYBdhLWcg3wwegN。 - Jasen

1
在我的情况下,我的解决方案中有几个项目。在其中一个项目中使用Nuget安装Unity后,我遇到了该错误,而我已经在另一个项目中安装了Unity。 问题出在CompilerServices.Unsafe库的不同版本上。
我的做法是检查所有已安装Unity的项目的app.config和packages.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程序包”,并在一步中选择要为其安装包的项目来安装包。


0
你可以在你的web.config文件中手动添加依赖项,然后查看它的工作情况。在你的web.config文件中添加以下依赖项:
<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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接