当我尝试执行 Azure Durable Function 时,遇到一个烦人的错误:“context”参数的类型预计为 azure.functions.Context,得到了
代码非常简单。
import azure.functions as func
import azure.durable_functions as df
import logging
app = df.DFApp(http_auth_level=func.AuthLevel.FUNCTION)
#HTTP triggered function invoked by the client
@app.route(route="orchestrators/{functionName}/{county}")
@app.durable_client_input(client_name="starter")
async def HttpAsyncTest(req: func.HttpRequest, starter: str) -> func.HttpResponse:
logging.info("CleanMatchParcel HTTP trigger function processed a request.")
fname = req.route_params["functionName"]
county = req.route_params["county"]
if fname and fname.lower() in ["funca", "funcb", "funcc"]:
if not county:
return func.HttpResponse("Missing county parameter in the query string.", status_code=500)
fparams = {"county": county}
instance_id = await starter.start_new(fname, None, fparams)
logging.info(f"Started orchestration with ID = '{instance_id}'.")
response, respcode = starter.create_check_status_response(req, instance_id)
return func.HttpResponse(response, respcode)
else:
return func.HttpResponse("Pass a valid function name in the query string.", status_code=500)
@app.orchestration_trigger(context_name="context1")
def funca(context1: df.DurableOrchestrationContext):
inputContext = context1.get_input()
reqCounty = inputContext.get("county")
#other code
return(msg, 200)
function.json 文件如下所示:
{
"scriptFile": "function_app.py",
"bindings": [
{
"authLevel": "function",
"name": "req",
"type": "httpTrigger",
"direction": "in",
"route": "orchestrators/{functionName}/{county}",
"methods": [
"post",
"get"
]
},
{
"name": "$return",
"type": "http",
"direction": "out"
},
{
"name": "starter",
"type": "durableClient",
"direction": "in"
},
{
"name": "context1",
"type": "orchestrationTrigger",
"direction": "in"
}
]
}
我尝试删除编排触发器装饰器,并省略函数参数 context1 的类型规范。这两件事使错误消失,但随后我得到了一个不同的错误。
奇怪的是,相同的代码在另一个 http 触发的 Azure 函数应用程序中运行良好。
可能出了什么问题?
V2 python 编程模型中不存在单独的
function.json
文件。所有与绑定相关的数据都将位于 function_app.py
文件本身中,对于 V2 模型,您的文件夹结构应如下所示。
我对您的代码做了一些修改并使其按预期工作。
function_app.py-
import azure.functions as func
import azure.durable_functions as df
import logging
app = df.DFApp(http_auth_level=func.AuthLevel.FUNCTION)
#HTTP triggered function invoked by the client
@app.route(route="orchestrators/{functionName}/{county?}")
@app.durable_client_input(client_name="starter")
async def HttpAsyncTest(req: func.HttpRequest, starter: str) -> func.HttpResponse:
logging.info("CleanMatchParcel HTTP trigger function processed a request.")
fname = req.route_params["functionName"]
county = req.route_params.get("county")
if fname and fname.lower() in ["funca", "funcb", "funcc"]:
if not county:
return func.HttpResponse("Missing county parameter in the query string.", status_code=500)
fparams = {"county": county}
instance_id = await starter.start_new(fname, None, fparams)
logging.info(f"Started orchestration with ID = '{instance_id}'.")
response = starter.create_check_status_response(req, instance_id)
return response
else:
return func.HttpResponse("Pass a valid function name in the query string.", status_code=500)
@app.orchestration_trigger(context_name="context")
def funca(context1: df.DurableOrchestrationContext):
inputContext = context1.get_input()
reqCounty = inputContext.get("county")
return (reqCounty,200)
我在调用函数 Url 时得到了预期的响应。
如果缺少县参数-
如果传递了无效的函数名-