gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:48:11 25 4
gpt4 key购买 nike

以前版本的 Autofac 可以工作,但由于他们转而使其成为可移植类库,因此无法加载。

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

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

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

异常(exception)情况:

        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

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

  <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);
}
}
}

最佳答案

我可以通过在以 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 事件。

这是解决方案,事后看来非常简单。只需拦截可重定位程序集引用的解析请求,并使用 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 时遇到的问题。

关于c# - 尝试将 Autofac 作为嵌入式程序集加载时出现 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18793959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com