執行期例外

找不到 handler — InvalidOperationException

當分派一個沒有註冊 handler 的 request/command/query/stream 時丟出(對 stream 來說, 是在開始列舉時)。訊息裡會指名缺少的那個具體註冊呼叫:

Davish.Sendr: no handler is registered for '<requestType>'. Call services.<registrationMethod><<requestType>, YourHandler>() during startup.

或者,對有回應型別的 request/command/query 而言:

Davish.Sendr: no handler is registered for '<requestType>' with response '<responseType>'. Call services.<registrationMethod><<requestType>, <responseType>, YourHandler>() during startup.

<registrationMethod> 會是 AddRequestHandler/AddCommandHandler/ AddQueryHandler/AddStreamRequestHandler 裡,符合該訊息種類的那一個。

covariant 轉型之後

IRequest<TResponse>、ICommand<TResponse>、IQuery<TResponse>、 IStreamRequest<TResponse> 都是 covariant,所以下面這段可以編譯:

IQuery<object> query = new StringQuery(); // StringQuery : IQuery<string>
await sender.SendAsync(query, cancellationToken);

查 handler 時用的是訊息的執行期型別加上靜態的 response 型別,這裡是 (StringQuery, object), 所以找不到 StringQuery 的 handler。這時手動與 generated 兩種 sender 都會指出是轉型造成的, 不會建議註冊 handler:

Davish.Sendr: no handler is found for '<requestType>' with response 'System.Object': '<RequestType>' declares IQuery<String>, not IQuery<Object>. It was sent as IQuery<Object> (a covariant upcast), so the handler was looked up with 'Object' as the response type. Send it as '<RequestType>' or as an interface it declares.

要改的是呼叫端:用訊息本身的型別,或它宣告的介面送出。再註冊一個回傳 object 的 handler 解決不了問題。

重複註冊 — InvalidOperationException

當透過 AddRequestHandler、AddCommandHandler、AddQueryHandler,或 AddStreamRequestHandler 為同一個 request/response 組合註冊第二個 handler 時丟出; 另外,當同一個 notification 型別的 AddNotificationHandler 被呼叫超過一次時,也會丟出。

Notification 發佈失敗

IPublisher.PublishAsync(不論手動或產生出來的)會聚合每一個實際執行過的 handler 的 失敗,而不是只顯示第一個:

兩個群組加起來的失敗數結果
0不會丟出例外。
1該例外會原樣重新丟出,並保留原始的堆疊追蹤。
2 以上會合併成一個包含全部例外的 AggregateException。

背後的執行語意見Notification 模型。

有 tracing listener 訂閱時的同步例外

當有 listener 訂閱了 Davish.Sendr 這個 ActivitySource 時,handler 同步丟出的例外 (在它回傳 Task 之前就丟出),會透過回傳的 Task 傳給呼叫端,而不是由 SendAsync/PublishAsync 本身丟出。不論哪一種,await 這個呼叫的程式碼看到的都是同一個 例外。沒有 listener 時,行為不變。見 Tracing。

編譯期診斷(Davish.Sendr.Generators)

ID嚴重性標題什麼時候會發生
SENDR001ErrorInvalid [DecorateWith] usage[DecorateWith<...>] 的某個型別參數,沒有實作該 handler 所屬的訊息種類所需的 decorator 介面。
SENDR002ErrorMultiple handlers registered for the same request有超過一個被發現的 handler(本地或來自某個被納入的 assembly)指向同一個 request/response 組合。回報在本專案宣告的 handler 上(另一個也在本專案時,列為 additional location);兩個都來自被納入的 assembly 時,回報在引入第二個 handler 的 IncludeAssemblyOf<...>() 呼叫上。不適用於 notification handler。
SENDR003InfoOpen generic handler is not discovered發現了一個 open generic 的 handler 類別並跳過;請改用手動註冊一個封閉建構的型別。
SENDR004ErrorValue type cannot be a Davish.Sendr handler某個 handler 宣告成 struct/record struct。Handler 解析要求是參考型別,請改用 class/record class。
SENDR005ErrorUnsupported UseGenerators() configuration某個 IncludeAssemblyOf<...>() 呼叫沒有直接寫在 UseGenerators(g => ...) 的 lambda 裡(例如放在 delegate 變數、method group、迴圈、條件式,或包一層 wrapper helper/extension method),source generator 無法從原始碼讀出它。
SENDR006ErrorInconsistent UseGenerators() assembly configuration同一次編譯裡,兩個以上針對同一個 sender 或同一個 publisher 的 UseGenerators() 呼叫,宣告了不同的 IncludeAssemblyOf<...>() 集合。
SENDR007ErrorExternal handler or decorator is not accessible透過 IncludeAssemblyOf 發現的 handler/decorator,呼叫端專案無法存取(不是 public,也沒有對應的 [assembly: InternalsVisibleTo("...")])。

這些診斷所保護的發現模型,見編譯期發現; 設定步驟見啟用 source generation。