![](/img/trans.png)
[英]Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'myContext' to type DataContext. error in Azure Function v2
[英]Azure Function Error: Microsoft.Azure.WebJobs.Host: Error indexing method 'Function1'. Cannot bind parameter 'document' to type IAsyncCollector`
我是Azure函數的新手,在我的第一個函數中,我正在使用CosmosDB。 在后台,函數可以完美地完成其工作,但是當我在門戶網站中打開函數時,出現此錯誤。
函數(LOANGILITY-AZFUNCTION / ProductDetailsFunc)錯誤:Microsoft.Azure.WebJobs.Host:錯誤索引方法“ ProductDetailsFunc”。 Microsoft.Azure.WebJobs.Host:無法將參數'document'綁定為IAsyncCollector`1。 確保綁定支持參數類型。 如果您使用綁定擴展(例如Azure存儲,ServiceBus,Timer等),請確保已在啟動代碼中調用了擴展的注冊方法(例如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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.