ManojSingh-5528 avatar image
0 Votes"
ManojSingh-5528 asked MayankBargali-MSFT edited

How to Dynamically configure SubscriptionProperties for Azure ServiceBus Topic

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!

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MayankBargali-MSFT avatar image
1 Vote"
MayankBargali-MSFT answered MayankBargali-MSFT edited

@ManojSingh-5528 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(
     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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.