在编排函数之间传递信息时,我的自定义序列化程序有时无法正常工作,我不知道这是否是因为对象是如何嵌套/构造的,或者这是否与持久函数以及我的方式有关m 实现序列化器。大多数情况下,它似乎在由持久客户端调用的 Ochestration 内的 Activity 调用上失败。
这是详细信息:
所以我有一个自定义基类,它本质上是一个string Enum
(它是我在 Stack Overflow 上找到的想法的汇编)
public abstract class StringEnum<T>
where T : StringEnum<T>
{
public readonly string Value;
protected StringEnum(string value)
{
Value = value;
}
public override string ToString()
{
return Value;
}
public override bool Equals(object obj)
{
try
{
return (string)obj == Value;
}
catch
{
return false;
}
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
public static IEnumerable<T> All
=> typeof(T).GetProperties()
.Where(p => p.PropertyType == typeof(T))
.Select(x => (T)x.GetValue(null, null));
public static implicit operator string(StringEnum<T> enumObject)
{
return enumObject?.Value;
}
public static implicit operator StringEnum<T>(string stringValue)
{
if (All.Any(x => x.Value == stringValue))
{
Type t = typeof(T);
ConstructorInfo ci = t.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(string) }, null);
return (T)ci.Invoke(new object[] { stringValue });
}
return null;
}
public static bool operator ==(StringEnum<T> a, StringEnum<T> b)
{
return a.Value == b.Value;
}
public static bool operator !=(StringEnum<T> a, StringEnum<T> b)
{
return a.Value != b.Value;
}
}
我有两个实现:
public class ReportType : StringEnum<ReportType>, IReportType
{
private ReportType(string value): base(value) { }
public new string Value { get { return base.Value; } }
public static ReportType A_Orders => new ReportType("A_GET_ORDERS");
// ... more types
}
public class ReportStatus : StringEnum<ReportStatus>
{
private ReportStatus(string value): base(value) { }
public new string Value { get { return base.Value; } }
public static ReportStatus New => new ReportStatus("New");
public static ReportStatus Done => new ReportStatus("Done");
// ... more types
}
我写了一个自定义JsonConverter
来处理这个类的 JSON 转换
public class StringEnumJsonConverter<T> : JsonConverter<T>
where T : StringEnum<T>
{
public override void WriteJson(JsonWriter writer, T value, JsonSerializer serializer)
{
writer.WriteValue(value.ToString());
}
public override T ReadJson(JsonReader reader, Type objectType, T existingValue, bool hasExistingValue, JsonSerializer serializer)
{
string s = (string)reader.Value;
return (T)s;
}
}
然后我在函数启动中实现了它
[assembly: FunctionsStartup(typeof(Functions.Startup))]
namespace Functions
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<IMessageSerializerSettingsFactory, StringEnumMessageSerializerSettingsFactory>();
}
internal class StringEnumMessageSerializerSettingsFactory : IMessageSerializerSettingsFactory
{
public JsonSerializerSettings CreateJsonSerializerSettings()
{
return new JsonSerializerSettings()
{
Converters = new List<JsonConverter>
{
new StringEnumJsonConverter<ReportType>(),
new StringEnumJsonConverter<ReportStatus>(),
},
ContractResolver = new StringEnumResolver()
};
}
}
internal class StringEnumResolver : DefaultContractResolver
{
protected override JsonContract CreateContract(Type objectType)
{
if (objectType == typeof(ReportType))
{
return GetContract(new StringEnumJsonConverter<ReportType>()), objectType);
}
else if (objectType == typeof(ReportStatus))
{
return GetContract(new StringEnumJsonConverter<ReportStatus>(), objectType);
}
return base.CreateContract(objectType);
}
private JsonContract GetContract(JsonConverter converter, Type objectType)
{
var contract = base.CreateObjectContract(objectType);
contract.Converter = converter;
return contract;
}
}
}
}
我有一个类使用ReportType
public class ReportsRequestOptions
{
public List<ReportType> ReportTypes { get; set; }
public List<int> Ids { get; set; }
public DateTime From { get; set; }
public DateTime To { get; set; }
}
和一个同时使用两者ReportType
并在另一个类ReportStatus
中使用的类list
public class ReportRequest
{
public ReportType ReportName { get; }
public ReportStatus ReportStatus { get; set; }
// other fields that work
}
internal class ClientReportsRequest
{
public int Id {get; set; }
public List<ReportRequest> Requests { get; set; }
public DateTime To {get; set; }
public DateTime From {get; set; }
}
ReportsRequestOptions
当我将数据从我的主编排函数移动时使用,HttpTrigger
但是当我将 a 传递ClientReportsRequest
到子编排中时,JsonConverter 似乎不起作用,这些值只是Null
它们通常显示的字符串。我可以在转换器中放置一个断点并查看它是否被调用,但由于某种原因,这些值没有出现在我的本地人中,所以我无法检查它以找出发生这种情况的原因。
执行:
[FunctionName(nameof(RunReportsAsync))]
public async Task<IActionResult> RunReportsAsync(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
[DurableClient] IDurableClient client
)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
ReportsRequestOptions requestOptions = JsonConvert.DeserializeObject<ReportsRequestOptions>(requestBody, new StringEnumJsonConverter<ReportType>());
// StringEnum data is correct at this point
if (!requestOptions.ReportTypes.Any())
requestOptions.ReportTypes.AddRange(ReportType.All);
var instanceId = await client.StartNewAsync(nameof(GetReports), requestOptions);
return new OkObjectResult(instanceId);
}
[FunctionName(nameof(GetReports))]
public async Task<RunLog> GetReports(
[OrchestrationTrigger] IDurableOrchestrationContext context
)
{
var requestOptions = context.GetInput<ReportsRequestOptions>();
// string enum data is correct at this point
var clientReportsRequests = GetClientInfo(storeIds)
.Select(x => new ClientReportsRequest()
{
ReportTypes = requestOptions.ReportTypes,
Id = x.Id,
From = requestOptions.From,
To = requestOptions.To
});
// ParallelForEach Async code shouldn't be the issue here.
// it's based on this article: https://dev.to/cgillum/scheduling-tons-of-orchestrator-functions-concurrently-in-c-1ih7
var results = (await clientReportsRequests.ParallelForEachAsync(MaxParallelStoreThreadCount, clientReportsRequest =>
{
return context.CallSubOrchestratorAsync<(int, List<ReportRequest>)>(nameof(GetReportsForClient), clientReportsRequest);
})).ToDictionary(x => x.Item1, x => x.Item2);
return new RunLog(requestOptions, results);
}
[FunctionName(nameof(GetReportsForClient))]
public async Task<(int, List<ReportRequest>)> GetReportsForClient(
[OrchestrationTrigger] IDurableOrchestrationContext context
)
{
var requestOptions = context.GetInput<ClientReportsRequest>();
var completedRequests = new List<ReportRequest>();
foreach (var request in requestOptions.Requests)
{
completedRequests.add(GetReport(request));
// GetReport code has been truncated for brevity but the issue is that neither field in the request
// has it's StringEnum data at this point
}
return (requestOptions.Id, completedRequests);
}
这几天我一直在打我的头,找不到答案,有人有什么想法吗?有没有更好的方法我应该序列化这个?