尝试将Autofac加载为嵌入式程序集时出现FileNotFoundException

时间:2013-09-13 19:37:48

标签: c# dll embedded-resource

以前版本的Autofac有效,但由于他们转而将其变为可移植类库,因此无法加载。

我尝试应用列出的here修补程序(KB2468871),但它告诉我不需要它。

当我将Autofac.dll文件移动到与可执行文件相同的位置时,错误消失了。当它从外部DLL加载它时,它加载正常。

为什么它不能作为嵌入式DLL工作?

以下是例外:

        System.IO.FileNotFoundException: Could not load file or assembly 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes' or one of its dependencies. The system cannot find the file specified.

        Stack trace: 
           at Autofac.Core.Registration.ComponentRegistration..ctor(Guid id, IInstanceActivator activator, IComponentLifetime lifetime, InstanceSharing sharing, InstanceOwnership ownership, IEnumerable`1 services, IDictionary`2 metadata)
           at Autofac.Core.Container..ctor()
           at Autofac.ContainerBuilder.Build(ContainerBuildOptions options)
           at MyApp.Configuration.Bootstrapper.Run(String[] args) in c:\Dev\MyApp\App\Configuration\Bootstrapper.cs:line 25
           at MyApp.Configuration.EntryPoint.Main(String[] args) in c:\Dev\MyApp\App\Configuration\EntryPoint.cs:line 22

如果有帮助,这里是.csproj文件的一部分,它将DLL嵌入到可执行文件中:

  <Target Name="AfterResolveReferences">
<ItemGroup>
  <EmbeddedResource Include="@(ReferenceCopyLocalPaths)" Condition="'%(ReferenceCopyLocalPaths.Extension)' == '.dll'">
    <LogicalName>%(ReferenceCopyLocalPaths.DestinationSubDirectory)%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)</LogicalName>
  </EmbeddedResource>
</ItemGroup>

...这里是EntryPoint类:

internal static class EntryPoint
{
    [STAThread]
    private static void Main(params string[] args)
    {
        AppDomain.CurrentDomain.AssemblyResolve += (sender, e) => loadEmbeddedAssembly(e.Name);
        Bootstrapper.Run(args); // must call separate class when using embedded assemblies
    }

    private static Assembly loadEmbeddedAssembly(string name)
    {
        var container = Assembly.GetExecutingAssembly();
        var path = new AssemblyName(name).Name + ".dll";

        using (var stream = container.GetManifestResourceStream(path))
        {
            if (stream == null)
            {
                return null;
            }

            var bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            return Assembly.Load(bytes);
        }
    }
}

2 个答案:

答案 0 :(得分:10)

我可以通过在针对Silverlight并使用System.Core类型的PCL库上使用Assembly.Load(byte [])轻松地重现您的问题,此问题并非特定于Autofac。加载程序集是一种相当邪恶的方式,与Assembly.LoadFile()一样糟糕。没有加载上下文是DLL Hell的配方。

CLR对这些PCL库引用有一个不值得羡慕的工作,它需要将可重定向的程序集引用神奇地映射到实际的引用。换句话说,2.0.5.0引用需要映射到运行时版本的正确版本,在您的情况下为4.0.0.0。只有在知道实际的运行时版本是什么时才能做到这一点,没有加载上下文会使这很困难。显然它不会尝试这样做,它会再次为2.0.5.0引用触发AssemblyResolve事件。

这是解决方案,后视非常简单。只是拦截对retargetable程序集引用的解析请求,并使用Assembly.Load()让CLR从AppDomain的加载上下文中对其进行排序。像这样更改AssemblyResolve事件处理程序:

private static Assembly loadEmbeddedAssembly(string name)
{
    if (name.EndsWith("Retargetable=Yes")) {
        return Assembly.Load(new AssemblyName(name));
    }
    // Rest of your code
    //...
}

这在我的测试应用程序中运行良好,我相信它也可以解决您使用Autofac的问题。

答案 1 :(得分:0)

问题可能是您尝试加载EmbeddedAssembly。

我对你如何做到这一点知之甚少 但我知道代码项目中的以下示例对我来说很好,所以它可以帮助你: - )

Load DLL From Embedded Resource

(如果我误解了你的问题,请告诉我,我会删除我的答案)