MiniExcel icon indicating copy to clipboard operation
MiniExcel copied to clipboard

Support asp.net 8.0 AOT properties

Open shps951023 opened this issue 2 years ago • 0 comments

image

fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HN0IL3TK5Q12", Request id "0HN0IL3TK5Q12:00000001": An unhandled exception was thrown by the application.
      System.InvalidOperationException: Todo un-ignore properties count can't be 0
         at MiniExcelLibs.Utils.CustomPropertyHelper.GetSaveAsProperties(Type, Configuration) + 0xd5
         at MiniExcelLibs.OpenXml.ExcelOpenXmlSheetWriter.SetGenericTypePropertiesMode(Type, String&, Int32&, List`1&) + 0x73
         at MiniExcelLibs.OpenXml.ExcelOpenXmlSheetWriter.CreateSheetXml(Object, String) + 0x2c8
         at MiniExcelLibs.OpenXml.ExcelOpenXmlSheetWriter.SaveAs() + 0x2ad
         at MiniExcelLibs.MiniExcel.SaveAs(String, Object, Boolean, String, ExcelType, IConfiguration, Boolean) + 0x147
         at Program.<>c__DisplayClass0_0.<<Main>$>b__1() + 0x7c
         at Microsoft.AspNetCore.Http.Generated.<GeneratedRouteBuilderExtensions_g>F69328E0708B4B584C5AACA22FE2C51A1CF192D6622828F613FC57C583CA77B63__GeneratedRouteBuilderExtensionsCore.<>c__DisplayClass2_0.<MapGet0>g__RequestHandler|4(HttpContext httpContext) + 0x18
         at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext) + 0x299
         at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext) + 0x377
         at Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware.Invoke(HttpContext context) + 0x109
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.<ProcessRequests>d__238`1.MoveNext() + 0x36f
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <InvariantGlobalization>true</InvariantGlobalization>
    <PublishAot>true</PublishAot>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="MiniExcel" Version="1.31.3" />
  </ItemGroup>

</Project>

using System.Diagnostics;
using System.Text.Json.Serialization;

var builder = WebApplication.CreateSlimBuilder(args);

builder.Services.ConfigureHttpJsonOptions(options =>
{
    options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
});

var app = builder.Build();

var sampleTodos = new Todo[] {
    new(1, "Walk the dog"),
    new(2, "Do the dishes", DateOnly.FromDateTime(DateTime.Now)),
    new(3, "Do the laundry", DateOnly.FromDateTime(DateTime.Now.AddDays(1))),
    new(4, "Clean the bathroom"),
    new(5, "Clean the car", DateOnly.FromDateTime(DateTime.Now.AddDays(2)))
};

var todosApi = app.MapGroup("/todos");
todosApi.MapGet("/", () => {
    var path = Path.GetTempPath() + Guid.NewGuid() + ".xlsx";
    MiniExcelLibs.MiniExcel.SaveAs(path, sampleTodos);
    Debug.WriteLine(path);
    Console.WriteLine(path);
    return sampleTodos;
});
todosApi.MapGet("/{id}", (int id) =>
    sampleTodos.FirstOrDefault(a => a.Id == id) is { } todo
        ? Results.Ok(todo)
        : Results.NotFound());

app.Run();

public record Todo(int Id, string? Title, DateOnly? DueBy = null, bool IsComplete = false);

[JsonSerializable(typeof(Todo[]))]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{

}

shps951023 avatar Jan 11 '24 13:01 shps951023