Masterly.Autofac.DependenciesScanner
1.0.0
See the version list below for details.
dotnet add package Masterly.Autofac.DependenciesScanner --version 1.0.0
NuGet\Install-Package Masterly.Autofac.DependenciesScanner -Version 1.0.0
<PackageReference Include="Masterly.Autofac.DependenciesScanner" Version="1.0.0" />
paket add Masterly.Autofac.DependenciesScanner --version 1.0.0
#r "nuget: Masterly.Autofac.DependenciesScanner, 1.0.0"
// Install Masterly.Autofac.DependenciesScanner as a Cake Addin #addin nuget:?package=Masterly.Autofac.DependenciesScanner&version=1.0.0 // Install Masterly.Autofac.DependenciesScanner as a Cake Tool #tool nuget:?package=Masterly.Autofac.DependenciesScanner&version=1.0.0
Masterly.Autofac.DependenciesScanner
Assembly scanning and decoration extensions for Autofac.
Features
- Scan the given assemblies and exteract all services.
- Register extracted services and determine its lifetime according to the implemented interface (eg.
ISocopedDependnecy
⇒ Instance Per Lifetime Scope ...etc). - Support custom registration by inheret from
ScannerModule
. - Scan both concrete and generic class types.
Give a Star! ⭐
If you like or are using this project please give it a star. Thanks!
Installation
Install the Masterly.Autofac.DependenciesScanner NuGet Package.
Package Manager Console
Install-Package Masterly.Autofac.DependenciesScanner
.NET Core CLI
dotnet add package Masterly.Autofac.DependenciesScanner
Usage
IScopedDependency
⇒ IDependencyA
lifetime would be Instance Per Lifetime Scope
public interface IDependencyA : IScopedDependency { }
IDependencyB
going to ineheret IDependencyA lifetime with is Instance Per Lifetime Scope
public interface IDependencyB : IDependencyA { }
ISingletonDependency
⇒ IDependencyC
lifetime would be Single Instance
public interface IDependencyC : ISingletonDependency { }
public abstract class AbstractDependencyClassA : IDependencyB { }
// ITransientDependency => AbstractDependencyClassB lifetime would be Instance per dependency
public abstract class AbstractDependencyClassB : ITransientDependency { }
public class DependencyA : IDependencyA { }
public class DependencyB : AbstractDependencyClassA { }
public class DependencyC : AbstractDependencyClassB { }
public class DependencyD : IDependencyC { }
As you can see above, all services going to be registered as all implemented interfaces and inhereted abstract classes, in addition to itself.
For example, DependencyB
can be resolved as IDependencyA
, IDependencyB
, AbstractDependencyClassA
, in addition to DependencyB
.
In the beginning use UseServiceProviderFactory extension in Program.cs as below
public class Program
{
public static void Main(string[] args)
{
// ASP.NET Core 3.0+:
// The UseServiceProviderFactory call attaches the
// Autofac provider to the generic hosting mechanism.
var host = Host.CreateDefaultBuilder(args)
.UseAutofacServiceProviderFactory()
.ConfigureWebHostDefaults(webHostBuilder => {
webHostBuilder
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>();
})
.Build();
host.Run();
}
}
You can use ContainerBootstraper to prepare and config Autofac container and scan services and modules in all given assemblies. Here, it registers services and Autofac modules and excexutes ScannerModules, then returns a built container (not recomended for Asp Core)
IContainer container = ContainerBootstrapper.Bootstrap(assemblies);
For Asp Core, use ContainerBootstrapper as below
// ConfigureContainer is where you can register things directly
// with Autofac. This runs after ConfigureServices so the things
// here will override registrations made in ConfigureServices.
// Don't build the container; that gets done for you by the factory.
public void ConfigureContainer(ContainerBuilder containerBuilder)
{
// Register your own things directly with Autofac here. Don't
// call containerBuilder.Populate(), that happens in AutofacServiceProviderFactory
// for you.
ContainerBootstrapper.Bootstrap(builder, assemblies);
}
To register single service
containerBuilder.RegisterDependency(serviceType);
Scan all services those implements IScopedDependency
, ITransientDependency
or ISingletonDependency
and register them in the ContainerBuilder
, also exceute scanner modules those inheret from ScannerModule
.
containerBuilder.ScanDependencies(assemblies);
ScannerModule
ScannerModule
gives tha ability to customize scanning and component registration.
For example:
public interface IApi { }
public class Api1 : IApi { public Api1(string url) { } }
public class Api2 : IApi { public Api2(string url) { } }
public static class ApiFactory
{
public static object Create(Type type, string url) {
object api = Activator.CreateInstance(type, url);
return api;
}
}
public class SampleScannerModule : ScannerModule
{
public override void Scan(ContainerBuilder builder, params Assembly[] assemblies)
{
foreach (var serviceType in assemblies.SelectMany(assembly => assembly.GetExportedTypes().Where(t => typeof(IApi).IsAssignableFrom(t))))
Register(builder, serviceType);
}
private void Register(ContainerBuilder builder, Type serviceType)
{
if (serviceType.IsInterface || serviceType.IsAbstract || !typeof(IApi).IsAssignableFrom(serviceType))
return;
string url = "https://example.com";
builder.RegisterComponent(RegistrationBuilder
.ForDelegate((ctx, parameters) => ApiFactory.Create(serviceType, url))
.As(serviceType)
.As<IApi>()
.InstancePerDependency()
.CreateRegistration());
}
}
Note
The default lifetime for any service deos not implement any of the mentioned interfaces, it is going to be Instance Per Lifetime Scope
License
MIT
Free Software, Hell Yeah!
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 3.1
- Autofac (>= 6.3.0)
- Autofac.Extensions.DependencyInjection (>= 7.2.0)
- Masterly.Autofac.Interfaces (>= 1.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 6.0.0)
-
.NETFramework 4.6.2
- Autofac (>= 6.3.0)
- Autofac.Extensions.DependencyInjection (>= 7.2.0)
- Masterly.Autofac.Interfaces (>= 1.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 6.0.0)
-
.NETStandard 2.0
- Autofac (>= 6.3.0)
- Autofac.Extensions.DependencyInjection (>= 7.2.0)
- Masterly.Autofac.Interfaces (>= 1.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 6.0.0)
-
net5.0
- Autofac (>= 6.3.0)
- Autofac.Extensions.DependencyInjection (>= 7.2.0)
- Masterly.Autofac.Interfaces (>= 1.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 6.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.