分类:
IOC
1、引用unity.dll 和 unity.Interception.dll
2、文件配置--将配置文件属性改为始终复制
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/> <!--<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />--> </configSections> <unity> <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,Unity.Interception.Configuration"/> <assembly name="Service"/> <containers> <container name="defaultContainer"> <register type="Service.IService.IUserinfoService, Service" mapTo="Service.ServiceImp.UserinfoService, Service"/> </container> </containers> </unity> </configuration>
3、调用
private static IUnityContainer _container; private static readonly unityhelper dbinstance = new unityhelper("defaultContainer"); private unityhelper(string containerName) { //UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity"); //_container = new UnityContainer(); //section.Configure(_container, containerName); ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "bin\\UnityIocHelper\\unity.config");//找配置文件的路径 Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); UnityConfigurationSection section = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName); _container = new UnityContainer(); section.Configure(_container, containerName); }
4、调用的类与实现的类呈现继承关系
评价
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术