I am trying to implement MEF framework in my .Net core and .Net framework application. Here is my sample.
Project One class library:
[Export(typeof(IProductRepository))]
public class ProductRepository : IProductRepository
{
public ProductRepository(ApplicationDBContext context)
{
this.context = context;
}
}
Project Two class library:
[Export(typeof(IProductService))]
public class ProductService : IProductService
{
public ProductService(IProductRepository _ProductRepository)
{
}
}
So here both project depends on interface injection on constructor, and in another class library i am implementing the MEF like
[System.Composition.ImportMany]
public static IProductService ProductService{ get; set; }
[System.Composition.ImportMany]
public static IProductRepository ProductRepository{ get; set; }
var executableLocation = Assembly.GetEntryAssembly().Location;
var assemblies = Directory
.GetFiles(executableLocation , "*.dll", SearchOption.TopDirectoryOnly)
.Select(AssemblyLoadContext.Default.LoadFromAssemblyPath)
.ToList();
var configuration = new ContainerConfiguration().WithAssemblies(assemblies);
using (var container = configuration.CreateContainer())
{
ProductRepository= container.GetExport<IProductRepository>();
ProductService= container.GetExport<IProductService>();
}
Here I am getting error 'No export was found for the contract 'IProductRepository"IProductService"'
My question is, how to pass an interface to constructor of class using MEF framework? how to pass an object to constructor of class using MEF framework? How to implement MEF when multiple projects are involved?