How to create and deploy function app in visual studio code?
要求
创建一个Azure功能来监视所有传入到IoT中心的消息并在对消息进行一些修改后将其发送到Cosmos数据库
建筑
问题:无法创建Azure功能,该功能将传入的消息读取到IoT中心并将其发送到Cosmos DB
已执行的步骤
我已按照以下步骤使用
创建和部署
- 打开Visual Studio代码
-
转到
Azure Extension Panel -
选择了
C# 语言 -
它创建了以下文件
- .vscode
- .gitignore
- FunctionApps.csproj
- host.json
- localsettings.json
-
现在,我单击了
Visual Studio Code 中的"创建功能"按钮,并选择了CosmosDBTrigger ,并映射了已经在Azure门户中创建的CosmoDB 。它在c#class下生成
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19namespace Company.Function
{
public static class CosmosDBTriggerCSharp
{
[FunctionName("CosmosDBTriggerCSharp")]
public static void Run([CosmosDBTrigger(
databaseName:"databaseName",
collectionName:"collectionName",
ConnectionStringSetting ="dev-test-cosmo-db_DOCUMENTDB",
LeaseCollectionName ="leases")]IReadOnlyList<Document> input, ILogger log)
{
if (input != null && input.Count > 0)
{
log.LogInformation("Documents modified" + input.Count);
log.LogInformation("First document Id" + input[0].Id);
}
}
}
}
现在,我通过键入(deploy to function app)并运行" Azure Functions:Deploy to Function App"命令来执行(Ctrl Shift P)来部署该功能。
我在Visual Studio代码
中看到
1 2 3 4 5 6 7 8 9 10 | 3:09:06 PM liveConnectMessageEnhancefunapp: Creating zip package... 3:09:07 PM liveConnectMessageEnhancefunapp: Starting deployment... 3:09:18 PM liveConnectMessageEnhancefunapp: Updating submodules. 3:09:19 PM liveConnectMessageEnhancefunapp: Preparing deployment for commit id 'f58cf57151'. 3:09:21 PM liveConnectMessageEnhancefunapp: Skipping build. Project type: Run-From-Zip 3:09:21 PM liveConnectMessageEnhancefunapp: Skipping post build. Project type: Run-From-Zip 3:09:24 PM messageEnhancefunapp: Syncing 0 function triggers with payload size 2 bytes successful. 3:09:25 PM messageEnhancefunapp: Updating D:\\home\\data\\SitePackages\\packagename.txt with deployment 20190110093910.zip 3:09:25 PM messageEnhancefunapp: Deployment successful. Deployment to"messageEnhancefunapp" completed. |
警告
C:\\Users\\deeku.nuget\\packages\\microsoft.net.sdk.functions\\1.0.24\\build\
etstandard1.0\\Microsoft.NET.Sdk.Functions.Build.targets(41,5): warning : Function [CosmosDBTriggerCSharp]: Missing value for AzureWebJobsStorage in local.settings.json. This is required for all triggers other than HTTP. [C:\\FunctionApps\\FunctionApps.csproj]
C:\\Users\\deeku.nuget\\packages\\microsoft.net.sdk.functions\\1.0.24\\build\
etstandard1.0\\Microsoft.NET.Sdk.Functions.Build.targets(41,5): warning : [C:\\FunctionApps\\FunctionApps.csproj]
但是当我导航到
Your app is currently in read-only mode because you are running from a package file. When running from a package, the file system is read-only and no changes can be made to the files. To make any changes update the content in your zip file and WEBSITE_RUN_FROM_PACKAGE app setting.
在您的示例中,您正在创建一个CosmosDBTrigger,如果您想捕获您的cosmos DB中的更改,可以使用它。但是,根据您的描述,您想捕获IoT中心中的更改。
我没有在Visual Studio Code中找到IoT Hub触发器,但是如果您使用的是Visual Studio完整版,则可以在此处找到IoT Hub触发器:
您只需要在此屏幕上设置连接,在该方法中,您将收到来自IoT中心的消息,您可以根据需要对其进行修改,并将其保存到Cosmos DB。
这是关于同一主题的类似博客文章:
https://medium.com/@avirup171/azure-iot-hub-azure-function-azure-cosmos-db-walkthrough-cc30d12d1055