我正在使用Azure功能从事件中心接收邮件,并通过Azure通知中心发送通知。效果很好!现在,我想知道是否可以为这些邮件添加标记,以便通过这些标记进行用户定位。
通知中心的输出有一个“tag expression”参数,您可以配置该参数。但这似乎是一个静态文本。我需要根据从事件中心收到的消息动态设置这些标记。我不确定你是否可以以某种方式将动态内容放在那里?
我还发现我正在使用的GcmNotification对象的构造函数有一个允许使用标记字符串的重载。但是,当我尝试在编译时收到警告时说明这已被弃用,当函数触发时,它显示错误,因为Tag属性应为空。
所以我不清楚a)这是否可行; b)如何做到这一点。有任何想法吗?
更新:按照建议我尝试创建一个POCO对象以映射到我的输入字符串。字符串如下:
[{"deviceid":"repsaj-neptune-win10pi","readingtype":"temperature1","reading":22.031614503139451,"threshold":23.0,"time":"2016-06-22T09:38:54.1900000Z"}]
POCO对象:
public class RuleMessage
{
public string deviceid;
public string readingtype;
public object reading;
public double threshold;
public DateTime time;
}
对于函数我现在尝试RuleMessage[]
和List<RuleMessage>
作为参数类型,但函数抱怨它无法转换输入:
2016-06-24T18:25:16.830执行函数时出现异常:Functions.submerged-function-ruleout。 Microsoft.Azure.WebJobs.Host:异常绑定参数'inputMessage'。 Microsoft.Azure.WebJobs.Host:将参数绑定到复杂对象(例如'RuleMessage')使用Json.NET序列化。 1.将参数类型绑定为'string'而不是'RuleMessage'以获取原始值并避免JSON反序列化,或者 2.将队列有效负载更改为有效的json。 JSON解析器失败:无法将当前JSON数组(例如[1,2,3])反序列化为类型'Submission#0 + RuleMessage',因为该类型需要JSON对象(例如{“name”:“value”})来正确反序列化。 要修复此错误,请将JSON更改为JSON对象(例如{“name”:“value”})或将反序列化类型更改为数组或实现集合接口的类型(例如ICollection,IList),例如List从JSON数组反序列化。 JsonArrayAttribute也可以添加到类型中,以强制它从JSON数组反序列化。
功能代码:
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.Azure.NotificationHubs;
public static void Run(List<RuleMessage> inputEventMessage, string inputBlob, out Notification notification, out string outputBlob, TraceWriter log)
{
if (inputEventMessage == null || inputEventMessage.Count != 1)
{
log.Info($"The inputEventMessage array was null or didn't contain exactly one item.");
notification = null;
outputBlob = inputBlob;
return;
}
log.Info($"C# Event Hub trigger function processed a message: {inputEventMessage[0]}");
if (String.IsNullOrEmpty(inputBlob))
inputBlob = DateTime.MinValue.ToString();
DateTime lastEvent = DateTime.Parse(inputBlob);
TimeSpan duration = DateTime.Now - lastEvent;
if (duration.TotalMinutes >= 0) {
notification = GetGcmMessage(inputMessage.First());
log.Info($"Sending notification message: {notification.Body}");
outputBlob = DateTime.Now.ToString();
}
else {
log.Info($"Not sending notification message because of timer ({(int)duration.TotalMinutes} minutes ago).");
notification = null;
outputBlob = inputBlob;
}
}
private static Notification GetGcmMessage(RuleMessage input)
{
string message;
if (input.readingtype == "leakage")
message = String.Format("[FUNCTION GCM] Leakage detected! Sensor {0} has detected a possible leak.", input.reading);
else
message = String.Format("[FUNCTION GCM] Sensor {0} is reading {1:0.0}, threshold is {2:0.0}.", input.readingtype, input.reading, input.threshold);
message = "{\"data\":{\"message\":\""+message+"\"}}";
return new GcmNotification(message);
}
public class RuleMessage
{
public string deviceid;
public string readingtype;
public object reading;
public double threshold;
public DateTime time;
}
更新28-6-2016 :我没有设法通过将ASA输出切换到分离的行来使它不再生成JSON数组。这是个临时的。修复,因为只要输出中有多行(可能发生),函数绑定现在就会失败。
无论如何,我现在开始设置tagExpression,根据指令我将其更改为:
{
"type": "notificationHub",
"name": "notification",
"hubName": "repsaj-neptune-notifications",
"connection": "repsaj-neptune-notifications_NOTIFICATIONHUB",
"direction": "out",
"tagExpression": "deviceId:{deviceid}"
}
{deviceid}
等于我的RuleMessage POCO上的deviceid属性。不幸的是,当我调用它输出的函数时,这不起作用:
执行函数时出现异常:Functions.submerged-function-ruleout。 Microsoft.Azure.WebJobs.Host:异常绑定参数'notification'。 Microsoft.Azure.WebJobs.Host:命名参数'deviceid'没有值。
这不是真的,我确定已经设置了属性,因为我已将其记录到输出窗口。我也尝试了类似{inputEventMessage.deviceid}的东西,但这也不起作用(因为我没有得到运行时如何将{deviceid}映射到正确的输入对象,而不是一个。
答案 0 :(得分:1)
tagExpression
绑定属性支持来自触发器输入属性的绑定参数。例如,假设您的传入Event Hub事件具有属性A
和B
。您可以使用parens语法在tagExpression
中使用这些属性,例如:My Tag {A}-{B}
。
通常,所有绑定类型的大多数属性都以这种方式支持绑定参数。