System.Text.Json的PublishTrimmed错误

koaltpgm  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(85)

我试图用参数-p:PublishTrimmed=true发布我的c#代码。它使用System.Text.JSON,并给我一个警告。/home/USER/tymaker-config-builder/Program.cs(69,33): warning IL2026: Using member 'System.Text.Json.JsonSerializer.Serialize<SerializeExtra.LetterInfo>(SerializeExtra.LetterInfo, System.Text.Json.JsonSerializerOptions?)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved. [/home/USER/tymaker-config-builder/tymaker-config-builder.csproj]
它也给了我另一个非常相似的警告:/home/tymaker-config-builder/Program.cs(69,13): Trim analysis warning IL2026: SerializeExtra.Program.Main(): Using member 'System.Text.Json.JsonSerializer.Serialize<TValue>(TValue,JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved. [/home/USER/tymaker-config-builder/tymaker-config-builder.csproj]
有没有办法解决这个问题,同时保持我的输出修剪?我不想在我的GitHub上发布~ 70 MB的可执行文件。
下面是我的代码:

using System.Text.Json;
using System.Text.Json.Serialization;
using System.IO;

namespace SerializeExtra
{
    
    public class LetterInfo
    {
        public string? gift { get; set; }
        public string? party { get; set; }
        public string? sender { get; set; }
        public string? address { get; set; }
    }

    public class Program
    {
        public static void Main()
        {
            
            Console.Write("Thank-you note bot JSON generator");
            Console.Write('\n');
            Console.Write("Version 1.0.0");
            Console.Write("\n\n");
            var letterInfo = new LetterInfo { };
            Console.Write("What gift are you getting?\nThis gift will be used until changed.\nIf you want to skip this, type \"skip\"\n\n");
            letterInfo.gift = Console.ReadLine();
            if (letterInfo.gift == "skip")
            {
                letterInfo.gift = null;
            }
            Console.Write("\n\n");
            Console.Write("What party are the thank-you notes for?\nThis party will be used until changed.\nIf you want to skip this, type \"skip\"\n\n");
            letterInfo.party = Console.ReadLine();
            if (letterInfo.party == "skip")
            {
                letterInfo.party = null;
            }
            Console.Write("\n\n");
            Console.Write("What is your name?\nThis sender will be used until changed.\nIf you want to skip this, type \"skip\"\n\n");
            letterInfo.sender = Console.ReadLine();
            if (letterInfo.sender == "skip")
            {
                letterInfo.sender = null;
            }
            Console.Write("\n\n");
            if (letterInfo.sender == null)
            {
                Console.Write("What do you want your closing word(s) to be? (e.g: {answer}, Joe)\nThis closing word will be used until changed.\nIf you want to skip this, type \"skip\"\n\n");
            } else
            {
                Console.Write("What do you want your closing word(s) to be? (e.g: {answer}, " + letterInfo.sender + ")\nThis closing word will be used until changed.\nIf you want to skip this, type \"skip\"\n\n");
            }
            letterInfo.address = Console.ReadLine();
            if (letterInfo.address == "skip")
            {
                letterInfo.address = null;
            }
            Console.Write("\n\n");
            
            var options = new JsonSerializerOptions { WriteIndented = true };
            string jsonString = JsonSerializer.Serialize(letterInfo, options);

            string savePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            File.WriteAllText(Path.Combine(savePath, "tymaker.json"), jsonString);
            Console.Write("Configuration file (tymaker.json) is saved at " + savePath + "\n\n");
        }
    }
}

字符串

xriantvc

xriantvc1#

This Microsoft article解释了如何在使用System.Text.Json时使用源代码生成,这在启用修剪时是必需的。
对于您的示例,您将创建一个从JsonSerializerContext派生的“上下文”类。

[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(LetterInfo))]
internal partial class LetterInfoContext : JsonSerializerContext
{
}

字符串
当你调用JsonSerializer.SerializeJsonSerializer.Deserialize时,你需要在上下文中传递:

string jsonString = JsonSerializer.Serialize(letterInfo, LetterInfoContext.Default.LetterInfo);


现在,如果你尝试发布,你应该看到它是成功的,没有给出这些警告:

❯ dotnet publish -c release -r linux-x64 --sc ./SerializeExtra.csproj
MSBuild version 17.8.3+195e7f5a3 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  SerializeExtra -> /home/user/repos/temp/SerializeExtra/bin/release/net8.0/linux-x64/SerializeExtra.dll
  SerializeExtra -> /home/user/repos/temp/SerializeExtra/bin/release/net8.0/linux-x64/publish/

相关问题