So I am using Unity MVC-4 for achieving Dependency Injection and it works great with my Controller classes but as soon as I try to inject in my non controller class, I get the NullReferenceException and I can see that my injected objects are not initialized by the framework. I will give you the corresponding classes that I am using:
Controller class (DI works):
public class HomeController : Controller
{
IMyService _myService;
#region CTOR
public HomeController(IMyService myService)
{
_myService = myService;
}
#endregion
public string GetMyString()
{
string mystring=string.Empty;
try
{
mystring = _myService.GetMyStringFromDLL();
}
catch (Exception ex)
{
StringBuilder str = new StringBuilder();
str.AppendLine("Exception in method GetMyString, Error msg: " + ex.Message);
WriteLog(sb);
}
return mystring;
}
}
And if I do the same thing in a non controller method (DI does not work here), I get a NullReferenceException:
public inteface IMyLogic
{
string GetMyString();
}
public class MyLogic: IMyLogic
{
IMyService _myService;
#region CTOR
public MyLogic(IMyService myService)
{
_myService = myService;
}
#endregion
public string GetMyString()
{
string mystring=string.Empty;
try
{
mystring = _myService.GetMyStringFromDLL(); //Getting NullReferenceException here
}
catch (Exception ex)
{
StringBuilder str = new StringBuilder();
str.AppendLine("Exception in method GetMyString, Error msg: " + ex.Message);
WriteLog(sb);
}
return mystring;
}
}
My BootStrapper.cs class looks like:
public static class Bootstrapper
{
public static IUnityContainer Initialise()
{
var container = BuildUnityContainer();
container.RegisterType<IMyService , MyService>();
container.RegisterType<IMyLogic, MyLogic>(new HierarchicalLifetimeManager());
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
return container;
}
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
RegisterTypes(container);
return container;
}
public static void RegisterTypes(IUnityContainer container)
{
}
}
If you see above in the line container.RegisterType<IMyService , MyService>();, the interface and its concrete implementation is in a separate module.
And my Global.asax.cs is:
protected void Application_Start()
{
Bootstrapper.Initialise();
AreaRegistration.RegisterAllAreas();
GlobalFilters.Filters.Add(new OfflineActionFilter());
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
How can I inject the IMyService in MyLogic class?
Unity.Mvc4runtime version? Can you expose yourIMyServiceandMyServicedefinitions, please. Because I put your code in my application (uses Framework v4.5 too) and it is working._myServicefield in theMyLogicclass isnull(not initialized - put break-point in the debugger and check it). This is means somehow theMyLogic.GetMyString()method is called before theUnityperformed registering the typeMyLogicmapping with the container. So, first, there is some code that related to problem and it not exposed in this post. Second, try to move two linescontainer.RegisterType<IMyService,...>(); container.RegisterType<IMyLogic,...);to theBootstrapper.BuildUnityContainer()method.Bootstrapper.Initialise();is first called to ensure that the container is mapped correctly with my service(s). I tried your other suggestion also and moved my service toBootstrapper.BuildUnityContainer()method but unfortunately the outcome is the same. I get the same error. How I can check the Unity.Mvc4 runtime version? Also exposing myIMyServicewould not have any effect since the error is generated because_myServiceobject is being received as null. I have double checkedMyServicelogic and everything looks right there.