AutoMapper dependency injection with parameters - c#

Error: No parameterless constructor for AutoMapperConfiguration
I am using the nuget package automapper DI
public class AutoMapperConfiguration : Profile
{
private readonly ICloudStorage _cloudStorage;
public AutoMapperConfiguration(ICloudStorage cloudStorage)
{
_cloudStorage = cloudStorage;
// Do mapping here
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ICloudStorage, AzureStorage>();
services.AddAutoMapper(); // Errors here
}
How do I use the automapper DI with parameters?

I don't think you are able to add DI parameters to Profiles. Part of the logic behind this may be that these are only instantianted once, so services registered through AddTransient would not behave as expected.
One option would be to inject it into an ITypeConverter:
public class AutoMapperConfiguration : Profile
{
public AutoMapperConfiguration()
{
CreateMap<SourceModel, DestinationModel>().ConvertUsing<ExampleConverter>();
}
}
public class ExampleConverter : ITypeConverter<SourceModel, DestinationModel>
{
private readonly ICloudStorage _storage;
public ExampleCoverter(ICloudStorage storage)
{
// injected here
_storage = storage;
}
public DestinationModel Convert(SourceModel source, DestinationModel destination, ResolutionContext context)
{
// do conversion stuff
return new DestinationModel();
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ICloudStorage, AzureStorage>();
services.AddAutoMapper();
}

you may want to try this on your Startup.cs, if AddAutoMapper is an an extension that you built, then add the code below to your extension.
public void ConfigureServices(IServiceCollection services)
{
var mapperConfiguration = new MapperConfiguration(mc =>
{
IServiceProvider provider = services.BuildServiceProvider();
mc.AddProfile(new AutoMapperConfiguration (provider.GetService<ICloudStorage>()));
});
services.AddSingleton(mapperConfiguration.CreateMapper());
}

I found one solution to resolve this.
Create one list of types before add profile and pass it in the parameter.
public class AutoMapperConfiguration : Profile
{
private readonly ICloudStorage _cloudStorage;
public AutoMapperConfiguration(ICloudStorage cloudStorage)
{
_cloudStorage = cloudStorage;
// Do mapping here
}
}
public void ConfigureServices(IServiceCollection services)
{
var types = new List<Type>();
services.AddSingleton<ICloudStorage, AzureStorage>();
services.AddAutoMapper((provider, cfg) =>
{
var storage = new AutoMapperConfiguration(provider.GetService<ICloudStorage>());
types.Add(storage.GetType());
cfg.AddProfile(storage);
//others profiles
}, types);
}

Related

Use DI in ConfigureServices?

I have implemented a custom InputFormatter (MyInputFormatter):
public class MyInputFormatter : SystemTextJsonInputFormatter
{
private readonly IMyDepenency _mydependency;
public CustomInputFormatter(
JsonOptions options,
ILogger<SystemTextJsonInputFormatter> logger,
IMyDependency myDependency
) : base(options, logger)
{
_mydependency = myDependency ?? throw new ArgumentNullException(nameof(myDependency));
}
public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
{
//...
}
}
Now, according to the documentation I need to use it as follows:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers(options =>
{
options.InputFormatters.Insert(0, new MyInputFormatter(...));
});
}
However, as you can see my CustomInputFormatter has some constructor arguments required and needs some services and it's not clear to me how to use DI to resolve these services. I have read through a lot of answers/blogs/pages like this one but either the inputformatter doesn't have any constructor arguments (and so no need for DI, just new up a new instance inline) or the following is suggested:
public void ConfigureServices(IServiceCollection services)
{
var sp = services.BuildServiceProvider();
services.AddControllers(options =>
{
options.InputFormatters.Insert(0, new MyInputFormatter(
sp.GetService<...>(),
sp.GetService<...>(),
sp.GetService<IMyDependency>(),
));
});
}
But we're not supposed to call BuildServiceProvider from ConfigureServices.
How would I go about this?
You can make use of the Options infrastructure and create an IConfigureOptions<MvcOptions> . This new service can take the necessary dependeices. It will be instantiated and "executed" the first time something (the MVC infrastructure) requests an IOptions<MvcOptions>
public class ConfigureMvcOptionsFormatters : IConfigureOptions<MvcOptions>
{
private readonly ILoggerFactory _factory;
private readonly JsonOptions _jsonOpts;
private readonly IMyDependency _depend;
public ConfigureMvcOptionsFormatters(IOptions<JsonOptions> options, ILoggerFactory loggerFactory, IMyDependency myDependency)
{
_factory = loggerFactory;
_jsonOpts = options.Value;
_depend = myDependency;
}
public void Configure(MvcOptions options)
{
var logger = _factory.CreateLogger<SystemTextJsonInputFormatter>();
var formatter = new MyInputFormatter(_jsonOpts, logger, _depend);
options.InputFormatters.Insert(0, formatter);
}
}
You then register your class to have its IConfigureOptions implementation run by calling the ConfigureOptions<T>() extension method on the IServiceCollection:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.ConfigureOptions<ConfigureMvcOptionsFormatters>();
}
Alternatively you can use an Options builder along with Configure and it's callback, specifying your dependencies as the generic arguments.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddOptions<MvcOptions>()
.Configure<IOptions<JsonOptions>, ILoggerFactory, IMyDependency>(
(o, j, l, d) => o.InputFormatters.Insert(0, new MyInputFormatter(j.Value, l.CreateLogger<SystemTextJsonInputFormatter>(), d)
);
}
Note: I've been using ILoggerFactory because I don't believe the infrastructure will inject an ILogger<ClassA> into ClassB. However, I admit I've never tried it and I'm not near a computer to verify. If it is in fact allowed, you can specify the type you need directly instead.

