IMessenger.Send behaviour

オープン
#1,042 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
28/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
csharp

調査の方向性

まず、例で使用されている IMessenger.Send API と AsyncRequestMessage の動作を読みます。Task と Task のケースを比較し、その後、リクエストメッセージの完了セマンティクスに関する既存のテストまたはドキュメントを確認します。動作が明示的に確認されて文書化されるか、意図した完了動作を記述するテストとともに、焦点を絞った API の変更が合意されれば完了です。

索引モデルが issue の本文から書いたものです。

説明

bug :bug:
Describe the bug

Service registration:
AddSingleton<IMessenger>(WeakReferenceMessenger.Default)

Message registration:
_messenger.Register<AsyncRelayCommandPageViewModel, TestAsyncMessage1>(this, static (r, m) => m.Reply(r.Action1(m)));

Behavour:

Case1:
If Action1 is of type Task<T> and we do following:

string response = await _messenger.Send<TestAsyncMessage1>(new());

private async Task<string> Action1(TestAsyncMessage1 msg)
{
    await Task.Delay(TimeSpan.FromSeconds(5));
    Trace.WriteLine("Action1 process complete");
    return "hello";
}

the response is of type string and contains the value hello.

Case2:
However, if Action1 is of type Task and we do following:

Task response = await _messenger.Send<TestAsyncMessage1>(new());

private async Task Action1(TestAsyncMessage1 msg)
{
    await Task.Delay(TimeSpan.FromSeconds(5));
    Trace.WriteLine("Action1 process complete");
}

the response is of type Task.

Issue:
In Case1 does the await actually awaits the internal task (Action1) and return the value of the task, in this case the string hello.
In Case2 does the await awaits that the message is sent, but not the internal task (Action1).

So if we want the same behaviour for Case2, where we want the internal task to be completed, we have to do something like:

Task response = await _messenger.Send<TestAsyncMessage1>(new());
await response;

We are now implementation our own solution, using an extension method:

public static class ExtensionMethods
{
    public static async Task ProcessTask<TMessage>(this IMessenger messenger, TMessage message)
        where TMessage : AsyncRequestMessage<Task>
        {
            var task = await messenger.Send(message);
            await task;
        }

    public static Task ProcessTask<TMessage>(this IMessenger messenger)
        where TMessage : AsyncRequestMessage<Task>, new()
        => messenger.ProcessTask(new TMessage());
}

Making it possible to do following:

await _messenger.ProcessTask<TestAsyncMessage1>(new());

Description
Depening on the return type, we have different behavour.
Is this wanted? Would it make sense to add something like ProcessTask to the library to extend the functionality?

Regression

No response

Steps to reproduce

Microsoft Visual Studio Professional 2022 (64-bit) - Current
Version 17.12.4

CommunityToolkit.Mvvm
Version 8.2.2

Expected behavior

See description of bug.

Screenshots

No response

IDE and version

VS 2022

IDE version

Version 17.12.4

Nuget packages
  • CommunityToolkit.Common
  • CommunityToolkit.Diagnostics
  • CommunityToolkit.HighPerformance
  • CommunityToolkit.Mvvm (aka MVVM Toolkit)
Nuget package version(s)

8.2.2

Additional context

No response

Help us help you

Yes, I'd like to be assigned to work on this item

主要言語
C#
スター
3.8k
フォーク
402
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

CommunityToolkit/dotnet のほかの issue

CommunityToolkit/dotnet の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。