IMessenger.Send behaviour

Abierto
#1,042 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
28/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
csharp

Línea de trabajo

Empieza leyendo la API IMessenger.Send y el comportamiento de AsyncRequestMessage que se usan en los ejemplos. Compara los casos Task y Task, y después revisa las pruebas o la documentación existentes sobre la semántica de finalización de los mensajes de solicitud. Se considera terminado cuando el comportamiento está confirmado y documentado explícitamente, o cuando se acuerda un cambio de API específico con pruebas que describan el comportamiento de finalización previsto.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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

Lenguaje dominante
C#
Estrellas
3.8k
Forks
402
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de CommunityToolkit/dotnet

Todos los issues de CommunityToolkit/dotnet

Issues similares

Más issues de C#

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.