How to correctly configure InstancePerTenant using Autofac?

I have been trying to setup my component instance per each tenant using InstancePerTenant. However, the InstancePerTenant somehow behave like a SingleInstance. Is there something wrong with my setup?
I have this ContainerBuilder extension which configure the multitenant related dependencies.
public static ContainerBuilder AddMultitenancy(this ContainerBuilder builder)
{
builder.RegisterType<HttpContextAccessor>().As<IHttpContextAccessor>().SingleInstance();
builder.RegisterType<TenantStore>().As<ITenantStore>().SingleInstance();
builder.RegisterType<TenantResolverStrategy>().As<ITenantIdentificationStrategy>().SingleInstance();
return builder
}
The tenant is identified by hostname:
public class TenantResolverStrategy : ITenantIdentificationStrategy
{
private readonly IHttpContextAccessor httpContextAccessor;
public TenantResolverStrategy(
IHttpContextAccessor httpContextAccessor
)
{
this.httpContextAccessor = httpContextAccessor;
}
public bool TryIdentifyTenant(out object tenantId)
{
// hostname is the tenantId
tenantId = httpContextAccessor.HttpContext?.Request?.Host.Value;
return (tenantId != null || tenantId == (object)"");
}
}
TenantStore is just a class to resolve the tenant entity from database based on the tenantId (hostname)
public class TenantStore : ITenantStore
{
private readonly ITenantIdentificationStrategy tenantIdentificationStrategy;
private readonly MemoryCacheStore cacheStore;
private readonly ITenantService tenantService;
public TenantStore(
ITenantIdentificationStrategy tenantIdentificationStrategy,
MemoryCacheStore cacheStore,
ITenantService tenantService
)
{
this.tenantIdentificationStrategy = tenantIdentificationStrategy;
this.cacheStore = cacheStore;
this.tenantService = tenantService;
}
public async Task<TenantEntity> GetTenantAsync(object tenantId)
{
var hostName = (string)tenantId;
var tenant = cacheStore.Get<TenantEntity>(CacheType.Tenant, hostName);
if (tenant == null)
{
tenant = await tenantService.GetTenantByHostNameFromDatabaseAsync(hostName);
cacheStore.Set(tenant, CacheType.Tenant, hostName);
}
return tenant ?? new TenantEntity();
}
}
In Startup.cs, I am registering TenantSpecific with InstancePerTenant:
public void ConfigureContainer(ContainerBuilder builder)
{
builder.AddMultitenancy();
builder.RegisterType<TenantSpecific>().As<ITenantSpecific>().InstancePerTenant();
}
public static MultitenantContainer ConfigureMultitenantContainer(IContainer container
{
var strategy = container.Resolve<ITenantIdentificationStrategy>();
var multitenantContainer = new MultitenantContainer(strategy, container);
// Nothing important here
multitenantContainer.RegisterMultitenantSpecificStuff();
return multitenantContainer;
}
TenantSpecific.cs and TenantSpecificController.cs:
public class TenantSpecific
{
public Guid Id { get; set; }
public TenantSpecific()
{
this.Id = Guid.NewGuid();
}
}
public class TenantSpecificController : ApiController
{
private readonly ITenantSpecific tenantSpecific;
public TenantSpecificController(ITenantSpecific tenantSpecific)
{
this.tenantSpecific = tenantSpecific;
}
[HttpGet]
public IActionResult Get()
{
return Ok(tenantSpecific.Id);
}
}
In Program.cs
public static IHostBuilder CreateHostBuilder(string[] args)
{
var host = Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacMultitenantServiceProviderFactory(Startup.ConfigureMultitenantContainer))
.ConfigureWebHostDefaults(webHostBuilder =>
{
webHostBuilder
.UseConfiguration(ConfigurationModule.GetConfiguration())
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>();
});
return host;
}
When I invoke http://tenant1.localhost/tenant-specific/ and http://tenant2.localhost/tenant-specific, the constructor of TenantSpecific is only called once like Singleton. The tenantSpecific.Id returns the same value. So I assume InstancePerTenant is not working here.
Is there something wrong with my setup?
As written in the documentation ASP.net core multitenant support
You should add a call to AddAutofacMultitenantRequestServices() to add the required middleware to the root container which is required for multitenancy to work.
public void ConfigureServices(IServiceCollection services)
{
// This will all go in the ROOT CONTAINER and is NOT TENANT SPECIFIC.
services.AddMvc();
services.AddControllers();
// This adds the required middleware to the ROOT CONTAINER and
// is required for multitenancy to work.
services.AddAutofacMultitenantRequestServices();
}

