Tôi không hiểu tại sao trình xử lý sau ( processMessageAsync ) được tham chiếu bên dưới không được kích hoạt cho một tên chủ đề cụ thể nhưng thành công cho các tên chủ đề khác:
subscriptionClient.RegisterMessageHandler(processMessageAsync, msgOptions)
Sau đây là lớp thuê bao của tôi :
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
}
Đây là cách tôi cố gắng để chạy thuê bao :
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
Đoạn mã sau xuất bản một thông báo mà thuê bao của tôi sẽ nhận được (nhưng không):
[<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
}
GHI CHÚ:
Điều thú vị về mã ở trên là khi tôi có Chức năng Azure chạy với ServiceBusTrigger được đặt cùng chủ đề và tên đăng ký, Chức năng Azure được kích hoạt mỗi khi tôi chạy thử nghiệm.
- Tôi không nhận được bất kỳ tin nhắn ngoại lệ
- Hàm ngoại lệReceuredHandler không bao giờ được kích hoạt trong trường hợp Người đăng ký của tôi
- Tôi không quan sát thấy bất kỳ lỗi người dùng nào trên bảng điều khiển Azure của tôi đối với tài nguyên của bus dịch vụ
Thành công với tên chủ đề khác nhau
Nếu tôi thay đổi tên chủ đề thành "yêu cầu chuyển phát nhanh" thì đối tượng thuê bao nhận được tin nhắn:
[<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
}
Đây là thuê bao có điều chỉnh tên chủ đề:
[<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
Đây là hai chủ đề trong Cổng thông tin Azure của tôi:
Nhấp vào Chủ đề trong Cổng thông tin có kết quả khác nhau:
Tôi nhận thấy rằng tôi phải nhấp vào "chấp nhận chuyển phát nhanh" hai lần chỉ để xem đăng ký của nó. Tuy nhiên, tôi có thể nhấp vào "yêu cầu chuyển phát nhanh" một lần và ngay lập tức xem đăng ký của nó.
I don't receive any exception messages
, có lẽ bởi vì bạn đang nuốt những ngoại lệ? Tôi nhìn thấy mộtwith _
khối sau mộttry
một