[英]CosmosDB Azure function Trigger with SignalR output binding
[英]Azure Function with CosmosDb Binding Proper Local Setup
我確定這里缺少一些簡單的東西,但是我無法使它正常工作。 我已經在本地安裝了Azure存儲模擬器,並通過Visual Studio 2019創建了Azure Function 2.0。
我可以在沒有CosmosDb綁定的情況下運行該函數,如下所示:
local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"EndpointUri": "https://localhost:8081",
"PrimaryKey": "<KEY_HERE>"
}
}
Function.cs
public static class Function
{
[FunctionName("func")]
public static async Task Run(
[TimerTrigger("0 * * * * *")] TimerInfo myTimer,
ILogger log
)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
var endpointUri = new Uri(Environment.GetEnvironmentVariable("EndpointUri", EnvironmentVariableTarget.Process));
var primaryKey = Environment.GetEnvironmentVariable("PrimaryKey");
using (var client = new DocumentClient(endpointUri, primaryKey))
{
var queryOptions = new FeedOptions { MaxItemCount = -1 };
var documents = client.CreateDocumentQuery<Doc>(UriFactory.CreateDocumentCollectionUri("db_id", "col_id"), queryOptions);
foreach (var document in documents)
{
...
}
}
}
}
該代碼將按預期運行,並獲取我在本地存儲模擬器中擁有的文檔。
我想將其切換為利用CosmosDb綁定,並且似乎找不到使其工作所需的神奇咒語。
我將Function.cs
上的方法簽名更新為:
public static async Task Run(
[TimerTrigger("0 * * * * *")] TimerInfo myTimer,
ILogger log,
[CosmosDB(
databaseName: "db_id",
collectionName: "col_id",
ConnectionStringSetting = "AzureWebJobsStorage",
SqlQuery = "SELECT * FROM col_id")
] IEnumerable<Doc> documents
)
但是,當我運行它時,出現以下錯誤:
[5/17/2019 3:09:29 PM]執行'func'(失敗,ID = 8326b1c4-3dd5-461d-b8de-d777c5b925d8)[5/17/2019 3:09:29 PM] System.Private.CoreLib :執行功能:func時發生異常。 Microsoft.Azure.WebJobs.Host:異常綁定參數“文檔”。 Microsoft.Azure.DocumentDB.Core:值不能為null。 [5/17/2019 3:09:29 PM]參數名稱:authKeyOrResourceToken。
我嘗試了許多其他修補程序,但都沒有成功。
第一個是利用默認充分明確的ConnectionString: "AzureWebJobsStorage": "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;",
如Azure文檔中所找到。
知道如何更改以上內容以成功針對Azure存儲模擬器在本地執行嗎?
CosmosDB
綁定中的ConnectionStringSetting
是Cosmos DB帳戶(而不是Azure存儲)的ConnectionStringSetting
字符串的設置。 有關每個屬性,請參見官方文檔 。
步驟1:使用有效的Cosmos DB連接字符串(可以是Emulator)將新設置添加到local.settings.json
。
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"EndpointUri": "https://localhost:8081",
"PrimaryKey": "<KEY_HERE>",
"CosmosDBConnectionString": "AccountEndpoint=https://localhost:8081;AccountKey=<KEY_HERE>;",
}
}
步驟2:修改您的Function.cs
以使用該新設置:
public static async Task Run(
[TimerTrigger("0 * * * * *")] TimerInfo myTimer,
ILogger log,
[CosmosDB(
databaseName: "db_id",
collectionName: "col_id",
ConnectionStringSetting = "CosmosDBConnectionString",
SqlQuery = "SELECT * FROM col_id")
] IEnumerable<Doc> documents
)
(可選)如果要手動運行查詢或執行其他操作,則始終可以從綁定中拉入DocumentClient
實例:
public static async Task Run(
[TimerTrigger("0 * * * * *")] TimerInfo myTimer,
ILogger log,
[CosmosDB(
databaseName: "db_id",
collectionName: "col_id",
ConnectionStringSetting = "CosmosDBConnectionString")
] DocumentClient documentClient
)
附帶說明,每當您手動創建
DocumentClient
,請遵循以下准則 ,切勿在每次執行時在using
語句內創建DocumentClient
實例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.