how to configure dependencies in asp.net core

I have an ASP.Net Core 2.1 application. I need to register & inject few dependencies of AWS.
Currently, the implementation looks like this:
public abstract class BaseService
{
protected readonly IConfiguration _configuration;
protected readonly RegionEndpoint _region;
protected readonly IAmazonDynamoDB _dynamoClient;
protected IPocoDynamo _pocoDynamo;
public BaseService(IConfiguration configuration)
{
_configuration = configuration;
var awsSettings = configuration.GetSection("AWS");
_dynamoClient = SetDynamoClient(awsSettings);
_pocoDynamo = SetPocoDynamoClient();
}
protected IAmazonDynamoDB SetDynamoClient(IConfigurationSection configuration)
{
AWSCredentials credentials = new BasicAWSCredentials(configuration["AccessKey"], configuration["AccessSecret"]);
return new AmazonDynamoDBClient(credentials, _region);
}
protected IPocoDynamo SetPocoDynamoClient()
{
return new PocoDynamo(_dynamoClient);
}
}
While unit testing, AWS services can't be mocked due to this.
I want to register all these dependencies in Startup.cs in ConfigureServices()
This is what I was trying:
public void ConfigureServices(IServiceCollection services)
{
AWSCredentials credentials = new BasicAWSCredentials(configuration["AccessKey"], configuration["AccessSecret"]);
services.AddTransient(IAmazonDynamoDB, (a) =>
{
return new AmazonDynamoDBClient(credentials, RegionEndpoint.GetBySystemName(""))
});
// here I need to pass the IAmazonDynamoDB to below IOC
// services.AddSingleton<IPocoDynamo,new PocoDynamo()> ();
return services;
}
But this is throwing an error
Error CS0119 'IAmazonDynamoDB' is a type, which is not valid in the given context
How to configure dependencies as required here?
Thanks!
Use factory delegate to call the registered service
public void ConfigureServices(IServiceCollection services) {
AWSCredentials credentials = new BasicAWSCredentials(configuration["AccessKey"], configuration["AccessSecret"]);
services.AddTransient<IAmazonDynamoDB>(sp =>
new AmazonDynamoDBClient(credentials, RegionEndpoint.GetBySystemName(""))
);
//here pass the IAmazonDynamoDB to below IOC
services.AddSingleton<IPocoDynamo>(serviceProvider => {
var pocoDynamo = new PocoDynamo(serviceProvider.GetRequieredService<IAmazonDynamoDB>());
pocoDynamo.SomeMethod();
return pocoDynamo;
});
}
The target class should no longer need to be dependent on IConfiguration as the dependencies can be explicitly injected via constructor injection.
public abstract class BaseService {
protected readonly IAmazonDynamoDB dynamoClient;
protected readonly IPocoDynamo pocoDynamo;
public BaseService(IAmazonDynamoDB dynamoClient, IPocoDynamo pocoDynamo) {
this.dynamoClient = dynamoClient;
this.pocoDynamo = pocoDynamo;
}
}

