我是Azure Function的新手,在我的第一个函数中,我正在使用CosmosDB。在后台,函数可以完美地完成其工作,但是当我在门户网站中打开函数时,出现此错误。
函数(LOANGILITY-AZFUNCTION / ProductDetailsFunc)错误: Microsoft.Azure.WebJobs.Host:错误索引方法 “ ProductDetailsFunc”。 Microsoft.Azure.WebJobs.Host:无法绑定 参数“ document”以键入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
。
答案 0 :(得分:1)
我们需要照顾的事情很少。
答案 1 :(得分: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;
}
}