c# Unity依赖注入WebService
1.IOC与DI简介
IOC全称是Inversion Of Control(控制反转),不是一种技术,只是一种思想,一个重要的面相对象编程的法则,它能知道我们如何设计出松耦合,更优良的程序。传统应用程序都是由我们在类内部主动创建依赖对象,从而导致类与类之
间高耦合,难于测试;有了IoC容器后,把创建和查找依赖对象的控制权交给了容器,由容器进行注入组合对象,所以对象与对象之间是松散耦合,这样也方便测试,利于功能复用,更重要的是使得程序的整个体系结构变得非常灵活。其实IoC
对编程带来的最大改变不是从代码上,而是从思想上,发生了“主从换位”的变化。应用程序原本是老大,要获取什么资源都是主动出击,但是在IoC/DI思想中,应用程序就变成被动的了,被动的等待IoC容器来创建并注入它所需要的资源
了。
IoC很好的体现了面向对象设计法则之—— 法则:“别找我们,我们找你”;即由IoC容器帮对象找相应的依赖对象并注入,而不是由对象主动去找。
DI全称是Dependency Injection(依赖注入),是组件之间依赖关系有容器在运行期决定,即由容器动态的将某个依赖关系注入到组件之中,依赖注入的目的并非软件系统带来更多的功能,
而是为了提升组件重用的频率,并为系统搭建一个灵活可扩展的平台。通过依赖注入机制,我们只需要通过简单的配置,而无需代码指定目标需要的资源,完成自身的业务逻辑,而不需要关心具体的资源来自何处
DI的关键是:“谁依赖谁,为什么需要依赖,谁注入水,注入了什么”
2.Unity IOC简介
Unity是一个IOC容器,用来实现依赖注入(DI)减少耦合,出自于微软。
组件地址:http://unity.codeplex.com/
3.使用Unity依赖注入编写Web ServiceDemo
a。使用NuGet程序包管理安装引用Unity
b。在Global.asax中配置容器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Microsoft.Practices.Unity;
using WebServiceDemo.Impl;
using WebServiceDemo.Interface; namespace WebServiceDemo
{
public class Global : System.Web.HttpApplication,IContainerAccessor
{ private static IUnityContainer _container; public static IUnityContainer Container {
get { return _container; }
set { _container = value; } } IUnityContainer IContainerAccessor.Container
{
get
{
return Container;
}
} protected void Application_Start(object sender, EventArgs e)
{ log4net.Config.XmlConfigurator.Configure();
CreateContainer();
} protected void Session_Start(object sender, EventArgs e)
{ } protected void Application_BeginRequest(object sender, EventArgs e)
{ } protected void Application_AuthenticateRequest(object sender, EventArgs e)
{ } protected void Application_Error(object sender, EventArgs e)
{ } protected void Session_End(object sender, EventArgs e)
{ } protected void Application_End(object sender, EventArgs e)
{ } protected virtual void CreateContainer()
{
IUnityContainer container = new UnityContainer(); container.RegisterType<IWebServiceDemo, WebServiceDemo>(); Container = container;
}
}
}
c。创建服务接口基类
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebServiceDemo.Interface; namespace WebServiceDemo.Services
{
public abstract class BaseService<T> :System.Web.Services.WebService where T:class
{
public BaseService()
{
InjectDependencies();
} protected virtual void InjectDependencies()
{
HttpContext context = HttpContext.Current; if (context == null)
return; IContainerAccessor accessor = context.ApplicationInstance as IContainerAccessor; if (accessor == null)
return; IUnityContainer container = accessor.Container; if (container==null)
{
throw new InvalidOperationException("Container on Global Application Class is Null. Cannot perform BuildUp.");
} container.BuildUp(this as T); }
}
}
d。创建实现服务的接口和实现类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebServiceDemo.Dto; namespace WebServiceDemo.Interface
{
public interface IWebServiceDemo
{
RespResult<ResultOutput> AddResult(int a,int b);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebServiceDemo.customize;
using WebServiceDemo.Dto;
using WebServiceDemo.Interface; namespace WebServiceDemo.Impl
{
public class WebServiceDemoImpl : IWebServiceDemo
{ private static readonly ILog log = LogManager.GetLogger(typeof(BeingSweptServiceImpl)); public RespResult<ResultOutput> AddResult(int a,int b)
{
return a+b; }
}
}
e。创建配置Webservice
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using WebServiceDemo.customize;
using WebServiceDemo.Interface;
using Microsoft.Practices.Unity;
using WebServiceDemo.Dto; namespace WebServiceDemo.Services
{
/// <summary>
/// Summary description for MainSweepService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class ServiceDemoService : BaseService<ServiceDemoService>
{
[Dependency]
public IWebServiceDemo _webServiceDemo
{
get; set;
} public ServiceDemoService () : base()
{ } [WebMethod(Description ="加法")]
public RespResult<ResultOutput> AddResult(int a,int b)
{
return this._webServiceDemo.AddResult(a,b);
} } }
c# Unity依赖注入WebService的更多相关文章
- Unity 依赖注入之二
1. 构造子注入 1.1 构造子注入初级代码 container.RegisterType<IMyWork, MyWork>(new InjectionConstructor(new Bo ...
- Unity依赖注入使用详解
写在前面 构造器注入 Dependency属性注入 InjectionMethod方法注入 非泛型注入 标识键 ContainerControlledLifetimeManager单例 Unity注册 ...
- WPF PRISM开发入门二(Unity依赖注入容器使用)
这篇博客将通过一个控制台程序简单了解下PRISM下Unity依赖注入容器的使用.我已经创建了一个例子,通过一个控制台程序进行加减乘除运算,项目当中将输入输出等都用接口封装后,结构如下: 当前代码可以点 ...
- C# Unity依赖注入
简介: 控制反转:我们向IOC容器发出获取一个对象实例的一个请求,IOC容器便把这个对象实例“注入”到我们的手中,在这个过程中你不是一个控制者而是一个请求者,依赖于容器提供给你的资源,控制权落到了容器 ...
- Unity 依赖注入
关于Ioc的框架有很多,比如astle Windsor.Unity.Spring.NET.StructureMap,我们这边使用微软提供的Unity做示例,你可以使用Nuget添加Unity,也可以引 ...
- 使用Microsoft.Practices.Unity 依赖注入
Unity是微软Patterns & Practices团队所开发的一个轻量级的,并且可扩展的依赖注入(Dependency Injection)容器,它支持常用的三种依赖注入方式:构造器注入 ...
- 使用Microsoft.Practices.Unity 依赖注入 转载https://www.cnblogs.com/slardar1978/p/4205394.html
Unity是微软Patterns & Practices团队所开发的一个轻量级的,并且可扩展的依赖注入(Dependency Injection)容器,它支持常用的三种依赖注入方式:构造器注入 ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(6)-Unity 依赖注入
系列目录 前言 为了符合后面更新后的重构系统,文章于2016-11-1日重写 本节重构一下代码,采用IOC控制反转,也就是依赖注入 您可以访问http://unity.codeplex.com/rel ...
- Unity 依赖注入知识点
三种依赖注入方法,构造器注入.属性注入.方法注入 可以配置Config文件,来实现不用修改代码.需要先将接口与实体关联,然后使用时会自动加载对应实体. namespace WeChatConsole ...
随机推荐
- [Node.js] Test Node RESTful API with Mocha and Chai
In this lesson, we will use Chai's request method to test our Node application's API responses.By th ...
- [Angular] FormBuildAPI
Using FormBuilder API can simply our code, for example we want to refactor following code by using F ...
- 从 BM 到 RBM
1. 拓扑结构上 如下图示,在拓扑结构上,RBM(受限的玻尔兹曼机)与 BM(玻尔兹曼机)的最大区别在于: RBM 取消了可见层的层内连接以及隐含层的层内连接,主要在于 BM 的层内连接使得其学习过程 ...
- [Angular] Create a simple *ngFor
In this post, we are going to create our own structure directive *ngFor. What it should looks like i ...
- 【t073】&&【t015】魔法物品
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 有两种类型的物品:普通物品和魔法物品.每种普通物品有一个价值P,但每种魔法物品有两种价值:鉴定前的价值 ...
- c++ try catch 问题(没有throw就会被删除,加上/EHa这样就不会被删除)
以前都是用try{} catch(…){}来捕获C++中一些意想不到的异常, 今天看了Winhack的帖子才知道,这种方法在VC中其实是靠不住的.例如下面的代码: 以前都是用try{} catch(… ...
- matplotlib油漆基础
http://blog.csdn.net/pipisorry/article/details/37742423 matplotlib介绍 matplotlib 是python最著名的画图库,它提供了一 ...
- 《Windows核心编程》之“完成端口”(对所有IO都是如此,不仅仅是对socket)
<Windows核心编程>第10章开头部分一再强调:“IO Completion Port”是“构建高性能.可升缩的应用程序”的最佳设施之一,它不仅适用于处理设备IO,也适用于其它越来越多 ...
- maven 依赖(依赖范围,聚合,继承等)
目录: 1.什么是依赖? 2.依赖的管理:依赖的范围与传递,依赖的排除,依赖的原则(maven对依赖冲突的处理原则) 3.依赖的版本管理 4.继承与聚合 1.什么是依赖? 简单的讲,当jar包A需要j ...
- 【非常高%】【codeforces 733A】Grasshopper And the String
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...