对象激活主要包括
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
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
namespace
Demo.Introduction
9
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
10
[ObjectPooling(true,100,1000)]
11
[JustInTimeActivation]
12
[ConstructionEnabled(Default = @"C:\Temp\")]
13
[EventTrackingEnabled]
14
public class CoursesComponent : ServicedComponent, ICourseOrder
15![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16
private string path;
17![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public CoursesComponent()
{ }
19![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
protected override void Construct(string s)
21![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22
path = s;
23
}
24![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
ICourseOrder 成员#region ICourseOrder 成员
26
[AutoComplete]
27
public void Order(string xmlOrder)
28![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29
CreateFile();
30
XmlDocument doc = new XmlDocument();
31
doc.LoadXml(xmlOrder);
32
XmlNodeList courses = doc.GetElementsByTagName("Course");
33
foreach (XmlNode nodeCourse in courses)
34![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
35
XmlElement xmlCourse = nodeCourse as XmlElement;
36
if (xmlCourse != null)
37![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
44
WritToFile(courseNumber, title, date, attendees[i]);
45
}
46
}
47
}
48
CloseFile();
49
}
50![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
#endregion
52![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
private StreamWriter writer = null;
54![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
56
/// Create and opens aunique file
57
/// </summary>
58
private void CreateFile()
59![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
60
string uniqueName = Guid.NewGuid().ToString();
61
writer = new StreamWriter(path + uniqueName + ".txt");
62
}
63![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
private void CloseFile()
65![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
66
writer.Close();
67
}
68![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
protected override void Activate()
70![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
71
CreateFile();
72
}
73![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
protected override void Deactivate()
75![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
76
CloseFile();
77
}
78![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
88
writer.WriteLine("{0};{1};{2};{3}", courseNumber, title, startDate.ToString("d", CultureInfo.InstalledUICulture), attendee);
89
}
90![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
108
XmlElement element = nodeList[i] as XmlElement;
109
if (element != null)
110![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
111
text[i] = element.InnerText;
112
}
113
}
114
return text;
115
}
116
}
117
}
118