jbogard / MediatR.Extensions.Microsoft.DependencyInjection

MediatR extensions for Microsoft.Extensions.DependencyInjection
MIT License
326 stars 90 forks source link

Cannot access a disposed object.\r\nObject name: 'IServiceProvider'. #99

Closed sc-cpais closed 2 years ago

sc-cpais commented 3 years ago

Hi everyone,

we are building a project with GraphQL (hot chocolate) + Mediator. The queries on graphql are served by the service layer directly and the mutations are service my mediator:

[GraphQLDescription("Represents the queries available.")] public class Query { public IList<ProjectsListDto> GetProjects([Service] IProjectQueryService projectService) { return projectService.GetProjects(default, default); } }

` public class Mutation { private readonly IMediator mediator;

    public Mutation(IMediator mediator)
    {
        this.mediator = mediator;
    }
    public async Task<ProjectEnergyDto> AddProjectEnergy(CreateEnergyProjectCommand model)
    {
        return await mediator.Send(model);
    }
    public async Task<ProjectEnergyDto> UpdateProjectEnergy(UpdateEnergyProjectCommand model)
    {
        return await mediator.Send(model);
    }
}

This is my UpdateEnergyProjectCommand public class UpdateEnergyProjectCommand : IRequest { [Required] public int ID{ get; set; } [Required] public string Name { get; set; } [Required] public string Description { get; set; } public Enums.Countries[] Countries { get; set; } public Enums.ProjectAssets[] ProjectAssets { get; set; } public Enums.CurrentStandardVersion CurrentStandardVersion { get; set; } public Enums.CarbonStream CarbonStream { get; set; } } `

ant this is my UpdateEnergyProjectHandler

` public class UpdateEnergyProjectHandler : IRequestHandler<UpdateEnergyProjectCommand, ProjectEnergyDto> { private readonly IProjectQueryService projectQueryService; private readonly IEnergyProjectRepository energyProjectRepository; private readonly IMapper mapper;

    public UpdateEnergyProjectHandler(IEnergyProjectRepository energyProjectRepository,
                                      IMapper mapper) 
    {
        this.energyProjectRepository = energyProjectRepository;
        this.mapper = mapper;
    }
    public Task<ProjectEnergyDto> Handle(UpdateEnergyProjectCommand request, CancellationToken cancellationToken)
    {
        var project = energyProjectRepository.GetByIdAsync(request.ID).Result;
        if (project != null)
        {
            mapper.Map<UpdateEnergyProjectCommand, ProjectEnergy>(request, project);
            energyProjectRepository.UpdateAsync(project);
            project = energyProjectRepository.GetByIdAsync(request.ID).Result;
            return Task.FromResult(mapper.Map<ProjectEnergy, ProjectEnergyDto>(project));
        }
        return null;

warning manage not found exception

    }

`

When I run the code, everything runs well. But when I run it right after I get the following exception: Cannot access a disposed object.\r\nObject name: 'IServiceProvider'.

I have tried this solution but it didn't worked for me.

"extensions": { "message": "Cannot access a disposed object.\r\nObject name: 'IServiceProvider'.", "stackTrace": " at Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException()\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)\r\n at MediatR.ServiceFactoryExtensions.GetInstances[T](ServiceFactory factory)\r\n at MediatR.Internal.RequestHandlerWrapperImpl2.Handle(IRequest1 request, CancellationToken cancellationToken, ServiceFactory serviceFactory)\r\n at MediatR.Mediator.Send[TResponse](IRequest1 request, CancellationToken cancellationToken)\r\n at SC.Certification.API.GraphQL.Mutation.UpdateProjectEnergy(UpdateEnergyProjectCommand model) in C:\sources\SC.Certification\SC.Certification.API\GraphQL\Mutation.cs:line 25\r\n at HotChocolate.Resolvers.Expressions.ExpressionHelper.AwaitTaskHelper[T](Task1 task)\r\n at HotChocolate.Types.FieldMiddlewareCompiler.<>c__DisplayClass3_0.<<CreateResolverMiddleware>b__0>d.MoveNext()\r\n--- End of stack trace from previous location ---\r\n at HotChocolate.Execution.Processing.ResolverTask.ExecuteResolverPipelineAsync(CancellationToken cancellationToken)\r\n at HotChocolate.Execution.Processing.ResolverTask.TryExecuteAsync(CancellationToken cancellationToken)" }

Can somebody help here? Thank you

sylvainonweb commented 3 years ago

Hi @sc-cpais , I have the same issue. Did you find a way to resolve it? Thanks in advance

mohammedbabelly commented 3 years ago

Same issue here!

sc-cpais commented 3 years ago

Hi @sylvainonweb @mohammedbabelly

I end up finding the solution and forgot to update the issue. Thank you for commenting. In Hot chocolate, we receive the injected service by method parameter. Something like this:

public async Task<ProjectEnergyDto> AddProjectEnergy([Service]ISender mediator, CreateEnergyProjectCommand model) { return await mediator.Send(model); }

Hope that can help you.

mohammedbabelly commented 3 years ago

Hi @sylvainonweb @mohammedbabelly

I end up finding the solution and forgot to update the issue. Thank you for commenting. In Hot chocolate, we receive the injected service by method parameter. Something like this:

public async Task<ProjectEnergyDto> AddProjectEnergy([Service]ISender mediator, CreateEnergyProjectCommand model) { return await mediator.Send(model); }

Hope that can help you.

Thank you so much, sir.