TimeWarpEngineering / timewarp-state

A Blazor State management library by TimeWarp.
The Unlicense
561 stars 56 forks source link

Mixed enviroment with plugins who use MediatR #372

Open phomberger opened 10 months ago

phomberger commented 10 months ago

First I would like to thank you for your work. Blazor-State is working like a charm and makes state management super easy. Now we have created a blazor application and integrated Blazor-State. This application is also using some custom created plugins from our company which are using MediatR for communication. As an example it´s using a command like this: public record UpdateSubProjectListCommand(IEnumerable<SubProjectDto> SubProjects) : IRequest;

and a handler:

 public class UpdateSubProjectListCommandHandler(IInboxRegistrationDao dao, IMapper mapper) : IRequestHandler<UpdateSubProjectListCommand>
 {
     public async Task Handle(UpdateSubProjectListCommand request, CancellationToken cancellationToken)
     {
         ...
     }
 }

Handler is called and communication with the own plugin is working. But I get a bunch of errors like this: Exception thrown: 'System.TypeLoadException' in System.Private.CoreLib.dll ("GenericArguments[0], 'InboxRegistration.Commands.UpdateSubProjectListCommand', on 'BlazorState.Pipeline.State.CloneStateBehavior2[TRequest,TResponse]' violates the constraint of type parameter 'TRequest'.") Exception thrown: 'System.TypeLoadException' in System.Private.CoreLib.dll ("GenericArguments[0], 'InboxRegistration.Commands.UpdateSubProjectListCommand', on 'BlazorState.Pipeline.State.CloneStateBehavior2[TRequest,TResponse]' violates the constraint of type parameter 'TRequest'.")

Then I tried to deactivate the UseCloneStateBehavior to false, but still exceptions are thrown. Can you please give me an advice what I can do? How can I prevent the Blazor-State clone behaviour for the other MediatR Handler which don´t implement the IAction interface?

Thanks and regards, Peter

StevenTCramer commented 10 months ago

hmm Are you on the latest version?

internal sealed class CloneStateBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
  where TRequest : notnull, IAction

this should only run if the Request implements IAction

phomberger commented 10 months ago

Yes, I´m using version 10.0 of Blazor-State

StevenTCramer commented 10 months ago

public record UpdateSubProjectListCommand(IEnumerable<SubProjectDto> SubProjects) : IRequest; this clearly isn't implementing IAction

so why is that exception happening, is strange.

Here is chatgpt converstaion https://chat.openai.com/c/11b293f5-087e-4eeb-8b8f-425384a64c48

also there is the mediatr issue https://github.com/jbogard/MediatR/issues/823

I don't see why you are getting this just yet.

phomberger commented 9 months ago

Hi Steven, thanks for your help. I can´t open your conversation with chatgpt. Also I don´t understand the behaviour. The other queries or commands don´t implement IAction at all. If this is a known bug of MediatR, then maybe it would be good to make a double check if something is really implementing IAction and just continue with the pipeline ...