Skip to content

Reading TaskOrchestrationContext in middleware throws InvalidOperationException #3347

@stevendarby

Description

@stevendarby

Minimal repro of middleware:

internal sealed class MyMiddleware : IFunctionsWorkerMiddleware
{
    public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
    {
        if (await GetInputValueAsync<TaskOrchestrationContext>(context) is { } orchestrationContext)
        {
            // Do stuff
        }

        await next.Invoke(context);
    }

    public static async Task<T?> GetInputValueAsync<T>(FunctionContext context)
    {
        foreach (var bindingMetadata in context.FunctionDefinition.InputBindings.Values)
        {
            var functionParameter = context.FunctionDefinition.Parameters.FirstOrDefault(parameter => parameter.Name == bindingMetadata.Name && parameter.Type.IsAssignableTo(typeof(T)));
            if (functionParameter != null)
            {
                return (await context.BindInputAsync<T>(bindingMetadata)).Value;
            }
        }

        return default;
    }
}

I have middleware which tries reading function inputs as TaskOrchestrationContext so that it can then read its input and set a correlation context based on the input content. This used to work until I recently updated packages, now I get: System.InvalidOperationException: 'Orchestration history state was either missing from the input or not a string value.'

Is there a workaround? Thanks

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions