具有 CosmosDB 输入绑定的 Azure Function App - 我可以在不使用 Urifactory.CreateDocumentURI() 的情况下获取数据吗?
Azure Function App with CosmosDB input binding - can I get data without using Urifactory.CreateDocumentURI()?
我是 Azure 的新手。我尝试使用 .NET Core 在 Visual Studio 中创建 Cosmos DB 输入绑定。
首先,我在我的 Cosmos DB 中创建了数据:
{
"id": "1",
"name": "myName",
"full_name": "My full name"
}
其次,我修改了local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
},
/* WARNING! Remember to add same ConnectionString in Azure Portal
(App Function -> Configuration) */
"ConnectionStrings": {
"CosmosDBConnectionString": "AccountEndpoint=my_connection_string"
}
}
第三次,我添加了我的代码:
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
[CosmosDB(
"BindingDB",
"names",
ConnectionStringSetting = "CosmosDBConnectionString")] DocumentClient documentClient,
ILogger log)
{
log.LogWarning(">>> Function started! <<<");
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
string partitionKey = data?.name ?? "";
string id = data?.id ?? "";
Document doc = await documentClient.ReadDocumentAsync(
UriFactory.CreateDocumentUri("BindingDB", "names", id),
new RequestOptions { PartitionKey = new PartitionKey(partitionKey)
});
var fullName = doc.GetPropertyValue<string>("full_name") ?? "";
string responseMessage = string.IsNullOrEmpty(fullName) ?
"Something went wrong!" :
$"Hello, {fullName}.";
return new OkObjectResult(responseMessage);
}
最后,我发送POST请求正文:
{
"id": "1",
"name": "myName"
}
有效!但我有一个问题:
问题 - 我在代码中硬编码了我的数据库和集合名称两次!我喜欢干净的代码。我可以不使用 UriFactory.CreateDocumentUri()
以某种方式获取我的数据吗?
文档不清晰,网上有些资源已经过时。这就是我在这里问的原因。
当然,您可以使用 this 替代方法(无需创建 URI):
//This reads a document record from a database & collection where
// - sample_database is the ID of the database
// - sample_collection is the ID of the collection
// - document_id is the ID of the document resource
// - partition_key is the Partition Key value, needed for Cosmos DB
var docLink = "dbs/sample_database/colls/sample_collection/docs/document_id";
var parKey = new RequestOptions { PartitionKey = new PartitionKey("partition_key") };
Customer customer = await client.ReadDocumentAsync<Customer>(docLink, parKey);
或者,您可以在静态变量中读取一次配置设置,然后在整个函数应用程序中使用它。
我是 Azure 的新手。我尝试使用 .NET Core 在 Visual Studio 中创建 Cosmos DB 输入绑定。
首先,我在我的 Cosmos DB 中创建了数据:
{
"id": "1",
"name": "myName",
"full_name": "My full name"
}
其次,我修改了local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
},
/* WARNING! Remember to add same ConnectionString in Azure Portal
(App Function -> Configuration) */
"ConnectionStrings": {
"CosmosDBConnectionString": "AccountEndpoint=my_connection_string"
}
}
第三次,我添加了我的代码:
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
[CosmosDB(
"BindingDB",
"names",
ConnectionStringSetting = "CosmosDBConnectionString")] DocumentClient documentClient,
ILogger log)
{
log.LogWarning(">>> Function started! <<<");
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
string partitionKey = data?.name ?? "";
string id = data?.id ?? "";
Document doc = await documentClient.ReadDocumentAsync(
UriFactory.CreateDocumentUri("BindingDB", "names", id),
new RequestOptions { PartitionKey = new PartitionKey(partitionKey)
});
var fullName = doc.GetPropertyValue<string>("full_name") ?? "";
string responseMessage = string.IsNullOrEmpty(fullName) ?
"Something went wrong!" :
$"Hello, {fullName}.";
return new OkObjectResult(responseMessage);
}
最后,我发送POST请求正文:
{
"id": "1",
"name": "myName"
}
有效!但我有一个问题:
问题 - 我在代码中硬编码了我的数据库和集合名称两次!我喜欢干净的代码。我可以不使用 UriFactory.CreateDocumentUri()
以某种方式获取我的数据吗?
文档不清晰,网上有些资源已经过时。这就是我在这里问的原因。
当然,您可以使用 this 替代方法(无需创建 URI):
//This reads a document record from a database & collection where
// - sample_database is the ID of the database
// - sample_collection is the ID of the collection
// - document_id is the ID of the document resource
// - partition_key is the Partition Key value, needed for Cosmos DB
var docLink = "dbs/sample_database/colls/sample_collection/docs/document_id";
var parKey = new RequestOptions { PartitionKey = new PartitionKey("partition_key") };
Customer customer = await client.ReadDocumentAsync<Customer>(docLink, parKey);
或者,您可以在静态变量中读取一次配置设置,然后在整个函数应用程序中使用它。