第2章 对象激活上下文-对象激活

对象激活主要包括

  • 构造字符串
  • 及时激活
  • 对象池
  • 私有组件

     

1.构造字符串

服务器苏建只能使用默认的构造函数,这样在对象创建的时候你就不能通过构造函数初始化数据.但是你可以使用构造字符串实现类似的功能,只是每次实例化的时候都只能使用相同的构造字符串.系统管理员可以改变构造字符串.(比如用到配置数据库的连接字符串).

通过[ConstructionEnabled]特性和其Default属性把默认的构造字符串添加到配置元数据中.在类内部你必须重写基类SericedComponent的Construct方法.当每次创建对象时,这种方法会被COM+基础结构调用.

2.即时激活(Just-in-Time Activation JITA)

JITA是一个用于减少服务器负载的特性.对于打开lJITA支持的组件,他的生命周期和他使用的客户端应用程序无关.这个服务器组件自己通过设置完成位来决定对象什么时候应该被终止.如果客户应用程序通过客户端的同一个引用来调用一个对象的方法,而这个对象在服务器端已经被终止的话,一个新的对象会被自动创建并激活.

JITA是通过设置[JustInTimeActiveation]来启用的.

要使用JITA,必须重写两个基类ServicedComponent的方法:Activate和Deactive.当对象生成后Activate方法会被运行时自动调用.当对象终止前Deactive方法会被自动调用.

为组件设置完成的两种方法:

  • [AutoComplate]
  • 设置ContextUtil的静态属性DeactivateOnReturn为True.
3.对象池

对象池对于那种初始化过程很长的对象(比如,连接到一个老系统的服务器,或创建一个复杂的矩阵以进行数学运算)是个有用的服务.如果调用方法所需要的时间少于创建所需要的时间,应该考虑对象池技术.

对象的初始化过程在客户端第一次使用它之前进行:在应用程序启动后,为对象池设定的最小的对象就会被创建和初始化.

4.私有组件

