使用会话ID将消息添加到IAsyncCollector主题输出中 [英] Adding messages to IAsyncCollector Topic output with a session ID

查看:59
本文介绍了使用会话ID将消息添加到IAsyncCollector主题输出中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前是否可以将消息从Azure函数推送到IAsyncCollector主题输出,还可以设置会话ID?我的话题确实是关于FIFO排序的,所以我们必须设置会话.因此,我们曾想过只将Guid设置为唯一的会话ID.我知道如何通过此输出将消息推送到我的主题,但是由于我们没有明确设置会话ID,因此当然会出错.当我们将其发送到IAsyncCollector时,是否可以在代码中的某个位置进行设置?

Is it currently possible to push messages to an IAsyncCollector Topic output from Azure functions and also set a session ID? My topics really on FIFO ordering and so we had to setup sessions. Because of this we had imagined just setting up a Guid to be the unique session id. I know how I would push messages to my topic through this output but of course that errors out since we aren't setting the session Id explicitly. Is it possible to set this somewhere in the code as we send it off to the 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函数v2)或BrokeredMessage(Azure函数v1) ).然后,您可以在邮件上设置SessionId属性.

Rather than binding to your AccountEventDTO directly, you'll need to bind to Message (Azure Functions v2) or BrokeredMessage (Azure Functions v1). Then you can set the SessionId property on the message.

要设置消息的正文,请将DTO序列化为JSON,并对其进行UTF-8编码:

To set the body of the message, serialize your DTO as JSON and UTF-8 encode it:

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 };

对于v1.

这篇关于使用会话ID将消息添加到IAsyncCollector主题输出中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