如何在开发 Azure Durable Function 时使 Activity 函数接受 json 输入 (Python)

How to make Activity Function to accept json input when developing Azure Durable Function (Python)

我有 Azure 持久功能。它主要基于: https://docs.microsoft.com/en-us/azure/azure-functions/durable/quickstart-python-vscode

我正在尝试将常规 HTTP 触发器迁移为持久函数吗?

我已修改代码以在 HTTP 启动和 Orchestrator 函数中接受 json。 但是 Activity 函数不能接受 json。 如何修改HelloActivity接受json?下一行似乎会引起问题。

def main(datajson: str) -> str:

你好Orchestrator:

import logging
import json

import azure.functions as func
import azure.durable_functions as df


def orchestrator_function(context: df.DurableOrchestrationContext):

    # Take the json data from the DurableOrchestrationContext, which was passed during the invocation
    data = context._input
    data = json.loads(data)
    logging.info(f"Orchestrator: Input is: {data}")
    
    result1 = yield context.call_activity('HelloActivity', data)
    #result1 = yield context.call_activity('HelloActivity', "ThisWouldWork")

    return [result1]
    main = df.Orchestrator.create(orchestrator_function)

你好Activity

import logging

def main(datajson: str) -> str:
    logging.info(f"Activity datajson :" + datajson)
    return f"Hello world"

错误:

Function 'HelloOrchestrator (Orchestrator)' failed with an error. Reason: Message: 
Activity function 'HelloActivity' failed:  TypeError: can only concatenate str (not 
"dict") to str 
 {"$type":"System.Exception, 
System.Private.CoreLib","ClassName":"System.Exception","Message":" TypeError: can only 
concatenate str (not \"dict\") to str","Data":null,"InnerException":{"$type":"Microsoft.Azure.WebJobs.Script.Workers.Rpc.RpcException, 

谢谢@John Hanley,您的建议将其作为答案发布以帮助其他社区。

Reason: Message:  Activity function 'HelloActivity' failed: 
TypeError: can only concatenate str (not  "dict") to str  ```

对于上述错误:

" 你的错误是这一行 logging. info(f"Orchestrator: Input is: {data}"), 传递原始字符串 (context._input) 而不是 dictionary 对象 ."

有关详细信息,请参阅此 Blog