为什么RegisterMessageHandler对于特定的主题名称不起作用?


9

我不明白为什么以下引用的以下处理程序(processMessageAsync)没有为特定主题名称触发,而为其他主题名称成功触发:

subscriptionClient.RegisterMessageHandler(processMessageAsync, msgOptions)

以下是我的订户类:

open System
open System.Linq
open System.Threading
open System.Text
open System.Threading.Tasks
open Microsoft.Azure.ServiceBus

type Subscriber(connectionString:string, topic:string, subscription:string) =

    let mutable subscriptionClient : SubscriptionClient = null

    let exceptionReceivedHandler (args:ExceptionReceivedEventArgs) =
        printfn "Got an exception: %A" args.Exception
        Task.CompletedTask

    let processMessageAsync (message:Message) (_:CancellationToken) = 

        try

            let _ = Encoding.UTF8.GetString(message.Body)
            subscriptionClient.CompleteAsync(message.SystemProperties.LockToken) |> Async.AwaitTask |> Async.RunSynchronously

            Task.CompletedTask

        with
            _ -> Task.CompletedTask

    member x.Listen() =

        async {

            subscriptionClient <- new SubscriptionClient(connectionString, topic, subscription)
            subscriptionClient.OperationTimeout <- TimeSpan.FromMinutes(3.0)

            let! rulesFound     = subscriptionClient.GetRulesAsync() |> Async.AwaitTask
            let  hasDefaultRule = rulesFound.Any(fun r -> r.Name = RuleDescription.DefaultRuleName)

            if hasDefaultRule then
                do! subscriptionClient.RemoveRuleAsync(RuleDescription.DefaultRuleName) |> Async.AwaitTask

            let msgOptions = MessageHandlerOptions(fun args -> exceptionReceivedHandler(args))
            msgOptions.AutoComplete         <- false
            msgOptions.MaxAutoRenewDuration <- TimeSpan.FromMinutes(1.0)
            msgOptions.MaxConcurrentCalls   <- 1

            subscriptionClient.RegisterMessageHandler(processMessageAsync, msgOptions)
        }

    member x.CloseAsync() =

        async {

            do! subscriptionClient.CloseAsync() |> Async.AwaitTask
        }

这是我尝试运行订阅服务器的方法

open System
open Subscription.Console

let connectionString = <connection_string>

[<EntryPoint>]
let main argv =

    printfn "Welcome to Subscription.Console"

    let topic,subscription = "Topic.courier-accepted","Subscription.all-messages"
    let subscriber = Subscriber(connectionString, topic, subscription)

    async { do! subscriber.Listen()
          } |> Async.RunSynchronously

    Console.ReadKey() |> ignore

    async { do! subscriber.CloseAsync()
          } |> Async.RunSynchronously

    0 // return an integer exit code 

以下代码发布了我的订户应该收到(但没有收到)的消息:

[<Fact>]
let ``Publish courier-accepted to servicebus``() =

    async {

        // Setup
        let  client    = TopicClient(sbConnectionstring, "Topic.courier-accepted")
        let! requestId = requestId()

        let updated = requestId |> modifyRequestId someCourierResponse
        let json    = JsonConvert.SerializeObject(updated)
        let message = Message(Encoding.UTF8.GetBytes(json))

        message.Label <- sprintf "request-id(%s)" (requestId.ToString())

        // Test
        do! client.SendAsync(message) |> Async.AwaitTask

        // Teardown
        do! client.CloseAsync()       |> Async.AwaitTask
    }

注意:

上面的代码有趣的是,当我运行的Azure Function的ServiceBusTrigger设置为相同的主题和订阅名称时,每次运行测试都会触发Azure Function。

  • 我没有收到任何异常消息
  • 我的订户实例上永远不会触发exceptionReceivedHandler函数
  • 我在Azure仪表板上没有发现任何针对Servicebus资源的用户错误

成功使用不同的主题名称

如果我将主题名称更改为“ courier-requested”,则订阅者实例将收到消息:

[<Fact>]
let ``Publish courier-requested to servicebus topic``() =

    // Setup
    let client    = TopicClient(sbConnectionstring, "Topic.courier-requested")
    let message   = Message(Encoding.UTF8.GetBytes(JsonFor.courierRequest))
    message.Label <- sprintf "courier-id(%s)" "b965f552-31a4-4644-a9c6-d86dd45314c4"

    // Test
    async {

        do! client.SendAsync(message) |> Async.AwaitTask
        do! client.CloseAsync()       |> Async.AwaitTask
    }

这是具有主题名称调整的订阅:

[<EntryPoint>]
let main argv =

    printfn "Welcome to Subscription.Console"

    let topic,subscription = "Topic.courier-requested","Subscription.all-messages"
    let subscriber = Subscriber(connectionString, topic, subscription)

    async { do! subscriber.Listen()
          } |> Async.RunSynchronously

    Console.ReadKey() |> ignore

    async { do! subscriber.CloseAsync()
          } |> Async.RunSynchronously

    0 // return an integer exit code

这是我的Azure门户中的两个主题: 在此处输入图片说明

在门户网站中单击主题具有不同的结果:

我注意到我必须单击两次“接受快递”才能查看其订阅。但是,我可以单击“快递要求”,然后立即查看其订阅。


1
I don't receive any exception messages,也许是因为您吞下了异常?我看到一个with _块后try一个
user1623521

在为速递接受的主题创建其他订阅,使用与我刚在门户中注册的订阅值匹配的订阅值启动订阅者,然后在订阅者仍在运行时删除最近创建的订阅之后,我确实观察到异常。
Scott Nimrod

我无法重现您的问题,似乎您在服务总线门户上遇到了不正常的行为。也许您可以向SB团队提交支持票。
周杰伦

我昨天提交了票。
Scott Nimrod

Answers:


0

如果我正确理解您尝试删除有问题的主题并重新创建它,那对我来说听起来像是在Azure中打ic。您不应该得到上面描述的需要单击两次的行为。有时,我是在Azure中创建事物的,其基础结构的下游存在问题,而支持请求是解决该问题的唯一方法。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.