How to Dynamically configure SubscriptionProperties for Azure ServiceBus Topic

Manoj Singh 41 Reputation points
2022-05-22T12:11:44.34+00:00

I'm trying to configure Azure ServiceBus Topic Subscription dynamically(using C#) with all of its properties which we can set up using Azure Portal.

I have tried the below code, but it's giving me an "object reference error" for SubscriptionProperties while setting up its values.

static SubscriptionProperties subscriptionProperties;
    static async Task Main(string[] args)
    {
        adminClient = new ServiceBusAdministrationClient(connectionString);
        client = new ServiceBusClient(connectionString);


        subscriptionProperties.AutoDeleteOnIdle = TimeSpan.FromDays(14);
        subscriptionProperties.DefaultMessageTimeToLive = TimeSpan.FromDays(14);
        subscriptionProperties.TopicName = topicName;
        subscriptionProperties.SubscriptionName = subscriptionName;
        subscriptionProperties.MaxDeliveryCount = 3;
        subscriptionProperties.LockDuration = TimeSpan.FromSeconds(5.00);
        subscriptionProperties.DeadLetteringOnMessageExpiration = true;
        subscriptionProperties.EnableDeadLetteringOnFilterEvaluationExceptions = true;

        Console.WriteLine($"Creating the subscription {subscriptionName} with a correlation filter");

        if (!await adminClient.SubscriptionExistsAsync(topicName, subscriptionName))
        {
            await adminClient.CreateSubscriptionAsync(
                    new CreateSubscriptionOptions(subscriptionProperties),
                    new CreateRuleOptions(subscriptionFilterName, new CorrelationRuleFilter() { Subject = correlationFilterValue }));

        }
}

Let me know if this is the correct way of setting the property values for "SubscriptionProperties" class or how can I do so?

Thanks for support!

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
700 questions
0 comments No comments
{count} votes

Accepted answer
  1. MayankBargali-MSFT 70,936 Reputation points Moderator
    2022-05-24T06:07:31.35+00:00

    @Manoj Singh Thanks for reaching out. Please refer to the sample here for the CURD operation.
    For Azure.Messaging.ServiceBus samples for .NET sample please refer to here.

    To create topic and subscription please refer here.

    string connectionString = "<connection_string>";  
    string topicName = "<topic_name>";  
    var client = new ServiceBusAdministrationClient(connectionString);  
    var topicOptions = new CreateTopicOptions(topicName)  
    {  
        AutoDeleteOnIdle = TimeSpan.FromDays(7),  
        DefaultMessageTimeToLive = TimeSpan.FromDays(2),  
        DuplicateDetectionHistoryTimeWindow = TimeSpan.FromMinutes(1),  
        EnableBatchedOperations = true,  
        EnablePartitioning = false,  
        MaxSizeInMegabytes = 2048,  
        RequiresDuplicateDetection = true,  
        UserMetadata = "some metadata"  
    };  
      
    topicOptions.AuthorizationRules.Add(new SharedAccessAuthorizationRule(  
        "allClaims",  
        new[] { AccessRights.Manage, AccessRights.Send, AccessRights.Listen }));  
      
    TopicProperties createdTopic = await client.CreateTopicAsync(topicOptions);  
      
    string subscriptionName = "<subscription_name>";  
    var subscriptionOptions = new CreateSubscriptionOptions(topicName, subscriptionName)  
    {  
        AutoDeleteOnIdle = TimeSpan.FromDays(7),  
        DefaultMessageTimeToLive = TimeSpan.FromDays(2),  
        EnableBatchedOperations = true,  
        UserMetadata = "some metadata"  
    };  
    SubscriptionProperties createdSubscription = await client.CreateSubscriptionAsync(subscriptionOptions);  
    

    Let me know if you still facing any issue creating the topic subscription dynamically.

    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.