I am straggling with that issue couple days and need help.
I send message to azure subscription in business layer using that code:
var sender = _serviceBusService.GetSender("event-send");
var message = new ServiceBusEvent()
{
SessionId = sessionId,
CourseId = courseId,
UserInfo = (BaseRequestUserInfo)userInfo,
PlaybackId = playBackId,
SsoProvider = userInfo?.SsoProvider,
Type = MessageType.SessionEvent
};
var body = JsonSerializer.Serialize(message);
var sbMessage = new ServiceBusMessage(body);
sbMessage.ApplicationProperties.Add("SsoProvider", userInfo.SsoProvider);
sbMessage.ApplicationProperties.Add("Type", MessageType.SessionEvent.ToString());
await sender.SendMessageAsync(sbMessage);
And I created Azure Function which will handle that message. I handle that message and in some cases I need to set that message to subscription one more time with EnqueueTime which depends on Delivery Count. When I debug my function, all properties in ServiceBusMessage object are null. Why does it happen? How could I fix that? Will I see Delicery Count in ApplicationProperties or there are only custom properties?
Azure Function
[FunctionName("VpSessionHandler")]
public async Task Run([ServiceBusTrigger("event-send", "vp-session-event", Connection = "ServiceBusTopicConnectionString")] ServiceBusMessage mySbMsg,
[ServiceBus("event-send", Microsoft.Azure.WebJobs.ServiceBus.ServiceBusEntityType.Topic, Connection = "ServiceBusTopicConnectionString")] ICollector<ServiceBusMessage> messageCollector)
{
try
{
var message = JsonSerializer.Deserialize<ServiceBusEvent>(mySbMsg.Body);
var accessContext = _eventTrackerService.ChooseAccessTokenContext(message.SsoProvider);
var accessToken = await accessContext.GetAccessTokenAsync(message.SsoProvider);
var eventSender = _eventTrackerService.ChooseEventSessionSenderContext(message.SsoProvider);
var sessionEventModel = new SessionEventModel()
{
PlaybackId = message.PlaybackId,
UserInfo = message.UserInfo,
AccessToken = accessToken,
CourseId = message.CourseId,
SessionId = message.SessionId,
};
var rez = await eventSender.SendSessionEventApiAsync(sessionEventModel);
if (!rez.IsSuccessStatusCode)
{
if (mySbMsg.ApplicationProperties.DeliveryCount > 1)
{
var delayTime = TimeSpan.FromSeconds(Math.Pow(2, mySbMsg.ApplicationProperties.DeliveryCount));
mySbMsg.ScheduledEnqueueTime = DateTime.UtcNow.Add(delayTime);
messageCollector.Add(mySbMsg);
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
_logger.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
}
P.S. As a first parameter I tried to use ServiceBusReceivedMessage, but I need ServiceBusMessage to add to subscription collection one more time. Additionally, new ServiceBusMessage will have new Delivery Count and it doesn't suit me.