概述
直接上代码
Web.config
<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>
</container>
<container name="session">
<register type="IStockQuoteService" mapTo="MoneyCentralStockQuoteService">
<lifetime type="singleton"/>
<property name="Logger"/>
</register>
</container>
</unity>
Global.asax
public class Global : System.Web.HttpApplication
{
private const string AppContainerKey = "application container";
private const string SessionContainerKey = "session container";
protected void Application_Start(object sender, EventArgs e)
{
IUnityContainer applicationContainer = new UnityContainer();
applicationContainer.LoadConfiguration("application");
ApplicationContainer = applicationContainer;
}
protected void Application_End(object sender, EventArgs e)
{
IUnityContainer applicationContainer = this.ApplicationContainer;
if (applicationContainer != null)
{
applicationContainer.Dispose();
this.ApplicationContainer = null;
}
}
protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
Page handler = HttpContext.Current.Handler as Page;
if (handler != null)
{
IUnityContainer container = SessionContainer;
if (container != null)
{
container.BuildUp(handler.GetType(), handler);
}
}
}
protected void Session_Start(object sender, EventArgs e)
{
IUnityContainer applicationContainer = this.ApplicationContainer;
if (applicationContainer != null)
{
IUnityContainer sessionContainer
= applicationContainer.CreateChildContainer();
sessionContainer.LoadConfiguration("session");
this.SessionContainer = sessionContainer;
}
}
protected void Session_End(object sender, EventArgs e)
{
IUnityContainer sessionContainer = this.SessionContainer;
if (sessionContainer != null)
{
sessionContainer.Dispose();
this.SessionContainer = null;
}
}
private IUnityContainer ApplicationContainer
{
get
{
return (IUnityContainer)this.Application[AppContainerKey];
}
set
{
this.Application[AppContainerKey] = value;
}
}
private IUnityContainer SessionContainer
{
get
{
return (IUnityContainer)this.Session[SessionContainerKey];
}
set
{
this.Session[SessionContainerKey] = value;
}
}
}
最后
以上就是勤奋书包为你收集整理的Microsoft.Practices.Unit在Web应用程序中的使用的全部内容,希望文章能够帮你解决Microsoft.Practices.Unit在Web应用程序中的使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复