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.

Enable Scrutor to use external DI

See original GitHub issue

We had DI running for quite some time now using the standard microsoft implementation. Due to several reasons (one beeing https://github.com/aspnet/AspNetCore/issues/2737) we now move to SimpleInjector (which we used on legacy WebApi projects alot).

Now SimpleInjector does not have rich assembly scanning support and states

Although many other DI libraries contain an advanced API for doing convention based registration, we found that doing this with custom LINQ queries is easier to write, more understandable, and can often prove to be more flexible than using a predefined and restrictive API.

I can totally accept this, however we are now sitting infront of quite some Scrutor lines which did this heavy lifting for us.

Something that would now have been really cool is to still be able to use Scrutor but inject a custom RegistrationStrategy which under the hood would not do services.Add but instead a custom action like container.Register. (SimpleInjector even has some code under the hood which turns ServiceDescriptor objects into SImpleInjector registrations).

Does this make any sense or is it completely out of the scope of this project?

Thanks, Philipp

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
pfeiglcommented, Sep 12, 2019

Closing - thanks for the help, everything worked out today just nicely.

0reactions
pfeiglcommented, Sep 11, 2019

This is btw. what seems to work ok for now for us. There is quite a few cases not considered here (which were not relevant to us).

  • Collection registrations use the Lifestyle of the first identified collection item
  • Only registrations based on ImplementationType do work (We did not use anything else like factories - atleast not via Scrutor)
var processedCollections = new List<Type>();
foreach(var descriptor in services)
{
	Lifestyle lifestyle;
	switch (descriptor.Lifetime)
	{
		case ServiceLifetime.Scoped:
			lifestyle = Lifestyle.Scoped;
			break;
		case ServiceLifetime.Singleton:
			lifestyle = Lifestyle.Singleton;
			break;
		case ServiceLifetime.Transient:
		default:
			lifestyle = Lifestyle.Transient;
			break;
	}

	if (services.Any(d => d != descriptor && d.ServiceType == descriptor.ServiceType))
	{
		if(processedCollections.Contains(descriptor.ServiceType))
		{
			continue;
		}
		Container.Collection.Register(descriptor.ServiceType, services
			.Where(d => d.ServiceType == descriptor.ServiceType)
			.Select(d => lifestyle.CreateRegistration(d.ImplementationType, Container)));
		processedCollections.Add(descriptor.ServiceType);
	}
	else
	{
		Container.Register(descriptor.ServiceType, descriptor.ImplementationType, lifestyle);
	}
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Scrutor to automatically register your services with ...
In this post I describe how to use the open source library Scrutor to add assembly scanning capabilities to the ASP.NET Core DI...
Read more >
Recently Active 'scrutor' Questions
I am trying to use Scrutor to ease DI registration in an asp.net core 3.1 application like below services.Scan(scan => scan .FromAssembliesOf( ...
Read more >
How to decorate objects created by a custom factory using . ...
First, let's put some constraints on where the value is coming from. Specifically, let's say we can have a service providing that value, ......
Read more >
Dynamically Loading Assemblies for Dependency Injection ...
We will use Scrutor as our DI framework. It's an extension of the native .Net DI framework with extensions for scanning and registering ......
Read more >
How to add a caching layer in .NET 5 with Decorator ...
Open your project and install it via UI or using the command line by running dotnet add package Scrutor . Now head to...
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