khellang/Scrutor

C#

Assembly scanning and decoration extensions for Microsoft.Extensions.DependencyInjection

scanningdecorationassembly-scanningdecoration-extensionsasp-net-coredependency-injectionconventionsconvention-registrationhacktoberfestscrutor
Star 增长趋势
Star
4.3k
Forks
279
周增长
+4
Issues
37
2k4k
2015年11月2019年6月2023年1月2026年9月
README

Scrutor Build status NuGet Package

Scrutor - I search or examine thoroughly; I probe, investigate or scrutinize
From scrūta, as the original sense of the verb was to search through trash. - https://en.wiktionary.org/wiki/scrutor

Assembly scanning and decoration extensions for Microsoft.Extensions.DependencyInjection

Installation

Install the Scrutor NuGet Package.

Package Manager Console

Install-Package Scrutor

.NET Core CLI

dotnet add package Scrutor

Usage

The library adds two extension methods to IServiceCollection:

  • Scan - This is the entry point to set up your assembly scanning.
  • Decorate - This method is used to decorate already registered services.

See Examples below for usage examples.

Examples

Scanning

var collection = new ServiceCollection();

collection.Scan(scan => scan
     // We start out with all types in the assembly of ITransientService
    .FromAssemblyOf<ITransientService>()
        // AddClasses starts out with all public, non-abstract types in this assembly.
        // These types are then filtered by the delegate passed to the method.
        // In this case, we filter out only the classes that are assignable to ITransientService.
        .AddClasses(classes => classes.AssignableTo<ITransientService>())
            // We then specify what type we want to register these classes as.
            // In this case, we want to register the types as all of its implemented interfaces.
            // So if a type implements 3 interfaces; A, B, C, we'd end up with three separate registrations.
            .AsImplementedInterfaces()
            // And lastly, we specify the lifetime of these registrations.
            .WithTransientLifetime()
        // Here we start again, with a new full set of classes from the assembly above.
        // This time, filtering out only the classes assignable to IScopedService.
        .AddClasses(classes => classes.AssignableTo<IScopedService>())
            // Now, we just want to register these types as a single interface, IScopedService.
            .As<IScopedService>()
            // And again, just specify the lifetime.
            .WithScopedLifetime()
        // Generic interfaces are also supported too, e.g. public interface IOpenGeneric<T> 
        .AddClasses(classes => classes.AssignableTo(typeof(IOpenGeneric<>)))
            .AsImplementedInterfaces()
        // And you scan generics with multiple type parameters too
        // e.g. public interface IQueryHandler<TQuery, TResult>
        .AddClasses(classes => classes.AssignableTo(typeof(IQueryHandler<,>)))
            .AsImplementedInterfaces());

Scanning compiled view (UI) types

By default, Scrutor excludes compiler-generated types from the .AddClasses() type filters. When loading views from a framework such as Avalonia UI, we need to opt in to compiler-generated types, like this:

.AddClasses(classes => classes
    // Opt-in to compiler-generated types
    .WithAttribute<CompilerGeneratedAttribute>()
    // Optionally filter types to reduce number of service registrations.
    .InNamespaces("MyApp.Desktop.Views")
    .AssignableToAny(
        typeof(Window),
        typeof(UserControl)
    )
    .AsSelf()
    .WithSingletonLifetime()

With some UI frameworks, these compiler-generated views implement quite a few interfaces, so unless you need them, it's probably best to register these classes .AsSelf(); in other words, be very precise with your filters that accept compiler generated types.

Decoration

var collection = new ServiceCollection();

// First, add our service to the collection.
collection.AddSingleton<IDecoratedService, Decorated>();

// Then, decorate Decorated with the Decorator type.
collection.Decorate<IDecoratedService, Decorator>();

// Finally, decorate Decorator with the OtherDecorator type.
// As you can see, OtherDecorator requires a separate service, IService. We can get that from the provider argument.
collection.Decorate<IDecoratedService>((inner, provider) => new OtherDecorator(inner, provider.GetRequiredService<IService>()));

var serviceProvider = collection.BuildServiceProvider();

// When we resolve the IDecoratedService service, we'll get the following structure:
// OtherDecorator -> Decorator -> Decorated
var instance = serviceProvider.GetRequiredService<IDecoratedService>();

Sponsors

Entity Framework Extensions and Dapper Plus are major sponsors and proud to contribute to the development of Scrutor.

Entity Framework Extensions

Dapper Plus

相关仓库
trufflesecurity/trufflehog

Find, verify, and analyze leaked credentials

GoGo ModulescliGNU Affero General Public License v3.0secrettrufflehog
trufflesecurity.com
27.7k2.6k
bee-san/RustScan

🤖 The Modern Port Scanner 🤖

Rustcrates.iocliGNU General Public License v3.0securitypentesting
20.4k1.4k
SpacehuhnTech/esp8266_deauther

Affordable WiFi hacking platform for testing and learning

COtherwifiarduino
deauther.com
15k2.8k
shadow1ng/fscan

一款内网综合扫描工具,方便一键自动化、全方位漏扫扫描。(An intranet comprehensive scanning tool, enabling one-click automated, all-round vulnerability scanning)

GoGo ModulescliMIT Licensefscanscanner
14.5k1.9k
yogeshojha/rengine

reNgine is an automated reconnaissance framework for web applications with a focus on highly configurable streamlined recon process via Engines, recon data correlation and organization, continuous monitoring, backed by a database, and simple yet intuitive User Interface. reNgine makes it easy for penetration testers to gather reconnaissance with minimal configuration and with the help of reNgine's correlation, it just makes recon effortless.

HTMLGNU General Public License v3.0security-toolsosint
yogeshojha.github.io/rengine/
8.8k1.3k
mebjas/html5-qrcode

A cross platform HTML5 QR code reader. See end to end implementation at: https://scanapp.org

TypeScriptnpmlibraryApache License 2.0html5qrcode
qrcode.minhazav.dev
6.2k1.2k
introlab/rtabmap

RTAB-Map library and standalone application

C++Otherslamscanning
introlab.github.io/rtabmap
4k956
leebaird/discover

Custom Bash and Python scripts used to automate various penetration testing tasks including recon, scanning, enumeration, and malicious payload creation using Metasploit. For use with Ubuntu.

ShellcliMIT Licensered-teambash
3.9k868
s0md3v/Smap

a drop-in replacement for Nmap powered by shodan.io

GoGo ModulescliGNU Affero General Public License v3.0port-scannernmap
3.3k320
activecm/rita-legacy

Real Intelligence Threat Analytics (RITA) is a framework for detecting command and control communication through network traffic analysis.

GoGo ModulesGNU General Public License v3.0ritanetwork-traffic
2.5k351
archerysec/archerysec

ASOC, ASPM, DevSecOps, Vulnerability Management Using ArcherySec.

JavaScriptnpmGNU General Public License v3.0vulnerability-assessmentvulnerabilities
archerysec.com
2.5k526
codingo/Reconnoitre

A security tool for multithreaded information gathering and service enumeration whilst building directory structures to store results, along with writing out recommendations for further testing.

PythonPyPIGNU General Public License v3.0oscppenetration-testing
2.2k451