我收到 Microsoft.Azure.WebJobs.Host 的 Azure 函数错误。在我的函数中,我使用 CosmosDB。在调试/本地中它的工作完美,但是当我在门户中打开我的函数时,我收到此错误。
函数(LOANGILITY-AZFUNCTION/ProductDetailsFunc)错误: Microsoft.Azure.WebJobs.Host:索引方法错误 '产品详细信息功能'。 Microsoft.Azure.WebJobs.Host:无法绑定 参数“文档”类型为 IAsyncCollector`1。确保 绑定支持参数类型。如果您使用绑定 扩展(例如 Azure 存储、ServiceBus、计时器等)确保 您已在您的中调用了扩展程序的注册方法 启动代码(例如 builder.AddAzureStorage()、builder.AddServiceBus()、 builder.AddTimers() 等)。
我的函数头原型是
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req,
[DocumentDB(
databaseName: "OB",
collectionName: "ProductDetails",
ConnectionStringSetting = "DBConnection")]IAsyncCollector<dynamic> document,
TraceWriter log)
从我的代码生成的 json 是
{
"generatedBy": "Microsoft.NET.Sdk.Functions-1.0.13",
"configurationSource": "attributes",
"bindings": [
{
"type": "httpTrigger",
"methods": [
"get",
"post"
],
"authLevel": "anonymous",
"name": "req"
}
],
"disabled": false,
"scriptFile": "../bin/Loangility01.dll",
"entryPoint": "Loangility01.ProductDetailsFunc.Run"
}
我还看到了其他一些SO问题,他们在代码中谈论
builder.something
,我没有在.Net Core Azure Function上工作,我的目标项目框架是4.6.1
。
根据我的测试,我们可以通过Visual Studio直接将Function部署到Azure上。但我们需要在 local.settings.json 中手动配置一些设置,例如 Cosmos Db 连接字符串。详细步骤如下
public static class Function2
{
[FunctionName("Function2")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, [DocumentDB(
databaseName: "ToDoItems",
collectionName: "Items",
ConnectionStringSetting = "CosmosDBConnection")]IAsyncCollector<dynamic> toDoItemsOut, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
// parse query parameter
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value;
if (name == null)
{
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
name = data?.name;
}
HttpResponseMessage response ;
if (name == null) {
response = req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body");
}
else {
response= req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}
await toDoItemsOut.AddAsync(response.Content);
return response;
}
}