Dependency Injection for third party data context in ASP.NET core

In my ASP.NET core Web API, I need to use MongoDb. Following is my implementation so far but I am stuck in resolving dependencies.
DataContext:
public class AppDbContext
{
public IMongoDatabase MongoDatabase { get; set; }
public AppDbContext()
{
var client = new MongoClient("mongodb://localhost:27017");
MongoDatabase = client.GetDatabase("cse-dev-db");
}
}
Repository:
public class BuyRepository: IBuyRepository {
private readonly AppDbContext _appDbContext;
public BuyRepository(AppDbContext appDbContext) {
_appDbContext = appDbContext;
}
public Buy Add(Buy buy) {
_appDbContext.MongoDatabase.GetCollection<Buy("Buy").InsertOne(buy);
return buy;
}
}
Controller:
private readonly BuyRepository _buyRepository;
public ValuesController(BuyRepository buyRepository) {
_buyRepository = buyRepository;
}
My question is how to add this dependencies in ConfigureServices
public void ConfigureServices(IServiceCollection services) {
services.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc();
// How to add dependencies here
}
PS: I have already seen this but it does not work.
Update
I have tried as per comment by a user
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddScoped<AppDbContext>();
services.AddMvc();
services.AddScoped<IBuyRepository, BuyRepository>();
}
Now I am getting following exception
Unable to resolve service for type
'CseApi.Repositories.BuyRepository' while attempting to activate
'CseApi.Controllers.ValuesController'.
Try to register services like:
public void ConfigureServices(IServiceCollection services) {
services.AddApplicationInsightsTelemetry(Configuration);
services.AddScoped<AppDbContext>();
services.AddScoped<IBuyRepository, BuyRepository>();
services.AddMvc();
// How to add dependencies here
}
Update for comment
Controller code should be something like below:
private readonly IBuyRepository _buyRepository;
public ValuesController(IBuyRepository buyRepository) {
_buyRepository = buyRepository;
}
Update your controller's injection from this:
private readonly BuyRepository _buyRepository;
to this:
private readonly IBuyRepository _buyRepository;

Self-Registering Libraries with Autofac 4 and vNext

