C# 在 web.config 中配置的 UnityContainer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10111396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:28:33  来源:igfitidea点击:

UnityContainer configured in web.config

c#asp.netdependency-injectioninversion-of-controlunity-container

提问by dotnet-practitioner

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.

如何在 web.config 中实现第 2 行,以便我只需要在后面的代码中编写第 1、3 和 4 行?我已经搜索了代码示例的每个地方,但他们不清楚。

Thanks

谢谢

采纳答案by Wiktor Zychla

Take a look at my tutorial

看看我的教程

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

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

There's an example XML configuration:

有一个示例 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>

and you load it with

然后你加载它

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