我有一个IoTHub,其路由指向触发函数的EventHub.
我在从事件对象获取DeviceId和其他IoT Hub属性时遇到问题,而没有将这些属性显式添加到有效负载.
如果我将输入类型设置为string(或自定义类型):
public static void Run(string iotMessage, TraceWriter log) {
log.Info($"C# Event Hub trigger function processed a message: {iotMessage}");
}
Run Code Online (Sandbox Code Playgroud)
我只获得有效负载而没有任何其他IoT Hub属性,如DeviceId,CorrelationId或MessageId.
我尝试将类型设置为EventData:
public static void Run(EventData iotMessage, TraceWriter log) {
log.Info($"C# Event Hub trigger function processed a message: {JsonConvert.SerializeObject(iotMessage)}");
}
Run Code Online (Sandbox Code Playgroud)
现在我可以通过两个getter访问IoT Hub属性:Properties和SystemProperties.例如,我可以像这样访问DeviceId iotMessage.SystemProperties["iothub-connection-device-id"].但它不会暴露有效载荷.
那么如何访问IoT Hub属性和有效负载?
在我们的 VSTS 发布管道中,我们想要调用一个 powershell 脚本,该脚本为我的 Azure 函数之一添加函数密钥(使用密钥管理 rest API)。
我根据这篇文章创建了一个脚本:https : //www.markheath.net/post/managing-azure-function-keys
Param(
[string] [Parameter(Mandatory=$true)] $ResourceGroup,
[string] [Parameter(Mandatory=$true)] $AppName,
[string] [Parameter(Mandatory=$true)] $FunctionName,
[string] [Parameter(Mandatory=$true)] $KeyName,
[string] [Parameter(Mandatory=$true)] $KeyValue
)
function getAuthenticationToken([string]$appName, [string]$resourceGroup)
{
$user = az webapp deployment list-publishing-profiles -n $appName -g $resourceGroup `
--query "[?publishMethod=='MSDeploy'].userName" -o tsv
$pass = az webapp deployment list-publishing-profiles -n $appName -g $resourceGroup `
--query "[?publishMethod=='MSDeploy'].userPWD" -o tsv
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$jwt = Invoke-RestMethod -Uri "https://$appName.scm.azurewebsites.net/api/functions/admin/token" -Headers @{Authorization=("Basic {0}" -f $encodedCreds)} …Run Code Online (Sandbox Code Playgroud) 我之前关于Azure Functions的帖子的后续问题.我需要使用命令式绑定器(Binder)更新DocumentDB中的文档.我真的不明白文档,我找不到任何例子(我或多或少找到一种TextWriter的例子).文档说我可以绑定到"out T",我找不到这个例子.
在运行函数之前说文档看起来像这样:
{
child: {
value: 0
}
}
Run Code Online (Sandbox Code Playgroud)
功能看起来像这样:
var document = await binder.BindAsync<dynamic>(new DocumentDBAttribute("myDB", "myCollection")
{
ConnectionStringSetting = "my_DOCUMENTDB",
Id = deviceId
});
log.Info($"C# Event Hub trigger function processed a message: document: { document }");
document.value = 100;
document.child.value = 200;
log.Info($"Updated document: { document }");
Run Code Online (Sandbox Code Playgroud)
根据第二个日志记录行,文档未正确更新.子项未更新(从商店读取时存在)并添加了值.无论哪种方式,都没有任何东西存在.我已经尝试在function.json中添加一个输出,但是编译器抱怨它并且文档声明你不应该有任何输出.
我错过了什么?