对象激活主要包括
1.构造字符串
服务器苏建只能使用默认的构造函数,这样在对象创建的时候你就不能通过构造函数初始化数据.但是你可以使用构造字符串实现类似的功能,只是每次实例化的时候都只能使用相同的构造字符串.系统管理员可以改变构造字符串.(比如用到配置数据库的连接字符串).
通过[ConstructionEnabled]特性和其Default属性把默认的构造字符串添加到配置元数据中.在类内部你必须重写基类SericedComponent的Construct方法.当每次创建对象时,这种方法会被COM+基础结构调用.
2.即时激活(Just-in-Time Activation JITA)
JITA是一个用于减少服务器负载的特性.对于打开lJITA支持的组件,他的生命周期和他使用的客户端应用程序无关.这个服务器组件自己通过设置完成位来决定对象什么时候应该被终止.如果客户应用程序通过客户端的同一个引用来调用一个对象的方法,而这个对象在服务器端已经被终止的话,一个新的对象会被自动创建并激活.
JITA是通过设置[JustInTimeActiveation]来启用的.
要使用JITA,必须重写两个基类ServicedComponent的方法:Activate和Deactive.当对象生成后Activate方法会被运行时自动调用.当对象终止前Deactive方法会被自动调用.
为组件设置完成的两种方法:
3.对象池
对象池对于那种初始化过程很长的对象(比如,连接到一个老系统的服务器,或创建一个复杂的矩阵以进行数学运算)是个有用的服务.如果调用方法所需要的时间少于创建所需要的时间,应该考虑对象池技术.
对象的初始化过程在客户端第一次使用它之前进行:在应用程序启动后,为对象池设定的最小的对象就会被创建和初始化.
4.私有组件
私有组件是COM+1.5带来的新特性之一.被标记为[PrivateComplent]特性的组件只能由应用程序内部的对象激活,客户端应用程序不行.
using System;
using System.EnterpriseServices;
using System.Xml;
using System.Globalization;
using System.IO;
namespace Demo.Introduction
{
[ObjectPooling(true,100,1000)]
[JustInTimeActivation]
[ConstructionEnabled(Default = @"C:\Temp\")]
[EventTrackingEnabled]
public class CoursesComponent : ServicedComponent, ICourseOrder
{
private string path;
public CoursesComponent() { }
protected override void Construct(string s)
{
path = s;
}
#region ICourseOrder 成员
[AutoComplete]
public void Order(string xmlOrder)
{
CreateFile();
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlOrder);
XmlNodeList courses = doc.GetElementsByTagName("Course");
foreach (XmlNode nodeCourse in courses)
{
XmlElement xmlCourse = nodeCourse as XmlElement;
if (xmlCourse != null)
{
string courseNumber = xmlCourse.GetAttribute("Number");
string title = GetText(xmlCourse, "Title")[0];
DateTime date = DateTime.Parse(GetText(xmlCourse, "StartDate")[0]);
string[] attendees = GetText(xmlCourse, "Attendee");
for (int i = 0; i < attendees.Length; i++)
{
WritToFile(courseNumber, title, date, attendees[i]);
}
}
}
CloseFile();
}
#endregion
private StreamWriter writer = null;
/// <summary>
/// Create and opens aunique file
/// </summary>
private void CreateFile()
{
string uniqueName = Guid.NewGuid().ToString();
writer = new StreamWriter(path + uniqueName + ".txt");
}
private void CloseFile()
{
writer.Close();
}
protected override void Activate()
{
CreateFile();
}
protected override void Deactivate()
{
CloseFile();
}
/// <summary>
/// Write course information to the comma-separated file
/// </summary>
/// <param name="courseNumber"></param>
/// <param name="title"></param>
/// <param name="startDate"></param>
/// <param name="attendee"></param>
private void WritToFile(string courseNumber, string title, DateTime startDate, string attendee)
{
writer.WriteLine("{0};{1};{2};{3}", courseNumber, title, startDate.ToString("d", CultureInfo.InstalledUICulture), attendee);
}
/// <summary>
/// parses the xml data of a single course for the xml element
/// tagName to return the inner text elements
/// </summary>
/// <param name="xmlCourse"></param>
/// <param name="tagName"></param>
/// <returns></returns>
private string[] GetText(XmlElement xmlCourse, string tagName)
{
string[] text = null;
XmlNodeList nodeList = xmlCourse.GetElementsByTagName(tagName);
if (nodeList.Count < 1)
throw new Exception("No elements of type <" + tagName + "> available");
//CourseException("No elements of type <" + tagName + "> available");
text = new string[nodeList.Count];
for (int i = 0; i < nodeList.Count; i++)
{
XmlElement element = nodeList[i] as XmlElement;
if (element != null)
{
text[i] = element.InnerText;
}
}
return text;
}
}
}