现在的位置: 首页 > 综合 > 正文

创建第一个COM+,Enterprise Service,JITA

2012年08月09日 ⁄ 综合 ⁄ 共 1635字 ⁄ 字号 评论关闭

第一次接触这个东西,先从JustInTimeActivation的概念入手。

JITA机制适用于于对象创建代价不甚昂贵、然而其独占的资源却极其有限的应用场合。 用来提高无状态组件的使用效率。

 

我创建了以下这个程序,非常简单,环境是vs2005,framework2.0.

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.EnterpriseServices;
using System.Reflection;

namespace ComConsoleApplication
{
   
    class Program
    {
        static void Main(string[] args)
        {
            JITA jita = new JITA();
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(jita.GetCreateStamp());
                Thread.Sleep(2000);
            }
            Console.ReadLine();
        }
    }

    [JustInTimeActivation(true)]
    public class JITA : ServicedComponent
    {
        private DateTime timeStamp;

        public JITA()
        {
            timeStamp = DateTime.Now;
        }
        //class implementation

        public DateTime GetCreateStamp()
        {
            ContextUtil.DeactivateOnReturn = true;
            return timeStamp;
        }
    }
}

 

如果你直接运行的话,那就会遇到这个错误invalid servicedComponent-derived classes were found in the assembly

解决很简单,把AssemblyInfo.cs中的comvisible设为true

[assembly: ComVisible(true)]

然后你还会遇到下面这个错误does not have a strong name

很简单啦,   sn –k keyfile.snk

然后指定一下snk文件。

这个程序的效果是:如果 [JustInTimeActivation(true)],而且 ContextUtil.DeactivateOnReturn = true;
没有被注释掉,那么console里面显示的日期都是不一样的。

如果设为false,或者被注释的话ContextUtil.DeactivateOnReturn = true;,那么每次显示的时间都是一样的,都是第一次实例化的那个时间点。

Remark:

The COM+ done bit determines how long the object remains active after finishing its work and can affect the duration of a transaction. When a method call returns, COM+ inspects the done bit. If the done bit is true, COM+ deactivates the object. If the done bit is false, the object is not deactivated.

 

参考:

Expert.ASP.NET.2.0.Advanced.Application.Design

MSDN

抱歉!评论已关闭.