question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

static object reference issue using Query in Controller

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
iyamuscommented, Jun 7, 2021

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?

    public class OrgController : ControllerBase
    {
        private readonly IMediator _mediator;

        public OrgController(IServices services, IMediator mediator)
        {
            _mediator = mediator;
        }

        [HttpGet]
        public async Task<ActionResult<List<OrgDTO>>> GetAllOrg()
        {
            return await _mediator.Send(new OrgList.Query());
        }

0reactions
iyamuscommented, Jun 8, 2021

@djeikyb @neozhu Thank you for your comments. It’s helpful. Have a great day!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found