Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,423 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello,
Trying to migrate a code from .NET6 to .NET8, isolated worker model, I have noticed that: if the orchestrator has a struct type parameter having a Guid field, getting the input from the orchestrator, with context.GetInput<T>(), results in an empty Guid.
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask" Version="1.1.4" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.15.0" />
In Program.cs, I have this line: services.Configure<JsonSerializerOptions>(o => o.IncludeFields = true);
public class Message
{
public InputModel Id { get; set; }
}
public struct InputModel
{
public Guid Id { get; }
public InputModel(Guid value) => Id = value;
public static InputModel FromString(string value) => new InputModel(Guid.Parse(value));
}
//this is how the orchestrator is called
await client.ScheduleNewOrchestrationInstanceAsync(
nameof(MyOrchestrator),
new Message
{
Id = InputModel.FromString(data)
},
new StartOrchestrationOptions { InstanceId = rowKey });
//in the orchestrator, trying to get the Message
var message = ctx.GetInput<Message>();
//the message contains an empty Guid
I have found the issue: the InputModel struct should be serialized correctly.
public class Message
{
public InputModel Id { get; set; }
}
[JsonConverter(typeof(InputModelConverter))]
public struct InputModel
{
public Guid Id { get; }
public InputModel(Guid value) => Id = value;
public static InputModel FromString(string value) => new InputModel(Guid.Parse(value));
}
public class InputModelConverter : JsonConverter<InputModel>
{
public override InputModel Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
// Assuming the JSON value is a simple string that can be parsed as a Guid
if (reader.TokenType != JsonTokenType.String)
{
throw new JsonException("Expected a string value.");
}
string guidString = reader.GetString();
if (!Guid.TryParse(guidString, out Guid guidValue))
{
throw new JsonException("The string value is not a valid GUID.");
}
return InputModel.FromString(guidValue.ToString());
}
public override void Write(Utf8JsonWriter writer, InputModel value, JsonSerializerOptions options)
{
// Serialize the InputModel instance's Id as a string
writer.WriteStringValue(value.Id.ToString());
}
}
}
//this is how the orchestrator is called
await client.ScheduleNewOrchestrationInstanceAsync(
nameof(MyOrchestrator),
new Message
{
Id = InputModel.FromString(data)
},
new StartOrchestrationOptions { InstanceId = rowKey });
//in the orchestrator, trying to get the Message
var message = ctx.GetInput<Message>();
//the message contains the correct Guid now