static object reference issue using Query in Controller
See original GitHub issueI am a newbie in CQRS / MediatR, so tried to figure out myself with the demo. I implemented Query and Handler in a class, those classes will be read in Controller. However, I have struggled with the error in Controller
, so tried to way#1 and way#2 but didn’t get resolved. Can you let me know what I made mistake here?
My dev environment is .NET Core 3.1 and MedatR version 9
Query/ Handler
using Application.DTO;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Persistence;
using Persistence.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Application.Handlers
{
public class OrgList
{
public class Query : IRequest<List<OrgDTO>>
{
}
public class Handler : IRequestHandler<Query, List<OrgDTO>>
{
private readonly BlogContext _context;
private readonly IMapper _mapper;
public Handler(BlogContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
public async Task<List<OrgDTO>> Handle(Query request, CancellationToken cancellationToken)
{
var organizations = await _context.Organizations
.Where(o => o.activation == true)
.Include(o => o.Employees)
.ToListAsync();
List<OrgDTO> result = _mapper.Map<List<Organization>, List<OrgDTO>>(organizations);
return result;
}
}
}
}
Controller__way#1
...
[HttpGet]
public async Task<ActionResult<List<OrgDTO>>> GetAllOrg()
{
return await Mediator.Send(new OrgList.Query); //<--- Error occurs here.
}
...
Error Message_way#1
Severity Code Description Project File Line Suppression State
Error CS1526 A new expression requires an argument list or (), [], or {} after type
Controller_way#2
[HttpGet]
public async Task<ActionResult<List<OrgDTO>>> GetAllOrg()
{
return await Mediator.Send(new OrgList.Query { }); //<--- Error occurs here.
// return await Mediator.Send(new OrgList.Query()); //<--- same result
}
Error Message_way#2
Severity Code Description Project File Line Suppression State
Error CS0120 An object reference is required for the non-static field, method, or property 'Mediator.Send<List<OrgDTO>>(IRequest<List<OrgDTO>>, CancellationToken'
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
Top Results From Across the Web
Controller Error "An object Reference is required for the ...
Controller Error "An object Reference is required for the non-static field" is being thrown when trying to call interface Service [duplicate].
Read more >C# Error: How to Resolve an object reference is required ...
In order to use a non-static field, method, or property, you must first create an object instance. Consider the following C# example. namespace ......
Read more >Object Reference Not Set to an Instance of an Object
This exception is thrown when you try to access a member—for instance, a method or a property—on a variable that currently holds a...
Read more >A brand new website interface for an even better experience!
static object reference issue using Query in Controller.
Read more >Still having big problems with Object reference not set to an ...
The error means that your code is trying to access/reference an object that is a null valued object that is not there in...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Well, I found a way to create a constructor with IMediator DI that works fine. But is this a normal way to use a constructor in the Controller? Or is there another way, such as calling Mediator directly in the Controller as I tried before?
@djeikyb @neozhu Thank you for your comments. It’s helpful. Have a great day!