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.

Decorator without any registrations on decorated type causes MissingTypeRegistrationException

See original GitHub issue


I am using the following code:

services.Scan(a =>
        .AddClasses(p => p.AssignableTo(typeof(IQueryHandler<,>)))

services.Decorate(typeof(IQueryHandler<,>), typeof(LoggingQueryHandlerDecorator<,>));

This registers all IQueryHandlers in my project and decorates each of them with a decorator.

I am currently setting up a project. It does not yet contain any queryhandlers, but I of course would like to already set up all DI registrations, so when I do add queryhandlers things work just fine. Also, if I have a project “template” that does not necessary contain queryhandlers by default but does support them, having the code above in the project would be a great benefit instead of having to add it later.

I would expect that if the project has no queryhandlers registered, no decorations would be applied. Things would work just fine. But this is not the case.

Right now, when I run this code the following exception is thrown on the line of the decorator.

Scrutor.MissingTypeRegistrationException: 'Could not find any registered services for type IQueryHandler<TQuery, TResult>'.'

Perhaps this should not be an exception? Thanks?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

sander1095commented, Dec 18, 2020

Hm, I guess my original issue is fixed if I use TryDecorate. 😃 No need to discuss this further, thank you for your help, I will close this issue!

khellangcommented, Dec 9, 2020

My code example that scans for IQueryHandlers doesnt throw even though there are no registrations to be found. Why is this different?

Because Decorate relies on services being present in order to do its job. Scan doesn’t in the same way.

I get that it would be more consistent to (not) throw in both cases. After all, having a scan result in no registrations could very well indicate a developer error. But changing that behavior now would be a breaking change.

If using TryDecorate works for you, I’ll consider this issue “solved”, but we can leave it open as a reminder to split Scan into a non-throwing TryScan alterative 😄

I feel like not throwing when none are found is a good idea, but you are the owner of the codebase and I only have my own usage to draw from, so I am unsure if my usage reflects all the usecases this library covers.

I feel the opposite. No types probably means you screwed up something. And I’d like to find that out as soon as possible 😅

Read more comments on GitHub >

github_iconTop Results From Across the Web

MissingTypeRegistrationException is thrown when ...
Suppose I have a very simple generic interface as below: interface IGeneric ... is thrown when decorating open generic registered types #49.
Read more >
Collecting decorated classes (without registry classes)
I want to register event listeners via decorators - which already works but I'm not a 100% satisfied with my current solution. Currently...
Read more >
Primer on Python Decorators
Using the @register decorator, you can create your own curated list of interesting variables, effectively hand-picking some functions from globals() .
Read more >
The Ultimate Guide to Python Decorators, Part III
The function of this decorator is to register the decorated function as a handler for a Flask route. The problem is that in...
Read more >
Python Decorators (With Examples)
In Python, a decorator is a design pattern that allows you to modify the functionality of a function by wrapping it in another...
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 Post

No results found

github_iconTop Related Hashnode Post

No results found