首页 > 解决方案 > context.GetInput在持久函数中获取 null

问题描述

我面临一个奇怪的问题。搜索了多个问题,但并没有真正解决这个问题。我有以下默认模板代码。

 [FunctionName("OrchFunction_HttpStart")]
    public async Task<HttpResponseMessage> HttpStart(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestMessage req,
        [DurableClient] IDurableOrchestrationClient starter,
        ILogger log)
    {
        // Function input comes from the request content.
        string instanceId = await starter.StartNewAsync("OrchFunction", null);

        log.LogInformation($"Started orchestration with ID = '{instanceId}'.");

        return starter.CreateCheckStatusResponse(req, instanceId);
    }

在 Orchstrator 函数中,我有以下代码

[FunctionName("OrchFunction")]
        public  async Task<List<string>> RunOrchestrator(
            [OrchestrationTrigger] IDurableOrchestrationContext context)
        {
            var outputs = new List<string>();
            var data = context.GetInput<JobPayload>();
            var inst=context.InstanceId;
           

            // returns ["Hello Tokyo!", "Hello Seattle!", "Hello London!"]
            return outputs;
        }

这里的问题是我要NULL进去了var data = context.GetInput<JobPayload>();。不知道为什么,因为它是我传递的 T 类型HttpRequestMessage。我知道它错了,但试过了var data = context.GetInput<HttpResponseMessage>();,仍然是空的。这里有什么问题?我得到了context.InstanceId价值。

标签: c#asp.net-coreasp.net-core-3.1azure-function-appazure-durable-functions

解决方案


https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.webjobs.extensions.durabletask.idurableorchestrationclient.startnewasync?view=azure-dotnet#Microsoft_Azure_WebJobs_Extensions_DurableTask_IDurableOrchestrationClient_StartNewAsync_System_String_System_String _

下面是 StartNewAsync 的不同重载。您正在使用的那个不会将任何输入传递给编排器,因此您不会在编排器中有任何输入。用这个作为启动器

 [FunctionName("OrchFunction_HttpStart")]
    public async Task<HttpResponseMessage> HttpStart(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestMessage req,
        [DurableClient] IDurableOrchestrationClient starter,
        ILogger log)
    {
        var payload = new JobPayload()
        {
          //Fill with data
        }
        // Function input comes from the request content.
        string instanceId = await starter.StartNewAsync<JobPayload>("OrchFunction", payload);

        log.LogInformation($"Started orchestration with ID = '{instanceId}'.");

        return starter.CreateCheckStatusResponse(req, instanceId);
    }

注意:JobPayload 必须是可序列化的


推荐阅读