对象激活主要包括
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]特性的组件只能由应用程序内部的对象激活,客户端应用程序不行.
1
using System;
2
using System.EnterpriseServices;
3
using System.Xml;
4
using System.Globalization;
5
using System.IO;
6
7
8
namespace Demo.Introduction
9

{
10
[ObjectPooling(true,100,1000)]
11
[JustInTimeActivation]
12
[ConstructionEnabled(Default = @"C:\Temp\")]
13
[EventTrackingEnabled]
14
public class CoursesComponent : ServicedComponent, ICourseOrder
15
{
16
private string path;
17
18
public CoursesComponent()
{ }
19
20
protected override void Construct(string s)
21
{
22
path = s;
23
}
24
25
ICourseOrder 成员#region ICourseOrder 成员
26
[AutoComplete]
27
public void Order(string xmlOrder)
28
{
29
CreateFile();
30
XmlDocument doc = new XmlDocument();
31
doc.LoadXml(xmlOrder);
32
XmlNodeList courses = doc.GetElementsByTagName("Course");
33
foreach (XmlNode nodeCourse in courses)
34
{
35
XmlElement xmlCourse = nodeCourse as XmlElement;
36
if (xmlCourse != null)
37
{
38
string courseNumber = xmlCourse.GetAttribute("Number");
39
string title = GetText(xmlCourse, "Title")[0];
40
DateTime date = DateTime.Parse(GetText(xmlCourse, "StartDate")[0]);
41
string[] attendees = GetText(xmlCourse, "Attendee");
42
for (int i = 0; i < attendees.Length; i++)
43
{
44
WritToFile(courseNumber, title, date, attendees[i]);
45
}
46
}
47
}
48
CloseFile();
49
}
50
51
#endregion
52
53
private StreamWriter writer = null;
54
55
/**//// <summary>
56
/// Create and opens aunique file
57
/// </summary>
58
private void CreateFile()
59
{
60
string uniqueName = Guid.NewGuid().ToString();
61
writer = new StreamWriter(path + uniqueName + ".txt");
62
}
63
64
private void CloseFile()
65
{
66
writer.Close();
67
}
68
69
protected override void Activate()
70
{
71
CreateFile();
72
}
73
74
protected override void Deactivate()
75
{
76
CloseFile();
77
}
78
79
/**//// <summary>
80
/// Write course information to the comma-separated file
81
/// </summary>
82
/// <param name="courseNumber"></param>
83
/// <param name="title"></param>
84
/// <param name="startDate"></param>
85
/// <param name="attendee"></param>
86
private void WritToFile(string courseNumber, string title, DateTime startDate, string attendee)
87
{
88
writer.WriteLine("{0};{1};{2};{3}", courseNumber, title, startDate.ToString("d", CultureInfo.InstalledUICulture), attendee);
89
}
90
91
/**//// <summary>
92
/// parses the xml data of a single course for the xml element
93
/// tagName to return the inner text elements
94
/// </summary>
95
/// <param name="xmlCourse"></param>
96
/// <param name="tagName"></param>
97
/// <returns></returns>
98
private string[] GetText(XmlElement xmlCourse, string tagName)
99
{
100
string[] text = null;
101
XmlNodeList nodeList = xmlCourse.GetElementsByTagName(tagName);
102
if (nodeList.Count < 1)
103
throw new Exception("No elements of type <" + tagName + "> available");
104
//CourseException("No elements of type <" + tagName + "> available");
105
text = new string[nodeList.Count];
106
for (int i = 0; i < nodeList.Count; i++)
107
{
108
XmlElement element = nodeList[i] as XmlElement;
109
if (element != null)
110
{
111
text[i] = element.InnerText;
112
}
113
}
114
return text;
115
}
116
}
117
}
118