Web.config and dll with Unity DI

Jun 15, 2014 at 11:26 PM
Hi,

I would ask about one thing...
I have some web App MVC. I added DI Unity for project. In global.asax.cs file I have a factory.
public class UnityControllerFactory : DefaultControllerFactory
{
private readonly IUnityContainer containter ;
public UnityControllerFactory (IUnityContainer conatiner )
{
this.containter=conatiner;
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{


return containter.Resolve(controllerType ) as IController; // here I expect some error but controller is coming from DLL...;/ I would like from web.config



}

}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

IUnityContainer container = new UnityContainer();
var controllerFactory = new UnityControllerFactory(container);
ControllerBuilder.Current.SetControllerFactory(controllerFactory);

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap { ExeConfigFilename = "Web.config" };
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
var unitySection = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
 var contain = new UnityContainer().LoadConfiguration(unitySection);

}
and also I have web.config but I don't have defined controlles like HomeController etc..

My question is ... Is any solution fot that I would like define controllers in web.config file but not take from DLL assembly.

Thank you in advance.
Lucas.
Jun 16, 2014 at 7:55 AM
Unity will resolve concrete types without any registration required. It will choose a constructor (the one with the most arguments) and then try to resolve the dependencies for that constructor and (if possible) return the object.

If you want to register the controllers in configuration you can do that: just call LoadConfiguration() on the UnityContainer. Based on the posted code it looks like you are using one container for the UnityControllerFactory and another container to load the configuration. You probably want to load the configuration into the UnityControllerFactory.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Jun 16, 2014 at 9:10 AM
Edited Jun 16, 2014 at 12:48 PM
Yes, I want to load the config into the UnitryControllerFactory.

Image
On the picture you can see.. is always HomeController... How can I change ? I would like full control for this container. I would like define containers in web.config ... But here I have default from DLL. Have you ever met with this problem?
Jun 17, 2014 at 6:49 AM
The controllerType is passed in by ASP.NET MVC to the Controllerfactory based on the RouteData. Unity will resolve (or attempt to resolve) whatever type is requested.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to