【问题标题】:Send message to azure service bus with properties将消息发送到具有属性的 azure 服务总线
【发布时间】:2021-06-23 10:32:49
【问题描述】:

我正在使用以下 powershell 代码向具有属性集的 Azure 服务总线主题发送消息:

function Send-AzServiceBusMessage {
    param(
        [Parameter(Mandatory = $true)]
        [string] $ResourceGroupName,
        [Parameter(Mandatory = $true)]
        [string] $NamespaceName,
        [Parameter(Mandatory = $true)]
        [string] $TopicName,
        [Parameter(Mandatory = $false)]
        [string] $PolicyName = 'RootManageSharedAccessKey',
        [Parameter(Mandatory = $false)]
        [string] $Property1
    )
      
    $message = [PSCustomObject] @{ "Body" = "Test message"; "Property1" = $Property1 }        
    $namespace = (Get-AzServiceBusNamespace -ResourceGroupName $ResourceGroupName -Name $namespacename).Name
    $key = (Get-AzServiceBusKey -ResourceGroupName $ResourceGroupName -Namespace $namespacename -Name $PolicyName).PrimaryKey
    $message.psobject.properties.Remove("Body")
    $token = New-AzServiceBusSasToken -Namespace $namespace -Policy $PolicyName -Key $Key
    
    #set up the parameters for the Invoke-WebRequest
    $headers = @{ "Authorization" = "$token"; "Content-Type" = "application/atom+xml;type=entry;charset=utf-8" }
    $uri = "https://$namespace.servicebus.windows.net/$TopicName/messages"
    $headers.Add("BrokerProperties", $(ConvertTo-Json -InputObject $Message -Compress))
        $result = Invoke-WebRequest -Uri $uri -Headers $headers -Method Post -Body $body 
    if ($result.StatusCode -ne 201) {
        $result
    }

我在主题订阅上设置了一条规则,这样: Property1 in ('somevalue')

但是,如果我设置了一个不带规则的全部订阅,我可以看到消息是由它接收的,而不是由具有规则的订阅接收的。所以我的问题是如何发送带有使用 powershell 设置的属性的消息。在 C# 中与此类似:

            var message = new BrokeredMessage { MessageId = Guid.NewGuid().ToString()};
            message.Properties["Property1"] = "somevalue";

【问题讨论】:

    标签: powershell azureservicebus


    【解决方案1】:

    基于the documentation,您似乎必须将自定义属性嵌套在Properties 成员中:

    $headers.Add("BrokerProperties", $(ConvertTo-Json -InputObject @{ Properties = $message } -Compress))
    

    【讨论】:

      【解决方案2】:

      解决方案同上,但有以下修改:

      $message = [PSCustomObject] @{ "Body" = "Test message" }
      $headers = @{ "Authorization" = "$token"; "Content-Type" = "application/atom+xml;type=entry;charset=utf-8"; "Property1" = $Property1  }
      $headers.Add("BrokerProperties", $(ConvertTo-Json -InputObject $Message -Compress))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-28
        • 1970-01-01
        • 1970-01-01
        • 2021-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-30
        相关资源
        最近更新 更多