ILmerge不实现Newtonsoft.json

问题描述 投票:1回答:1

第二天,我把头撞在墙上,无法弄清楚。如何实现此dll,任何错误都会不断跳出来。我尝试了Internet上的许多不同选项。没有任何帮助。帮我弄清楚如何解决问题。如何通过ILMerge在我的项目中实现Newtonsoft.Json?预先谢谢你。

错误:

离开命令“” C:\ Users \ User \ Desktop \ FastFoodDemo \ ILMerge \ merge_all.bat““ C:\ Users \ User \ Desktop \ FastFoodDemo \”“ C:\ Users \ User \ Desktop \ FastFoodDemo \ FastFoodDemo \ bin \ Debug \ RCC.exe”调试”,代码1。[FastFoodDemo

活动总决赛:

“ $(SolutionDir)\ ILMerge \ merge_all.bat”“ $(SolutionDir)”“ $(TargetPath)” $(ConfigurationName)

merge_all.bat

@ECHO OFF

rem #    set .NET version and output folder name
set net="v4, C:\Windows\Microsoft.NET\Framework\v4.0.30319"
set output=Output

rem #    process arguments
set ILMergeSolution=%1ILMerge\ILMerge.exe

rem # determine programm files of x86 for 32 and 64 Platform
IF     EXIST "%PROGRAMFILES(x86)%" set prorgammFiles=%PROGRAMFILES(x86)%
IF NOT EXIST "%PROGRAMFILES(x86)%" set prorgammFiles=%PROGRAMFILES%

rem #	if ILMerge.exe not in the $(SolutionDir)ILMerge\
rem #		then try to use installed in prorgammFiles
IF     EXIST %ILMergeSolution% set ILMerge="%ILMergeSolution%"
IF NOT EXIST %ILMergeSolution% set ILMerge=%prorgammFiles%\Microsoft\ILMerge\ILMerge.exe

set target_path=%2
set target_file=%~nx2
set target_dir=%~dp2
set ConfigurationName=%3

rem #    set output path and result file path
set outdir=%target_dir%%output%
set result=%outdir%\%target_file%

rem #    print info
@echo     Start %ConfigurationName% Merging %target_file%. 
@echo Target: %target_path%
@echo target_dir: %target_dir%
@echo Config: %ConfigurationName% 

rem #    recreate outdir
IF EXIST "%outdir%" rmdir /S /Q "%outdir%"
md "%outdir%"

rem #    run merge cmd
@echo Merging: '"%ILMerge%" /wildcards /targetplatform:%net% /out:"%result%" %target_path% "%target_dir%*.dll"'
"%ILMerge%" /wildcards /targetplatform:%net% /out:"%result%" %target_path% "%target_dir%*.dll"

rem #    if succeded
IF %ErrorLevel% EQU 0 (
    
    rem #    clear real output folder and put there result assembly
    IF %ConfigurationName%==Release (

        del  %target_dir%*.*
        del  %target_dir%*.dll
        del  %target_dir%*.pdb
        del  %target_dir%*.xml
        del  %target_dir%*.*
        
        copy %result% %target_dir%
        rmdir /S /Q %outdir%
        set result=%target_path% 
        @echo Result: %target_file% "->  %target_path%"
    ) ELSE (
       @echo Result: %target_file% "->  %result%" )
   
   set status=succeded
   set errlvl=0    
) ELSE (
    set status=failed 
    set errlvl=1
    )

@echo Merge %status%
exit %errlvl% 
c# visual-studio ilmerge
1个回答
0
投票

ILMerge在Newtonsoft.Json方面遇到严重问题。您可以通过将其与ILmerge构建过程分开嵌入来解决此问题。

  1. 删除对Newtonsoft.json的引用,然后将其重新添加通过使用添加引用>指向本地文件.dll手动进行浏览。将“本地复制”设置为FALSE。
  2. 下一步在“资源”下添加.dll
  3. 在资源下,在您刚刚添加的dll下,在构建操作下选择嵌入

接下来通过像这样钩接到AppDomain.CurrentDomain.AssemblyResolve事件,告诉程序找不到dll的位置。

public static void Main()
{
    AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;

    App.Main(); 
}

private static Assembly OnResolveAssembly(object sender, ResolveEventArgs e)
{
    var thisAssembly = Assembly.GetExecutingAssembly();

    // Get the Name of the AssemblyFile
    var assemblyName = new AssemblyName(e.Name);
    var dllName = assemblyName.Name + ".dll";

    // Load from Embedded Resources - This function is not called if the Assembly is already
    // in the same folder as the app.
    var resources = thisAssembly.GetManifestResourceNames().Where(s => s.EndsWith(dllName));
    if (resources.Any())
    {

        // 99% of cases will only have one matching item, but if you don't,
        // you will have to change the logic to handle those cases.
        var resourceName = resources.First();
        using (var stream = thisAssembly.GetManifestResourceStream(resourceName))
        {
            if (stream == null) return null;
            var block = new byte[stream.Length];

            // Safely try to load the assembly.
            try
            {
                stream.Read(block, 0, block.Length);
                return Assembly.Load(block);
            }
            catch (IOException)
            {
                return null;
            }
            catch(BadImageFormatException)
            {
                return null;
            }
        }
    }

    // in the case the resource doesn't exist, return null.
    return null;
}

来源:Similar Stackoverflow Question

Related Article

© www.soinside.com 2019 - 2024. All rights reserved.