Autofac异常:无法从程序集中加载类型 [英] Autofac Exception: Could not load type from assembly

查看:270
本文介绍了Autofac异常:无法从程序集中加载类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在构建Autofac容器时,在注册接口实现时,Autofac会引发异常:

While building an Autofac Container, upon registering an interface implementation, Autofac would throw an exception:

protected override void Load(ContainerBuilder builder)
{
    var assembly = typeof(MyModule).Assembly;

    builder.RegisterAssemblyTypes(assembly)
        .Where(x => x.Name.EndsWith("Procedure")
        .AsImplementedInterfaces();

    base.Load(builder);
}

错误:

Could not load type from assembly

尝试的解决方案:

  • 清理构建/重建
  • 删除bin/obj目录
  • 更改分配装配的方式
  • 更改注册方式
  • 重新启动Visual Studio
  • Attempted Solutions:

    • Clean build / Rebuild
    • Deleting bin/obj directories
    • Changing manner in which I was assigning the assembly
    • Changing manner in which I was performing registrations
    • Restarting Visual Studio
    • 推荐答案

      解决方案

      在搜索Google,StackOverflow,Autofac文档以及我的灵魂之后,我能找到的最接近我问题的是此答案有关全局程序集缓存(GAC)的信息发布到此问题用于测试目的的容器.

      Solution

      After searching Google, StackOverflow, Autofac documentation, and my soul, the closest I could find to my issue was this answer concerning the Global Assembly Cache (GAC) posted to this question concerning a container being build for testing purposes.

      这使我想到了错误的程序集.在我们的环境中,我们经常将Nuget软件包用于我们的核心接口和基础结构.我检查了自己是否在Service层中使用了最新版本的软件包,因此在被挖掘得更深之前,我仍然感到困惑.

      This lead me to think wrong assemblies. In our environment, we use Nuget packages frequently for our core interfaces and infrastructure. I checked that I was using the latest version of the package in my Service layer, so I was still stumped until I dug just a bit further.

      最后,问题归结于Nuget软件包.我的解决方案的表示层(通过Autofac注册组件)具有包含接口定义的Nuget包的过时版本.通过更新表示层中的Nuget程序包以使其与Service层中的程序包匹配,问题就消失了.

      In the end, the issue came down to the Nuget packages. The presentation layer of my solution, which was registering components via Autofac, had an outdated version of the Nuget package which contained the interface definitions. By updating the Nuget package in the presentation layer to match that of the Service layer, the problem disappeared.

      这篇关于Autofac异常:无法从程序集中加载类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