私有组件是COM+1.5带来的新特性之一.被标记为[PrivateComplent]特性的组件只能由应用程序内部的对象激活,客户端应用程序不行.

  1 None.gif using  System;
  2 None.gif using  System.EnterpriseServices;
  3 None.gif using  System.Xml;
  4 None.gif using  System.Globalization;
  5 None.gif using  System.IO;
  6 None.gif
  7 None.gif
  8 None.gif namespace  Demo.Introduction
  9 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 10InBlock.gif    [ObjectPooling(true,100,1000)]
 11InBlock.gif    [JustInTimeActivation]
 12InBlock.gif    [ConstructionEnabled(Default = @"C:\Temp\")]
 13InBlock.gif    [EventTrackingEnabled]
 14InBlock.gif    public class CoursesComponent : ServicedComponent, ICourseOrder
 15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 16InBlock.gif        private string path;
 17InBlock.gif
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        public CoursesComponent() dot.gif{ }
 19InBlock.gif
 20InBlock.gif        protected override void Construct(string s)
 21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 22InBlock.gif            path = s;
 23ExpandedSubBlockEnd.gif        }

 24InBlock.gif
 25ContractedSubBlock.gifExpandedSubBlockStart.gif        ICourseOrder 成员#region ICourseOrder 成员
 26InBlock.gif        [AutoComplete]
 27InBlock.gif        public void Order(string xmlOrder)
 28ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 29InBlock.gif            CreateFile();
 30InBlock.gif            XmlDocument doc = new XmlDocument();
 31InBlock.gif            doc.LoadXml(xmlOrder);
 32InBlock.gif            XmlNodeList courses = doc.GetElementsByTagName("Course");
 33InBlock.gif            foreach (XmlNode nodeCourse in courses)
 34ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 35InBlock.gif                XmlElement xmlCourse = nodeCourse as XmlElement;
 36InBlock.gif                if (xmlCourse != null)
 37ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 38InBlock.gif                    string courseNumber = xmlCourse.GetAttribute("Number");
 39InBlock.gif                    string title = GetText(xmlCourse, "Title")[0];
 40InBlock.gif                    DateTime date = DateTime.Parse(GetText(xmlCourse, "StartDate")[0]);
 41InBlock.gif                    string[] attendees = GetText(xmlCourse, "Attendee");
 42InBlock.gif                    for (int i = 0; i < attendees.Length; i++)
 43ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 44InBlock.gif                        WritToFile(courseNumber, title, date, attendees[i]);
 45ExpandedSubBlockEnd.gif                    }

 46ExpandedSubBlockEnd.gif                }

 47ExpandedSubBlockEnd.gif            }

 48InBlock.gif            CloseFile();
 49ExpandedSubBlockEnd.gif        }

 50InBlock.gif
 51ExpandedSubBlockEnd.gif        #endregion

 52InBlock.gif
 53InBlock.gif        private StreamWriter writer = null;
 54InBlock.gif
 55ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 56InBlock.gif        /// Create and opens aunique file
 57ExpandedSubBlockEnd.gif        /// </summary>

 58InBlock.gif        private void CreateFile()
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 60InBlock.gif            string uniqueName = Guid.NewGuid().ToString();
 61InBlock.gif            writer = new StreamWriter(path + uniqueName + ".txt");
 62ExpandedSubBlockEnd.gif        }

 63InBlock.gif
 64InBlock.gif        private void CloseFile()
 65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 66InBlock.gif            writer.Close();
 67ExpandedSubBlockEnd.gif        }

 68InBlock.gif
 69InBlock.gif        protected override void Activate()
 70ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 71InBlock.gif            CreateFile();
 72ExpandedSubBlockEnd.gif        }

 73InBlock.gif
 74InBlock.gif        protected override void Deactivate()
 75ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 76InBlock.gif            CloseFile();
 77ExpandedSubBlockEnd.gif        }

 78InBlock.gif
 79ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 80InBlock.gif        /// Write course information to the comma-separated file
 81InBlock.gif        /// </summary>
 82InBlock.gif        /// <param name="courseNumber"></param>
 83InBlock.gif        /// <param name="title"></param>
 84InBlock.gif        /// <param name="startDate"></param>
 85ExpandedSubBlockEnd.gif        /// <param name="attendee"></param>

 86InBlock.gif        private void WritToFile(string courseNumber, string title, DateTime startDate, string attendee)
 87ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 88InBlock.gif            writer.WriteLine("{0};{1};{2};{3}", courseNumber, title, startDate.ToString("d", CultureInfo.InstalledUICulture), attendee);
 89ExpandedSubBlockEnd.gif        }

 90InBlock.gif
 91ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 92InBlock.gif        /// parses the xml data of a single course for the xml element
 93InBlock.gif        /// tagName to return the inner text elements
 94InBlock.gif        /// </summary>
 95InBlock.gif        /// <param name="xmlCourse"></param>
 96InBlock.gif        /// <param name="tagName"></param>
 97ExpandedSubBlockEnd.gif        /// <returns></returns>

 98InBlock.gif        private string[] GetText(XmlElement xmlCourse, string tagName)
 99ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
100InBlock.gif            string[] text = null;
101InBlock.gif            XmlNodeList nodeList = xmlCourse.GetElementsByTagName(tagName);
102InBlock.gif            if (nodeList.Count < 1)
103InBlock.gif                throw new Exception("No elements of type <" + tagName + "> available");
104InBlock.gif            //CourseException("No elements of type <" + tagName + "> available");
105InBlock.gif            text = new string[nodeList.Count];
106InBlock.gif            for (int i = 0; i < nodeList.Count; i++)
107ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
108InBlock.gif                XmlElement element = nodeList[i] as XmlElement;
109InBlock.gif                if (element != null)
110ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
111InBlock.gif                    text[i] = element.InnerText;
112ExpandedSubBlockEnd.gif                }

113ExpandedSubBlockEnd.gif            }

114InBlock.gif            return text;
115ExpandedSubBlockEnd.gif        }

116ExpandedSubBlockEnd.gif    }

117ExpandedBlockEnd.gif}

118 None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值