目前是否可以将消息推送到 Azure 函数的 IAsyncCollector 主题输出并设置会话 ID?我的主题实际上是关于 FIFO 排序的,所以我们必须设置会话。因此,我们设想只设置一个 Guid 作为唯一的会话 ID。我知道如何通过此输出将消息推送到我的主题,但当然会出错,因为我们没有显式设置会话 ID。当我们将其发送到 IAsyncCollector 时,是否可以在代码中的某个位置设置它?
这就是我们所拥有的,
[FunctionName("AccountCreatedHook")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]HttpRequestMessage req,
TraceWriter log, [ServiceBus("topic-name", Connection = "busname", EntityType = Microsoft.Azure.WebJobs.ServiceBus.EntityType.Topic)] IAsyncCollector<AccountEventDTO> accountCreatedTopic)
{
log.Info("C# HTTP trigger function processed a request.");
// Get request body
var accountEvent = await req.Content.ReadAsAsync<AccountEventDTO>();
var payload = req.Content.ReadAsStringAsync().Result;
if (accountEvent != null && accountEvent.Name != null)
{
await accountCreatedTopic.AddAsync(accountEvent);
return req.CreateResponse(HttpStatusCode.OK, "Account successfully added to topic.");
}
return req.CreateResponse(HttpStatusCode.BadRequest, "Account was not formed well.");
}
而不是绑定到你的AccountEventDTO
直接,你需要绑定到Message
(Azure Functions v2)或BrokeredMessage
(Azure 函数 v1)。然后你可以设置SessionId
消息上的属性。
要设置消息正文,请将 DTO 序列化为 JSON 并对其进行 UTF-8 编码:
var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(accountEvent));
var message = new Message(bytes) { SessionId = sessionId };
对于 v2 或
var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(accountEvent));
var memoryStream = new MemoryStream(bytes, writable: false);
var message = new BrokeredMessage(memoryStream) { SessionId = sessionId };
for v1.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)