【问题标题】:How to make Activity Function to accept json input when developing Azure Durable Function (Python)开发 Azure Durable Function (Python) 时如何使 Activity Function 接受 json 输入
【发布时间】:2022-01-03 18:43:49
【问题描述】:

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

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

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

def main(datajson: str) -> str:

HelloOrchestrator:

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)

HelloActivity

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, 

【问题讨论】:

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

标签: azure-functions azure-durable-functions


【解决方案1】:

谢谢@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

【讨论】:

    猜你喜欢
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 2021-06-14
    • 1970-01-01
    相关资源
    最近更新 更多