要减少多次创建对象带来的开销,比如创建对象比调用对象上方法的代价更多,可以考虑使用Object Pooling: [ObjectPooling(10,100)] class XXX : ServicedComponent { }
我想实现的效果是:在客户调用COM+组件的时候 //client code if (GetCOMPlusObjectByName("xxx") == FALSE) { //create COMPLUS object by name "xxx" //........ } else { //just use the exist COM+ object and call its methods } 请问应该如何做
COM+组件类标记为JustInTimeActivationAttribute,这样对象的生命周期不受客户端控制try://client code if (GetCOMPlusObjectByName("xxx") == FALSE) { component.Dispose(); component = new Xxx... // a newly created com+ object } else { component = new Xxx... // reuse the exist com+ object }
[ObjectPooling(10,100)]
class XXX : ServicedComponent
{
}
//client code
if (GetCOMPlusObjectByName("xxx") == FALSE)
{
//create COMPLUS object by name "xxx"
//........
}
else
{
//just use the exist COM+ object and call its methods
}
请问应该如何做
if (GetCOMPlusObjectByName("xxx") == FALSE)
{
component.Dispose();
component = new Xxx... // a newly created com+ object
}
else
{
component = new Xxx... // reuse the exist com+ object
}
另外按照我对上面发言的理解,com+本身不提供named object pool这样的机制,还是需要自己实现singleton,如果用这种方式,那么如何才能保证com+ object不会被系统destroy而导致信息数据丢失
只是注意,这个COM+类不能有[AutoComplete]的方法,如果有的话则客户端不要调用,否则对象会终止,然后下次请求时会自动新建一个新的对象
由于设置ContextUtil.DeactivateOnReturn=true和[AutoComplete]特性的作用等效,所以方法体内也不要用ContextUtil.DeactivateOnReturn=true