gpt4 book ai didi

json - Azure Durable Functions我应该如何处理TaskFailedExceptionDeserializationException

转载 作者:行者123 更新时间:2023-12-02 23:16:40 30 4
gpt4 key购买 nike

如果从 Azure ActivityFunctions 引发异常并且无法将其序列化为 json,则会引发包含原始异常作为 InnerException 的 TaskFailedExceptionDeserializationException。

这应该如何处理?可能存在我无法控制的各种异常,无法序列化。

最佳答案

如上所述here :

try injecting an IErrorSerializerSettingsFactory (introduced in v2.1)with custom JsonConverter objects on it to get around this issue

非常简化的示例(我不确定如何正确实现自定义 jsonConverter,您必须自己弄清楚)

启动类(使用 dependency injection ):

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System.Collections.Generic;

[assembly: FunctionsStartup(typeof(MyNamespace.Startup))]

namespace MyNamespace
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<IErrorSerializerSettingsFactory, CustomErrorSerializer>();
}
}

public class CustomErrorSerializer : IErrorSerializerSettingsFactory
{
public JsonSerializerSettings CreateJsonSerializerSettings()
{
return new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All,
Converters = new List<JsonConverter>
{
new CustomConverter()
}
};
}
}
}

自定义转换器类:

public class CustomConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (!value.GetType().IsSerializable)
return;

JToken t = JToken.FromObject(value);
t.WriteTo(writer);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
}

public override bool CanRead
{
get { return false; }
}

public override bool CanConvert(Type objectType)
{
return true;
}
}

我的不可序列化异常:

public class HttpResponseException : Exception
{
public HttpStatusCode StatusCode { get; }

public HttpResponseException(HttpStatusCode statusCode)
{
StatusCode = statusCode;
}

}

当我从事件函数中抛出它时,我没有收到错误消息“无法从 TaskActivity 反序列化异常”。

关于json - Azure Durable Functions我应该如何处理TaskFailedExceptionDeserializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63520382/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com