dapr / dotnet-sdk

Dapr SDK for .NET
Apache License 2.0
1.11k stars 337 forks source link

Add support Native AOT #1097

Open alexbocharov opened 1 year ago

alexbocharov commented 1 year ago

Current version sdk isn't compatible compiles to Native AOT.

System.InvalidOperationException: JsonSerializerOptions instance must specify a TypeInfoResolver setting before being marked as read-only.
src-gateway-1          |          at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_JsonSerializerOptionsNoTypeInfoResolverSpecified() + 0x27
src-gateway-1          |          at System.Text.Json.JsonSerializerOptions.ConfigureForJsonSerializer() + 0x20
src-gateway-1          |          at System.Text.Json.JsonSerializer.GetTypeInfo(JsonSerializerOptions, Type) + 0x2d
src-gateway-1          |          at System.Text.Json.JsonSerializer.GetTypeInfo[T](JsonSerializerOptions) + 0x2b
src-gateway-1          |          at System.Text.Json.JsonSerializer.SerializeToUtf8Bytes[TValue](TValue, JsonSerializerOptions) + 0x21
src-gateway-1          |          at Dapr.Client.TypeConverters.ToJsonByteString[T](T, JsonSerializerOptions) + 0x16
src-gateway-1          |          at Dapr.Client.DaprClientGrpc.PublishEventAsync[TData](String, String, TData, CancellationToken) + 0x5d
alexbocharov commented 1 year ago
 at System.Text.Json.JsonSerializer.GetTypeInfo(JsonSerializerOptions, Type) + 0x2d
         at System.Text.Json.JsonSerializer.GetTypeInfo[T](JsonSerializerOptions) + 0x2b
         at Microsoft.AspNetCore.Builder.DaprEndpointRouteBuilderExtensions.<>c__DisplayClass2_0.<<CreateSubscribeEndPoint>b__0>d.MoveNext() + 0x346
      --- End of stack trace from previous location ---
         at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c
         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xbe
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task) + 0x44
         at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.<Invoke>d__13.MoveNext() + 0xd6
alexbocharov commented 1 year ago

I'm use Dapr.AspNetCore 1.11.0-rc01

adamreed90 commented 1 year ago

See https://github.com/dotnet/core/issues/8288 and specifically https://learn.microsoft.com/en-us/aspnet/core/fundamentals/native-aot?view=aspnetcore-8.0#changes-to-support-source-generation

alexbocharov commented 1 year ago

@adamreed90 Problem Subscription. This class is internal on Dapr.

adamreed90 commented 1 year ago

Did you try creating a serializer for it as shown in the 2nd link?

adamreed90 commented 1 year ago

If you're using the .NET 8 Minimal Api with Native AoT Project Example, you need to add the SerializerContext to the TypeInfoResolverChain for Dapr.

Example:

var dapr = app.Services.GetService(typeof(DaprClient)) as DaprClient;
dapr.JsonSerializerOptions.TypeInfoResolverChain.Clear();
dapr.JsonSerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
paule96 commented 8 months ago

@adamreed90 at least for me this is not working for dapr workflows. I still get the error mentioned above.

"properties": {
    "dapr.workflow.custom_status": "",
    "dapr.workflow.failure.error_message": "Reflection-based serialization has been disabled for this application. Either use the source generator APIs or explicitly configure the 'JsonSerializerOptions.TypeInfoResolver' property.",
    "dapr.workflow.failure.error_type": "System.InvalidOperationException",
    "dapr.workflow.input": ""
  }

That's my app right now.


var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services =>
{
    services.ConfigureHttpJsonOptions(o =>
    {
        o.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
    });
    services.AddDaprClient(o =>
    {
        var op = new JsonSerializerOptions();
        op.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
        o.UseJsonSerializationOptions(op);
    });
    services.AddDaprWorkflow(options =>
    {
        options.RegisterWorkflow<GetWeatherAndMood>();
    });
});

// Start the app - this is the point where we connect to the Dapr sidecar
using var host = builder.Build();
host.Run();

[JsonSerializable(typeof(IEnumerable<int>))]
[JsonSerializable(typeof(string))]
[JsonSerializable(typeof(IEnumerable<WorkflowResult>))]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{

}