【发布时间】:2021-03-02 16:21:33
【问题描述】:
我正在开发一个简单的 WPF 应用程序,它使用两个外部 DLL,Nancy.dll 和 Nancy.Hosting.Self.dll 通过 http 发送一些数据。我想保持 .exe 文件独立,所以我试图将两个 .dll 文件合并到应用程序中。我尝试了多种构建后合并方法,例如NetZ 和ILMerge,但似乎都存在wpf 应用程序的问题并且没有输出工作可执行文件。
this post 有多个解决此问题的建议,尽管它们都归结为相同的两件事:
- 使用构建后合并。这在我的情况下效果不佳。
- 将 DLL 作为嵌入式资源放入应用程序中,并在必要时利用
AppDomain.CurrentDomain.AssemblyResolve事件加载它。
第二个选项似乎很有希望:事件被触发,它找到嵌入的资源,从中生成数据流并将其作为程序集加载。我可以通过多种方式验证程序集是否加载:
Visual Studio 的Debug -> Windows -> Modules 显示加载的程序集,AppDomain.CurrentDomain.GetAssemblies() 还显示正在加载的程序集。
但是,在使用程序集时(在这种情况下调用Nancy.Hosting.Self.NancyHost host = new NancyHost();)我仍然收到以下错误:
Could not load file or assembly 'Nancy, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
我确实确保所有依赖项都已加载(如建议的here)并且所有加载的程序集都在同一个 AppDomain 中。另请注意,上述错误仅针对Nancy.dll,AssemblyResolve 事件确实可以正确加载Nancy.Hosting.Self.dll。
我真的看不出我还有什么可能做错了,我加载程序集是否有问题,或者是否特别是 Nancy 行为异常(我发现 this issue on GitHub 我不确定它是否有关的)。如果您对加载程序集、合并 dll 或 Nancy 的替代方案有任何建议,我会很高兴听到。
附:
我尝试从Nancy.dll 调用不同的方法,令我惊讶的是我能够做到; AssemblyResolve 方法确实有效。
不过,初始化 Nancy.Hosting.Self.NancyHost 仍然存在问题,堆栈跟踪提示了原因:
at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName)
at Nancy.AppDomainAssemblyCatalog.CreateRemoteReferenceProber(AppDomain appDomain)
at Nancy.AppDomainAssemblyCatalog.LoadNancyReferencingAssemblies(IEnumerable`1 loadedAssemblies)
at Nancy.AppDomainAssemblyCatalog.GetAvailableAssemblies()
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at Nancy.DefaultTypeCatalog.GetTypesAssignableTo(Type type)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Nancy.DefaultTypeCatalog.GetTypesAssignableTo(Type type, TypeResolveStrategy strategy)
at Nancy.Bootstrapper.NancyBootstrapperLocator.GetBootstrapperType(ITypeCatalog typeCatalog)
at Nancy.Bootstrapper.NancyBootstrapperLocator.LocateBootstrapper()
at Nancy.Bootstrapper.NancyBootstrapperLocator.get_Bootstrapper()
因此,显然,当 Nancy 初始化其引导程序时,它会尝试获取其引用程序集并为此使用方法 GetAssemblyDirectories。自然地,DLL 不存在(因为我试图将它们合并到 .exe 中)并且引导程序无法初始化。
由于我无法解决此问题,并且 Nancy 不再被维护,我想重申我的问题:
有人知道用于 c# 的轻量级 Web 框架吗?
【问题讨论】:
标签: c# wpf dll assemblies nancy