如何更新 Azure 通知中心注册中的过期时间?

How to update Expiration Time in Azure Notification Hub registration?

我使用 Azure 通知中心已有一段时间了。但是,我为一个新项目创建了一个新的通知中心,我注意到了一些非常奇怪的行为。每当我创建注册时,它的 ExpirationDate 设置为 12/31/9999 7:59:59.

所以,对于某些人来说,我想这可能是一个好处,但我想在一段时间不活动后让我的到期。我查看了 RegistrationDescription 对象并找到了 ExpirationTime 但它是只读的...

如何设置?这只是 Azure 中的一个错误吗?也许我在 Azure 配置中缺少一个标志?

您可以这样做,但在集线器级别,而不是在注册级别。查看 Improved Per Message Telemetry and device expiry for Notification Hubs 博客 post:

To take advantage of this expiry change, simply update your notification hub’s Time To Live property. This can be done through REST or our .NET SDK:

var namespaceManager = NamespaceManager.CreateFromConnectionString("connectionstring");
NotificationHubDescription hub = namespaceManager.GetNotificationHub("foo");
hub.RegistrationTtl = TimeSpan.MaxValue;
namespaceManager.UpdateNotificationHub(hub);

要通过 REST API 执行此操作,请查看 Update Notification Hub method, which takes NotificationHubDescription 正文,其中有一个 RegistrationTtl 节点。这应该是上面 SDK 代码片段的 REST 等价物。

文档已过时,我必须向 Microsoft 开票才能在 2020 年完成此操作。

我创建了一个控制台应用程序并添加了以下 nuget 包 -

https://www.nuget.org/packages/Microsoft.Azure.Management.NotificationHubs https://www.nuget.org/packages/Microsoft.Azure.Management.ResourceManager.Fluent/

Install-Package Microsoft.Azure.Management.NotificationHubs -Version 2.3.2-preview

Install-Package Microsoft.Azure.Management.ResourceManager.Fluent -Version 1.34.0

然后我写了这个方法-

    private async Task SetNotificationHubRegistrationTimeToLive()
    {
        // Login to Azure using az login
        // az account set -s <name or ID of subscription> to set the proper subscription
        // Get credentials: "az ad sp create-for-rbac --sdk-auth"
        // See https://docs.microsoft.com/en-us/cli/azure/get-started-with-azure-cli?view=azure-cli-latest and https://docs.microsoft.com/en-us/azure/cloud-shell/quickstart

        var clientId = "ec1b...";
        var clientSecret = "oJJ6...";
        var tenantId = "2b86...";

        var credentials =
                SdkContext
                .AzureCredentialsFactory
                .FromServicePrincipal(
                    clientId,
                    clientSecret,
                    tenantId,
                    AzureEnvironment.AzureGlobalCloud);

        var client = new NotificationHubsManagementClient(credentials)
        {
            SubscriptionId = "yoursubscriptionid"
        };

        var resourceGroupName = "yourgroup";
        var namespaceName = "yournamespace"; // this should NOT be the namespace full name beam-dev-notification-hub-namespace-free.servicebus.windows.net
        var notificationHubName = "yournotificationhub";

        var timeSpan = new TimeSpan(days: 90, hours: 0, minutes: 0, seconds: 0);
        var registrationTtlTimeSpanString = timeSpan.ToString();

        var notificationHub = await client.NotificationHubs.GetAsync(resourceGroupName, namespaceName, notificationHubName);

        await client
              .NotificationHubs
              .CreateOrUpdateAsync(
                resourceGroupName,
                namespaceName,
                notificationHubName,
                new NotificationHubCreateOrUpdateParameters(notificationHub.Location)
                {
                    RegistrationTtl = registrationTtlTimeSpanString
                });
    }

然后您将在 https://portal.azure.com/ 通知中心属性中看到 -