none
为什么对IOperationInvoker扩展时,postinvoke调用线程处理了多次? RRS feed

  • 问题

  • 我想通过对wcf调度程序扩展,实现服务运行时间的记录.

    对接口IOperationInvoker作了扩展:
    1.在其preinvoke中记录了时间a;
     public void PreInvoke(object instance, object[] inputs)
            {
                    try
                    {
                        DateTime BeginDateTime = DateTime.Now;
                        myType.ServiceName = instance.ToString();
                        myType.BeginDate = BeginDateTime.ToString("yyyyMMdd");
                        myType.BeginTime = BeginDateTime.ToString("hhmmss") + BeginDateTime.Millisecond.ToString("000");
                        myType.ClientIPAndPort = CommonFunction.GetRemotIpAddressAndPort();
                    }
                    catch (Exception ex)
                    {
                        ExceptionHandle.HandleException(ex);
                    }
            }
    2.在postinvoke中记录了时间b,获取自定义实例ID,通过将waitcallback委托传递给线程池向数据库记录时间a和b.
     public void PostInvoke(object instance, object returnValue, object[] outputs)
            {
                    try
                    {
                        //获取定义实例ID
                        myType.InstanceID = CommonFunction.GetInstanceID();

                        //记录时间b
                        DateTime EndDateTime = DateTime.Now;
                        myType.EndDate = EndDateTime.ToString("yyyyMMdd");
                        myType.EndTime = EndDateTime.ToString("hhmmss") + EndDateTime.Millisecond.ToString("000");

                        //线程处理,将时间a,b记录到数据库中
                        ThreadPool.QueueUserWorkItem(new WaitCallback(SetInfoThread), myType);
                    }
                    catch (Exception ex)
                    {
                        ExceptionHandle.HandleException(ex);
                    }
            }

          GetInstanceID和SetInfoThread都是自定义的方法,前者是获取一个Guid,后者是存储数据.
          运行结果是平均在上千条数据数据中有两条数据是一模一样的.如下:
     ID         INSTANCE_ID                                           BEGIN_DATE     BEGIN_TIME   END_DATE       END_TIME
    9054  0334a8fa-79cd-40bd-81d7-d70bd7bb5ad5        20100205         094121203      20100205        094121796     
    9055  0334a8fa-79cd-40bd-81d7-d70bd7bb5ad5        20100205         094121203      20100205        094121796

    ID是由db的Sequences生成的

    2010年2月11日 7:30

全部回复