Including Autofac.Module in custom module breaks O

2019-05-30 23:17发布

问题:

See here & here for why I'm trying to do this.

I have a custom module that I'm building (and which is it's own .csproj in the Orchard .sln, as normal)

I have a (relatively complex) dependency that I wish to inject into Orchard. Currently if I build and run the code it's perfectly happy until it hits my dependency at which point it null-refs (as I'd expect it to). I can access the admin site and any pages that don't reference the dependency.

I add a reference to Autofac to my project. -> All fine.

I add the following class:

using System;
using Autofac;

namespace MyCustom.Module.Namespace
{
  public class LoaderModule
  {}
}

Everything is still fine - I rebuild and still get the expected behaviour. Then I make my LoaderModule derive from Autofac.Module as instructed elsewhere:

using System;
using Autofac;

namespace MyCustom.Module.Namespace
{
  public class LoaderModule : Autofac.Module
  {}
}

And suddenly I'm getting "The resource cannot be found." 404s from IIS for the entire Orchard site - including admin site and content pages which done't hit the dependency.

Help!?

回答1:

Bah! It was a versioning issue.

Starting the site with debugger attached, and breaking on all exceptions revealed things like this:

{"Could not load file or assembly 'Autofac, Version=3.3.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)":"Autofac, Version=3.3.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da"}

I'd added Autofac from nuget, but Orchard itself was using a bundled version that was different. Removing my nuget reference to Autofac and re-adding by locating the bundled copy of Autofac.dll made the problem go away.