设置本地调试环境
您可以使用local.settings.json 文件来定义本地设置。在本地使用 Azure 存储的先决条件是您需要在您的计算机上运行 Azure Storage Emulator。在local.settings.json 文件中,将Azure Storage Account 连接字符串定义为UseDevelopmentStorage=true。该文件应如下所示:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true"
}
}
默认情况下,如果没有为QueueTrigger 属性的Connection 属性提供值,它将使用AzureWebJobsStorage 设置:
public static class FooAdded
{
[FunctionName("FooAdded")]
public static void Run([QueueTrigger("tracker-readings")]string myQueueItem, TraceWriter log)
{
log.Info($"C# Queue trigger function processed: {myQueueItem}");
}
}
或者,如果你想明确指定一个连接字符串,那么你可以将Connection设置为连接字符串的名称(不是连接字符串本身),并添加一个条目它在配置文件的Values 部分:
QueueTrigger("tracker-readings", Connection = "CustomConnection")
在local.settings.json 文件中:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"CustomConnection": "Some custom connection string"
}
}
更多详情请参考官方文档:
设置 Azure 环境:
local.settings.json 中的值不会发布到 Azure,因此需要在要部署到的 Azure 环境中定义每个设置的值。请注意,AzureWebJobsStorage 和 AzureWebJobsDashboard 的值是根据您在创建函数时选择的存储帐户自动设置的。
定义设置的最简单方法是通过 Azure 门户。转到Function App 中的应用程序设置并定义存储帐户连接字符串的实际值。您无需对 azure 函数进行任何代码更改,它会自动从应用程序设置中获取连接字符串。
或者,您可以使用Azure Resource Manager templates,以编程方式部署和更新环境设置。
您在 Azure 中创建的每个函数应用都有自己的环境,因此在为相关设置提供值后,您可以将函数部署到 Azure 中的多个环境(开发/测试/生产等)以及在本地调试代码无需每次都更改连接字符串。