我是靠谱客的博主 勤奋书包,最近开发中收集的这篇文章主要介绍Microsoft.Practices.Unit在Web应用程序中的使用,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

直接上代码

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应用程序中的使用所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部