UnityContainer配置在web.config中(UnityContainer config

2019-09-16 12:12发布

我有以下代码

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

如何实现在web.config中这样,我将只需要一行代码1,3线2条,并在我的代码比4落后? 我搜索了代码示例的每一个地方,但他们并不清楚。

谢谢

Answer 1:

看看我的教程

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

这里有一个示例XML配置:

<?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>

你加载它与

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


文章来源: UnityContainer configured in web.config