How can I call MediatR handlers outside the api project of my app?

30 Views Asked by At

I have a handler which I want mediator to invoke it outside .net runtime, here is my sample code of what I have in mind:

public class SageSync
{


    public async Task ExecuteSync()
    {
         //I want this method to execute my mediator handler
          await mediator.Send(new SupplierSyncRequest());
        
    }
}

The code for my handler:

public class SupplierSyncHandler : BaseHandler<SupplierSyncRequest, SupplierSyncResponse>
{

    public SupplierSyncHandler(IUnitOfWork uow, IHttpContextAccessor httpContextAccessor) : base(uow, httpContextAccessor) { }


    public async override Task<SupplierSyncResponse> Handle(SupplierSyncRequest request, CancellationToken cancellationToken)
    {
        // If validation succeeds, continue with the actual handler logic
        //request.SageSupplierGuid = Guid.NewGuid();
        var worker = new SageWorker();
        var result = await worker.GetData<SageSupplier>("2020/1/1", WebService.Suppliers);

        await _SageSupplierWriteRepository.Create(result);

        var log = CreateKadupulLog(OperationType.Create, "SageSuppliers Created");
        await _KadupulLogWriteRepository.Create(log);
        await _uow.SaveAsync();

        // Create and return the successful response
        _SupplierSyncResponse.Data = true;
        return _SupplierSyncResponse;
    }
}

I've heard couple of terms like scopedServices and registering the mediator somewhere but didn't fully understand what they meant

0

There are 0 best solutions below