之前的Autofac版本可以使用,但是自从它转换成可移植类库后,它就无法加载。
我尝试了应用这里(KB2468871)列出的修复方法,但它告诉我不需要。
当我将Autofac.dll文件移到可执行文件相同的位置时,错误消失了。当它从外部DLL中加载时,它可以正常加载。
为什么它作为嵌入式DLL无法工作?
这里是异常信息:
如果需要帮助,这是.csproj文件中将DLL嵌入可执行文件的部分:
我尝试了应用这里(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);
}
}
}
LoadFrom
,这似乎对提问者有用。 - default.kramer