Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Reflection.TargetException In Queuing Events #80

Closed
khabubu opened this issue Mar 30, 2019 · 12 comments
Closed

System.Reflection.TargetException In Queuing Events #80

khabubu opened this issue Mar 30, 2019 · 12 comments
Assignees
Labels
bug Something isn't working

Comments

@khabubu
Copy link

khabubu commented Mar 30, 2019

Describe the bug
I am trying to queue an event with the following line

_queue.QueueBroadcast(new PurchaseCreatedEvent(organisation, purchase));

When the event is broadcasted the following exception is raised

System.Reflection.TargetException
Message : Non-static method requires a target.
StackTrace :    at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Coravel.Events.Dispatcher.Broadcast[TEvent](TEvent toBroadcast)
   at Coravel.Queuing.Queue.<>c__DisplayClass10_0`1.<<QueueBroadcast>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Coravel.Tasks.ActionOrAsyncFunc.Invoke()
   at Coravel.Queuing.Queue.InvokeTask(ActionOrAsyncFunc task)
Help Link : 
Source : System.Private.CoreLib

Affected Coravel Feature
The Coravel Feature is Queuing

Expected behaviour
I am expecting that when the queue starts the listeners of the broadcasted event handle the event. I am using Coravel with ASP NET Core 2.1.4

@jamesmh
Copy link
Owner

jamesmh commented Mar 30, 2019

Thanks for all the details! I've run into this before so I know where it's happening but will need to figure out why....

Could you share the general shape of your PurchaseCreatedEvent? (just the method signatures)

Also:

  • is the PurchaseCreatedEvent in the same project as your web or executable project?
  • if not, what type of project is it (.NET Standard 2.0?, etc.)

@khabubu
Copy link
Author

khabubu commented Mar 30, 2019

Thank you for the quick replay. The shape of the PurchaseCreatedEvent is

public class PurchaseCreatedEvent : IEvent
    {
        public Organisation Organisation { get; set; }

        public Purchase Purchase { get; set; }

        public PurchaseCreatedEvent(Organisation organisation, Purchase purchase)
        {
            Organisation = organisation;
            Purchase = purchase;
        }
    }
  1. It is in the same project.

@jamesmh
Copy link
Owner

jamesmh commented Mar 30, 2019

What do your listeners look like? Are they in the same project also?

@khabubu
Copy link
Author

khabubu commented Mar 30, 2019

The listener(s) are in the same project as illustrated below

public class PurchaseCreatedListener : IListener<PurchaseCreatedEvent>
    {
        private readonly ApplicationDbContext _context;

        public PurchaseCreatedListener(ApplicationDbContext context)
        {
            _context = context;
        }

        public async Task HandleAsync(PurchaseCreatedEvent broadcasted)
        {
            // Ommitted lines for brevity.
        }
    }

I am not sure if I have configured events and queues below as show below

var provider = app.ApplicationServices;
var registration = provider.ConfigureEvents();
registration.Register<PurchaseCreatedEvent>()
    .Subscribe<PurchaseCreatedListener>();
provider.ConfigureQueue().LogQueuedTaskProgress(provider.GetService<ILogger<IQueue>>())
    .OnError(e => { // Ommited for brevity });

I hope I have provide enough information.

@jamesmh
Copy link
Owner

jamesmh commented Mar 30, 2019

Awesome. I forgot to ask (sorry!) - what version of Coravel are you using?

@khabubu
Copy link
Author

khabubu commented Mar 30, 2019

Version 2.3.1

@jamesmh
Copy link
Owner

jamesmh commented Mar 30, 2019

Great. I'll try to get to this in the new couple days.

@jamesmh jamesmh self-assigned this Mar 30, 2019
@jamesmh jamesmh added the bug Something isn't working label Mar 30, 2019
@jamesmh
Copy link
Owner

jamesmh commented Mar 31, 2019

Quick question: Did you register your listeners with AddTransient or AddScoped?

@khabubu
Copy link
Author

khabubu commented Mar 31, 2019

No I didn't register with either. I think that is my issue. Let me try registering it. Ohh is this the proper way to register a listener

services.AddTransient<IListener<PurchaseCreatedEvent>, PurchaseCreatedListener>();

I had registered the listeners and events as follow

var provider = app.ApplicationServices;
var registration = provider.ConfigureEvents();
registration.Register<PurchaseCreatedEvent>()
                .Subscribe<PurchaseCreatedListener>();

@jamesmh
Copy link
Owner

jamesmh commented Apr 1, 2019

You can just do services.AddTransient<PurchaseCreatedListener>() - does that fix your issues?

@khabubu
Copy link
Author

khabubu commented Apr 1, 2019

Yes it does. Thank you very much.
PS : I feel stupid now. Thanks again

@jamesmh
Copy link
Owner

jamesmh commented Apr 1, 2019

hahaha! Don't worry about it. Glad everything is working now 👍

@jamesmh jamesmh closed this as completed Apr 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants