1. asp.net集成unity示例
2. 示例代码下载
1. asp.net集成unity示例
1.1 添加引用,如下:
1.2 在Default.aspx页面中添加Dependency属性标签,表明该属性是通过依赖注入来生成。如下:
private IStockQuoteService stockQuoteService;
// 添加属性标签,表明的是需要注入属性
[Dependency]
public IStockQuoteService StockQuoteService
{
get { return stockQuoteService; }
set { stockQuoteService = value; }
}
1.3 添加Global.asax文件,在Application_Start中创建UnityContainer,并在Application_End函数中将UnityContainer销毁掉。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace StocksTicker
{
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.Web.Configuration;
using System.Web.UI;
public class Global : System.Web.HttpApplication
{
private const String AppContainerKey = "application container";
private IUnityContainer ApplicationContainer
{
get
{
return (IUnityContainer)this.Application[AppContainerKey];
}
set
{
this.Application[AppContainerKey] = value;
}
}
protected void Application_Start(object sender, EventArgs e)
{
// 在这里新建一个container
IUnityContainer applicationContainer = new UnityContainer();
applicationContainer.LoadConfiguration("application");
ApplicationContainer = applicationContainer;
}
protected void Application_End(object sender, EventArgs e)
{
// 销毁container
IUnityContainer applicationContainer = this.ApplicationContainer;
if (null != applicationContainer)
{
applicationContainer.Dispose();
this.ApplicationContainer = null;
}
}
// 这里实现拦截注入,在页面中实现注入
protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
Page handler = HttpContext.Current.Handler as Page;
if (null !=handler )
{
IUnityContainer container = this.ApplicationContainer ;
if ( null != container )
{
container.BuildUp(handler.GetType(), handler);
}
}
}
}
}
1.4 修改web.xml文件,增加配置节点configSection和向container中注册类型,详细信息参考这里
<section name="unity"
type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Version=2.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="TraceSource" type="System.Diagnostics.TraceSource, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<alias alias="ILogger" type="StocksTicker.Loggers.ILogger, StocksTicker"/>
<alias alias="TraceSourceLogger" type="StocksTicker.Loggers.TraceSourceLogger, StocksTicker"/>
<alias alias="IStockQuoteService" type="StocksTicker.StockQuoteServices.IStockQuoteService, StocksTicker"/>
<alias alias="MoneyCentralStockQuoteService" type="StocksTicker.StockQuoteServices.MoneyCentralStockQuoteService, StocksTicker"/>
<container name="application">
<register type="ILogger" mapTo="TraceSourceLogger">
<lifetime type="singleton"/>
<constructor>
<param name="traceSourceName">
<value value="default"/>
</param>
</constructor>
</register>
<register type="IStockQuoteService" mapTo="MoneyCentralStockQuoteService">
<lifetime type="singleton"/>
<property name="Logger"/>
</register>
</container>
</unity>
完成asp.net和unity的集成工作。