dotnet-practitioner
dotnet-practitioner

Reputation: 14148

UnityContainer configured in web.config

I have the following code

var container = new UnityContainer();                       //LINE 1
container.RegisterType<ILogUtility,LogUtil>();              //LINE 2
var logger = container.Resolve<Logger>();                   //LINE 3
logger.Log(LogType.Warn, "logging from container");         //LINE 4

How do I implement line 2 in web.config such that I will only have to code line 1, 3, and 4 in my code behind? I have searched every where for code example but they are not clear.

Thanks

Upvotes: 3

Views: 23395

Answers (1)

Wiktor Zychla
Wiktor Zychla

Reputation: 48230

Take a look at my tutorial

http://netpl.blogspot.com/2011/11/unity-application-block-is-lightweight.html

There's an example XML configuration:

<?xml version="1.0" encoding="utf-8" ?>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">       
<container>        
<register type="ConsoleApplication30.Logic.ICustomService, ConsoleApplication30"                  
          mapTo="ConsoleApplication30.Logic.CustomServiceImpl, ConsoleApplication30" />    
</container></unity>

and you load it with

IUnityContainer container = new UnityContainer();                
container.LoadConfiguration();

Upvotes: 12

Related Questions