问题
Issue
While building an Autofac Container, upon registering an interface implementation, Autofac would throw an exception:
Registration:
protected override void Load(ContainerBuilder builder)
{
var assembly = typeof(MyModule).Assembly;
builder.RegisterAssemblyTypes(assembly)
.Where(x => x.Name.EndsWith("Procedure")
.AsImplementedInterfaces();
base.Load(builder);
}
Error:
Could not load type from assembly
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
回答1:
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.
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.
The Culprit
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.
来源:https://stackoverflow.com/questions/37734742/autofac-exception-could-not-load-type-from-assembly