Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ';无法加载程序集';,但程序集已加载_C#_Wpf_Dll_Assemblies_Nancy - Fatal编程技术网

C# ';无法加载程序集';,但程序集已加载

C# ';无法加载程序集';,但程序集已加载,c#,wpf,dll,assemblies,nancy,C#,Wpf,Dll,Assemblies,Nancy,我正在开发一个简单的WPF应用程序,它使用两个外部dll,Nancy.dll和Nancy.Hosting.Self.dll通过http发送一些数据。我想保持.exe文件独立,因此我尝试将两个.dll文件合并到应用程序中。我尝试了多种编译后合并方法,如NetZ和ILMerge,但这两种方法似乎都存在wpf应用程序问题,并且没有输出可执行文件 对于解决这个问题,有多种建议,尽管它们都归结为相同的两件事: 使用构建后合并。这对我来说不太管用 将DLL作为嵌入式资源放入应用程序中,并在必要时使用AppD

我正在开发一个简单的WPF应用程序,它使用两个外部dll,
Nancy.dll
Nancy.Hosting.Self.dll
通过http发送一些数据。我想保持.exe文件独立,因此我尝试将两个.dll文件合并到应用程序中。我尝试了多种编译后合并方法,如
NetZ
ILMerge
,但这两种方法似乎都存在wpf应用程序问题,并且没有输出可执行文件

对于解决这个问题,有多种建议,尽管它们都归结为相同的两件事:

  • 使用构建后合并。这对我来说不太管用
  • 将DLL作为嵌入式资源放入应用程序中,并在必要时使用
    AppDomain.CurrentDomain.AssemblyResolve
    事件加载它
  • 第二个选项似乎很有希望:事件被触发,它找到嵌入的资源,从中生成数据流并将其作为程序集加载。我可以通过多种方式验证程序集是否已加载:
    Visual Studio的
    Debug->Windows->Modules
    显示加载的程序集,并且
    AppDomain.CurrentDomain.GetAssemblys()
    还显示正在加载的程序集

    但是,在使用程序集时(在本例中调用
    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.
    
    我确实确保也加载了所有依赖项(如建议的),并且所有加载的程序集都位于同一AppDomain中。还请注意,上述错误仅适用于
    Nancy.dll
    AssemblyResolve
    事件确实能够正确加载
    Nancy.Hosting.Self.dll

    我真的不知道我还可能做错了什么,也不知道我在加载程序集时是否出了问题,或者是否是Nancy的行为异常(我发现了Nancy的行为,我不确定这是否相关)。如果您对加载程序集、合并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在初始化其引导程序时,会尝试获取其引用程序集,并为此使用该方法。当然,DLL不存在(因为我正试图将它们合并到.exe中),引导程序无法初始化

    由于我无法解决这个问题,而且Nancy也不再得到维护,我想重申我的问题:

    有人知道一个很好的c#轻量级web框架吗?

    我真的不想去寻找其他东西,因为Nancy对我来说工作很好。我最终做的是将dll写入我知道nancy将要查找的位置,初始化引导程序,然后再次删除数据:

    bool NancyPresent = File.Exists("Nancy.dll");
    if (!NancyPresent) {
        var assembly = Assembly.GetExecutingAssembly();
        using (Stream stream = assembly.GetManifestResourceStream("Resources.Nancy.dll"))
        using (MemoryStream MS = new MemoryStream()) {
            stream.CopyTo(MS);
            File.WriteAllBytes("Nancy.dll", MS.ToArray());
        }
        Nancy.Bootstrapper.INancyBootstrapper bootstrapper = Nancy.Bootstrapper.NancyBootstrapperLocator.Bootstrapper;
        bootstrapper.Initialise();
        File.Delete("Nancy.dll");
    }
    
    附带的信息是dll加载良好,尽管Nancy的错误表明情况并非如此。即使是开源的,你也永远不知道第三方软件包到底要做什么