I have a problem with method injection between assemblies in a multiple assembly project. It only works for me in the scope of the Assembly1 with Autofac configuration.
I set the Autofac with the below packages: Autofac 3.5.2, Autofac.MVC4 3.1.0, Autofac.Mvc5 3.0.0 (project .NET Framework 4.8 MVC). I'd like to inject the object from Assembly2 (class definition of repo2) to Assembly2. There is no problem with the constructor and method injection in the scope of the Assembly1 where I have the Autofac configuration. When I try to do the same in assembly3 I get null (there is an dependency between assemblies). The only way I can force the Autofac to make an injection to Assembly2 is to use the below code in ConfigureContainer() (after DependencyResolver):
var variableX = container.Resolver<Solution2>();
Somehow the container needs a strict hint on how to resolve the solution2. I don't know why?
After the use of above code I get the possibility to get the object in solution2 but only on the start of the application and it is the problem that I'm not able to use it when it is necessary.
Below the code of the application:
Global.asax Code - ConfigureContainer()
var builder = new ContainerBuilder();
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
builder.RegisterAssemblyModules(loadedAssemblies);
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterModelBinderProvider();
builder.RegisterAssemblyModules(Assembly.GetExecutingAssembly());
builder.RegisterModule<ServiceModule>();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
Container module
public class ContainerModule : Autofac.Module
{
protected override void Load(Autofac.ContainerBuilder builder)
{
builder.RegisterModule<ServiceModule>();
}
}
Service Module
// CODE TO INJECT REPO OBJECT DO Controller - constructor injection - works fine - the same Assembly
builder.RegisterAssemblyTypes(typeof(IRepo).Assembly).AsImplementedInterfaces().InstancePerRequest();
var assembly = typeof(ServiceModule).GetTypeInfo().Assembly;
var assembly2 = typeof(IRepo).GetTypeInfo().Assembly;
builder.RegisterAssemblyTypes(assembly)
.Where(x => x.IsAssignableTo<IRepo>())
.AsImplementedInterfaces()
.InstancePerRequest();
// CODE TO INJECT REPO2 OBJECT DO Controller - method injection - works fine - the same assembly
builder.RegisterType<SolutionController>()
.OnActivating(e => e.Instance.SetMyDependency(new Repo2()));
// CODE TO INJECT REPO3 OBJECT To Another solution - method injection - does not work - another assembly
builder.RegisterType<Solution2>()
.OnActivating(e => e.Instance.SetMyDependency2(new Repo3()));
The Repo class has only one simple GetData() method with Debug.Write.
public void GetData()
{
System.Diagnostics.Debug.Write("works");
}
The class where I want to inject this Repo3 object. I got System.NullReferenceException.
public class SolutionCalculator
{
protected DataSolution _datasolution;
public SolutionCalculator(DataSolution datasolution)
{
_datasolution = datasolution;
}
public IRepo3 _repo3;
public void SetMyDependency2(IRepo3 myDependency)
{
_repo3 = myDependency;
}
I tried to fix that via multiple modifications in the code and I had consulted that problem with fellow programmers. I made some progress (mentioned in post). I have been looking for an answer for 4 days.