如何在 C# 中为 Azure ServiceSub 订阅动态配置不同的 "SubscriptionProperties"

How to dynamically Configure different "SubscriptionProperties" for Azure ServiceSub Subscription in C#

我正在尝试动态配置 Azure ServiceBus 主题订阅(使用 C#)及其所有我们可以使用 Azure 门户设置的属性。

我已经尝试了下面的代码,但它在设置 SubscriptionProperties 的值时给我一个“对象引用错误”。

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

        }
}

请告诉我这是否是为“SubscriptionProperties”class 设置 属性 值的正确方法,或者我该怎么做?

我能够按照此 link https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample07_CrudOperations.md#create-a-topic-and-subscription

解决此问题
var client = new ServiceBusAdministrationClient(connectionString);
    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);

这可能对其他有类似疑问的读者有所帮助。