i'd like to create a Plugin Enviroment for my ASP.Net 5.0 / MVC 6 Application. I'm using Autofac as IOC Container and i like to load the Plugins (Class Libraries) from the build in DNX LibraryManager. The goal of using the Library Manager is, that i don't have to care about NuGet Packages and Frameworks.
The Problem i have is the LifeCycle, i have to build the IOC Container before the instance of the LibraryManager is available. Because the Autofac Container provides his own IServiceProvider Instance which i have to inject within the ConfigureService() Method call (AddAutofac).
Does anyone know how to get this working?
Update: I have fixed my problem with Davids help and updated the code to get it working with the release candidates. Also i have added support for configuration.
In my DNX Class Library i implemented a Class for Self-Registration:
public class AutofacModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.Register(c => new SimpleService())
.As<IService>()
.InstancePerLifetimeScope();
}
}
In my MVC WebApplication i have added the Class Library as Dependency.
Startup.cs
public IConfiguration Configuration { get; set; }
public class Startup
{
public Startup( IApplicationEnvironment applicationEnvironment )
{
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.SetBasePath( applicationEnvironment.ApplicationBasePath );
configurationBuilder.AddJsonFile( "appsettings.json" );
configurationBuilder.AddJsonFile( "autofac.json" );
configurationBuilder.AddEnvironmentVariables();
this.Configuration = configurationBuilder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDependencies();
}
public void Configure(IApplicationBuilder applicationBuilder, IHostingEnvironment hostingEnvironment)
{
applicationBuilder.UseDependencies( this.Configuration );
applicationBuilder.UseStaticFiles();
applicationBuilder.UseMvc();
}
}
I have created an DependencyResolver to keep the ContainerBuilder instance.
DependencyResolver.cs
public class DependencyResolver : IDependencyResolver
{
private IContainer container;
private readonly ContainerBuilder builder;
public DependencyResolver()
{
this.builder = new ContainerBuilder();
}
public void RegisterModule( IModule module )
{
this.builder.RegisterModule( module );
}
public void RegisterModules( IEnumerable<Assembly> assemblies )
{
this.builder.RegisterAssemblyModules(assemblies.ToArray());
}
public void Populate( IServiceCollection services)
{
this.builder.Populate( services );
}
public void Build()
{
this.container = this.builder.Build();
}
public T Resolve<T>() where T : class
{
return this.container?.Resolve<T>();
}
}
IDependencyResolver.cs
public interface IDependencyResolver
{
void RegisterModule( IModule module );
void RegisterModules( IEnumerable<Assembly> assemblies );
void Populate(IServiceCollection services);
void Build();
T Resolve<T>() where T : class;
}
Last but not least i have created an Extension Class
DependencyResolverExtensions.cs
public static class DependencyResolverExtensions
{
public static IServiceCollection AddDependencies( this IServiceCollection services )
{
DependencyResolver dependencyResolver = new DependencyResolver();
dependencyResolver.Populate(services);
ServiceDescriptor serviceDescriptor = new ServiceDescriptor(typeof ( IDependencyResolver ), dependencyResolver );
services.TryAdd(serviceDescriptor);
return services;
}
public static IApplicationBuilder UseDependencies(this IApplicationBuilder applicationBuilder, IConfiguration configuration)
{
IDependencyResolver dependencyResolver = applicationBuilder.GetService<IDependencyResolver>();
if (dependencyResolver == null) return applicationBuilder;
ILibraryManager libraryManager = applicationBuilder.GetService<ILibraryManager>();
if (libraryManager == null) return applicationBuilder;
IEnumerable<Assembly> assemblies = libraryManager.GetLoadableAssemblies();
dependencyResolver.RegisterModules(assemblies);
ConfigurationModule configurationModule = new ConfigurationModule( configuration );
dependencyResolver.RegisterModule( configurationModule );
dependencyResolver.Build();
IServiceProvider serviceProvider = dependencyResolver.Resolve<IServiceProvider>();
applicationBuilder.ApplicationServices = serviceProvider;
return applicationBuilder;
}
public static IEnumerable<Assembly> GetLoadableAssemblies(this ILibraryManager libraryManager)
{
List<Assembly> result = new List<Assembly>();
IEnumerable<Library> libraries = libraryManager.GetLibraries();
IEnumerable<AssemblyName> assemblyNames = libraries.SelectMany(e => e.Assemblies).Distinct();
assemblyNames = Enumerable.Where(assemblyNames, e => e.Name.StartsWith("MyLib."));
foreach (AssemblyName assemblyName in assemblyNames)
{
Assembly assembly = Assembly.Load(assemblyName);
result.Add(assembly);
}
return result;
}
public static T GetService<T>(this IApplicationBuilder applicationBuilder) where T : class
{
return applicationBuilder.ApplicationServices.GetService(typeof (T)) as T;
}
}
If you need to switch between different implementations, like mock and real data you can use the Autofac Configuration.
autofac.json
{
"components": [
{
"type": "MyLib.Data.EF.EntitiesData, MyLib.Data.EF",
"services": [
{
"type": "MyLib.Abstractions.IDataRepository, MyLib.Abstractions"
}
]
}
]
}
It's a shame that ConfigureServices is not injectable, that would make this a lot easier.
Looking at the code you should be safe to replace the IServiceProvider inside Configure(...) instead of inside ConfigureServices(...) and get the intended behavior. ApplicationServices is setable.
In your UseAutofac method you should be able to do something like:
public static IApplicationBuilder UseAutofac( [NotNull] this IApplicationBuilder applicationBuilder )
{
IAutofacResolver autofacResolver = applicationBuilder.GetService<IAutofacResolver>();
ILibraryManager libraryManager = applicationBuilder.GetService<ILibraryManager>();
autofacResolver.RegisterLibraryModules( libraryManager);
applicationBuilder.ApplicationServices = autofacResolver.Resolve();
return applicationBuilder;
}
I've come up with a solution that uses part of this, but also uses a ComponentContainer that addresses the potential memory leaks in the DependencyResolver. This also works with RC1. Not sure yet about RC2 as it's not complete enough for me to test.
The ComponentContainer looks like this:
public static class ComponentContainer {
static IContainer _container;
static ContainerBuilder _containerBuilder;
public static ContainerBuilder Builder {
get {
if (_containerBuilder == null)
_containerBuilder = new ContainerBuilder();
return _containerBuilder;
}
}
public static IServiceProvider ServiceProvider {
get {
if (_container == null)
_container = _containerBuilder.Build();
return _container.Resolve<IServiceProvider>();
}
}
public static ComponentFactory<TObject> Component<TObject>() => new ComponentFactory<TObject>(_container);
public static void RegisterAssembly(Assembly assembly) {
if (assembly == null) return;
foreach (var obj in assembly.GetTypes().Where(t => t.GetCustomAttribute<ExportAttribute>() != null)) {
ExportAttribute att = obj.GetCustomAttribute<ExportAttribute>();
if (att.ContractType != null) {
_containerBuilder.RegisterType(obj).As(att.ContractType);
} else {
foreach (var intf in obj.GetInterfaces())
_containerBuilder.RegisterType(obj).As(intf);
}
}
}
}
public class ComponentFactory<TObject> : IDisposable {
protected TObject CurrentObject;
protected ILifetimeScope CurrentScope;
public TObject Current => (TObject)CurrentObject;
public ComponentFactory(IContainer container) {
CurrentScope = container.BeginLifetimeScope();
CurrentObject = CurrentScope.Resolve<TObject>();
}
public TObject Component => CurrentObject;
public void Dispose() {
(CurrentObject as IDisposable)?.Dispose();
CurrentScope.Dispose();
}
}
Then in Startup.cs I do the following:
public virtual IServiceProvider ConfigureServices(IServiceCollection services) {
services.AddMvc();
services.AddOptions();
services.AddSession();
services.AddCaching();
var assemblyLoadContextAccessor = services.FirstOrDefault(s => s.ServiceType == typeof(IAssemblyLoadContextAccessor)).ImplementationInstance as IAssemblyLoadContextAccessor;
var libraryManager = services.FirstOrDefault(s => s.ServiceType == typeof(ILibraryManager)).ImplementationInstance as ILibraryManager;
var loadContext = assemblyLoadContextAccessor.Default;
foreach(var library in libraryManager.GetLibraries()) {
var assembly = loadContext.Load(library.Name);
if(assembly != null) {
var module = assembly.GetTypes().FirstOrDefault(t => t == typeof(IModule));
if(module != null)
ComponentContainer.Builder.RegisterAssemblyModules(assembly);
else
ComponentContainer.RegisterAssembly(assembly);
}
}
ComponentContainer.Builder.Populate(services);
return ComponentContainer.ServiceProvider;
}
To export modules within an assembly, I either mark them with an ExportAttribute or add a class to the assembly that implements Autofac's IModule. The code in ConfigureServices will enumerate through the application's modules and feed them to the static Builder in ComponentContainer. Once the container has been built, you can either resolve modules through injection in a constructor or you can request a specific type by:
(using var myComponentFactory = ComponentContainer.Component<IMyModule>()) {
//You can now access your component through myComponentFactory.Component
//Once it passes out of scope of using, it will be properly disposed of
//along with the scope from which it was created.
}
Edit: With the release of RC2, this code is no longer valid as the enumeration of assemblies and classes will fail. I haven't come up with a good solution yet. If anyone else has any suggestions for enumerating assemblies in RC2, please let me know.

Categories