I want to keep messages in Service Bus Topic/Subscription for 30 days once it is completed, but make sure that the subscriber will not read it twice.
Here is the Azure function i created to perform it.
public class MyFunction
{
private readonly ILogger<MyFunction> _logger;
public MyFunction(ILogger<MyFunction> logger)
{
_logger = logger;
}
[Function(nameof(MyFunction))]
public async Task Run([ServiceBusTrigger("%TopicName%", "%SubscriptionName%", Connection = "ServiceBus.ConnectionString")] string message)
{
try
{
_logger.LogInformation("Started !");
JObject JsonObject = JObject.Parse(message);
}
catch (Exception ex)
{
throw;
}
}
}
The issue is that once the function above complete its execution, the message disappear from the Service Bus subscription.
Any idea ?
That's not how Azure Service Bus works. It's not a database. Once a message is successfully processed, it will be removed from the queue. If it consistently fails, it will be dead-lettered.
What you need is an auditing feature. Then, you can have successfully processed messages audited and retained for whatever period that you need to. You could either use a middleware that would do it for you (MessageHanlder, NServiceBus, MassTransit, Wolverine, etc.) or build it*.
* not trivial and will require the ability to settle messages, which is currently being worked on for the Functions Isolated Worker SDK.