1

I have the controller, where I am doing the DI. I have 1 constructor with Interface Injected. Hence I have created a Default constructor with out any parameter. (Because if we don't do this then Issue comes as "Make sure that the controller has a parameterless public constructor")

I have the below parametezied constructor:

private IDAL DAL;
private IBLL BLL;

public ABCController(IDAL DalLayer, IBLL BllLayer)
{
    DAL = DalLayer;
    BLL = BllLayer;
}

How to call this constructor in the below parameter-less constructor? I have used :this(), but here the issue is throwing DalLayer,BllLayer are undefined. Because those were declared in the other constructor:

public ABCController():this(DalLayer,BllLayer)
{

}

How to resolve the issue?

scopchanov
  • 7,966
  • 10
  • 40
  • 68
  • 2
    `public G3MSController() : this(null, null) {}` – Dmitry Bychenko Sep 12 '18 at 11:44
  • Sounds like you've not registered either (or both) the IDAL or IBLL with the DI. So the DI is not sure how to construct the ABCController. For example, in unity you could use `container.RegisterType();` 'DAL' of course being the implmentation of your IDAL interface. – Shane Yu Sep 12 '18 at 11:53
  • what DI framework are you using? – Kenneth Garza Sep 12 '18 at 11:57
  • Nope that code I haven't shared, but I have registered. :) Unity I am using currently. –  Sep 12 '18 at 12:00
  • Is it too late to change from Unity to something like [Autofac](https://autofac.org/)? ;) Because Unity isn't the greatest IoC container... – Matthew Watson Sep 12 '18 at 12:14
  • Yes, its Org standard . –  Sep 12 '18 at 12:25
  • @matthew-watson I don't think suggesting using a different DI is the right thing to suggest here, it does not sort out his problem; unity in his case is not configured correctly. – Shane Yu Sep 12 '18 at 12:28
  • @LokanathDas Have your DAL and BLL implementations got other dependencies? Are those also registered with unity? The error thrown should contain information in the stack trace that should indicate what is wrong, other than it needing a parameterless constructor. – Shane Yu Sep 12 '18 at 12:30
  • message="Type 'ABC_Api.Controllers.ABCController' does not have a default constructor" source="System.Core" detail="System.Web.HttpUnhandledException (0x80004005): An error occurred when trying to create a controller of type 'ABCController'. Make sure that the controller has a parameterless public constructor. ---> System.InvalidOperationException: An error occurred when trying to create a controller of type 'ABCController'. Make sure that the controller has a parameterless public constructor –  Sep 12 '18 at 12:33
  • Nothing else in the stack trace? That's a shame. Just make sure that you've registered any other interfaces/services that the DAL and BLL also use as it needs to construct and inject all the way through. Are you able to share more of the code? Is it on github or anywhere we can take a look to aid you? – Shane Yu Sep 12 '18 at 12:35
  • IN unit config I have registered below ` public static void RegisterComponents() { var container = new UnityContainer(); // register all your components with the container here // it is NOT necessary to register your controllers // e.g. container.RegisterType(); container.RegisterType(); container.RegisterType(); DependencyResolver.SetResolver(new UnityDependencyResolver(container)); }` –  Sep 12 '18 at 12:39
  • Sorry , m unable to format the code in the comment section. UnityConfig.RegisterComponents(); // In the App_start() in global.asax.cs even I have done –  Sep 12 '18 at 12:39
  • No worries. Does either of your DAL or BLL take anything in their constructors? – Shane Yu Sep 12 '18 at 12:41
  • No its simply an implementation class. Where I am implementing the IDAL & IBLL. Nothing is there or declared any Constructor. its by default constructor. –  Sep 12 '18 at 12:46
  • Hmm, the only reason the error would be raised with message "Make sure that the controller has a parameterless public constructor" is when it is trying to instantiate a new ABCController but cannot because it is unable to resolve the parameters the controller needs. Without seeing more code I am unsure how to help you; you just need to ensure that all services are registered with unity and that the unity container is correctly being set as the resolver for your application. – Shane Yu Sep 12 '18 at 12:50
  • What kind of application have you created? I am assuming from the comments in the pasted code that this is a MVC5 application with the Unity.MVC5 package installed. – Shane Yu Sep 12 '18 at 13:06
  • Yes it 4.5 API, where I am Using DI for controllers.. –  Sep 12 '18 at 13:07
  • public ABCController() : this(null, null) {} This is not a fix, but build issue is not coming. but as its null assigned to it, where ever I am calling the methods accosicated with DAL & BLL methods its throwing Object instance not set –  Sep 12 '18 at 13:08
  • I am putting together a quick example for you, hold tight – Shane Yu Sep 12 '18 at 13:09
  • Ah, quick question...after installing the Unity MVC package did you add the line 'UnityConfig.RegisterComponents();' to your Application_Start method? Along side the FilterConfig and RouteConfig lines. `RouteConfig.RegisterRoutes(RouteTable.Routes);` https://code.msdn.microsoft.com/Dependency-Injection-in-11d54863 – Shane Yu Sep 12 '18 at 13:23
  • Yes it is there. –  Sep 12 '18 at 14:18
  • Can you host the code somewhere like github? If so I can take a look, otherwise i'd just creating a new project starting over following the link I posted. – Shane Yu Sep 12 '18 at 14:47
  • Its something I cant post anywhere.. But the scenario is like this above code snippet. I registered my Interfaces , I have setup the resolver. Everything is done... But I haven't registered the Controller any where(Hopefully this is not required) –  Sep 14 '18 at 06:09

2 Answers2

0

Here the issue was user was using Unity.MVC5, but where it requires Unity.WEbAPI. User assumption was Unity framework should be identiacal & it should contain all type(i.e. WEbAPI,.net, Mvc , mvp etc..) but its not like that. Unity framework need to be specific to the requirement.

As the resolver for all of the Unity framework are different.

lokanath das
  • 736
  • 1
  • 10
  • 35
  • In this case, only Interface suppose to be Registered in the Unityconfig . After that the controller resolver will automatically resolve the issue. In the above case given , default constructor was not required. It was only the Prameterized constructor required. – lokanath das Sep 14 '18 at 08:48
-1

You can create a factory responsible for creating objects. Unfortunately You didn't specify which library do you use for DL. If you use ninject there is a method that allow you to pass additional arguments into object to inject for example MyClass m = kernel.Get<MyClass>( new ConstructorArgument( "i", 2) ); following this topic

I dont have knowledge about how It's looks in other DL libraries.

Asvv
  • 44
  • 1
  • 5
  • His problem is the setup of unity, suggesting factories or other things is just confusing the matter. – Shane Yu Sep 12 '18 at 12:31