Skip to content

SourceGenerator generates an error for IStreamRequest with structs #36

@Tornhoof

Description

@Tornhoof

Hi,
I tried converting one of my MediatR solutions to your source-gen approach and noticed the following:

The srcgen generates the following error for IStreamRequest with structs in 2.0.15-preview

Error	CS0266	Cannot implicitly convert type 'System.Collections.Generic.IAsyncEnumerable<MyStruct>' to 'System.Collections.Generic.IAsyncEnumerable<object>'. An explicit conversion exists (are you missing a cast?)	ConsoleApp7	

Repro:

using Mediator;

public class Program
{
	public static int Main(string[] args)
	{
		return 0;
	}
}

public sealed class MyRequest : IStreamRequest<MyStruct>
{
}

public sealed class MyRequestHandler : IStreamRequestHandler<MyRequest, MyStruct>
{
	public IAsyncEnumerable<MyStruct> Handle(MyRequest request, CancellationToken cancellationToken)
	{
		throw new NotImplementedException();
	}
}

public readonly struct MyStruct
{

}

The generated code snippet is:

        /// <summary>
        /// Create stream.
        /// Throws <see cref="global::System.ArgumentNullException"/> if message is null.
        /// Throws <see cref="global::Mediator.MissingMessageHandlerException"/> if no handler is registered.
        /// </summary>
        /// <param name="message">Incoming message</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <returns>Async enumerable</returns>
        public global::System.Collections.Generic.IAsyncEnumerable<object> CreateStream(
            object message,
            global::System.Threading.CancellationToken cancellationToken = default
        )
        {
            switch (message)
            {
                case global::MyRequest m: return CreateStream(m, cancellationToken);
                default:
                {
                    ThrowArgumentNullOrInvalidMessage(message as global::Mediator.IStreamMessage, nameof(message));
                    return default;
                }
            }
            
        }

Workaround: use class instead of struct

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions