gpt4 book ai didi

c# - 工作流基础多个跟踪参与者不能一致工作

转载 作者:太空宇宙 更新时间:2023-11-03 23:31:00 25 4
gpt4 key购买 nike

我在 win7 下运行的 wf4.5 和 C# v4.0.30319 中自定义工作流基础跟踪参与者时遇到问题。单个跟踪参与者似乎工作可靠,但一旦我使用第二个跟踪参与者,无论事件是否被跟踪,我都会成为一个运气游戏。我已经检查了所有可能的堆栈溢出答案以及可能的每个可用的谷歌结果。

我创建了一个简单的应用程序来演示该行为。

我有一个简单的代码事件:

using System;
using System.Activities;

namespace WorkflowTrackingTestApp
{
public sealed class CodeActivity1 : CodeActivity
{
public InArgument<string> Text { get; set; }

protected override void Execute( CodeActivityContext context )
{
Console.WriteLine( "executing code activity " + typeof( string ).Assembly.ImageRuntimeVersion );
}
}
}

除了打印“执行代码事件和 c# 版本”之外,它什么都不做。

我有两个自定义跟踪参与者。请注意,它们都有一个跟踪配置文件,并且这些配置文件具有不同的名称,因为在另一个线程中有人建议这可能是问题所在。

namespace ActivityLibrary
{
using System;
using System.Activities.Tracking;

public class EventTrackingParticipant0 : TrackingParticipant
{
public EventTrackingParticipant0()
{
this.TrackingProfile = new TrackingProfile
{
Name = "CustomTrackingProfile0",
Queries =
{
new WorkflowInstanceQuery
{
// Limit workflow instance tracking records for started and
// completed workflow states.
States = {WorkflowInstanceStates.Started, WorkflowInstanceStates.Completed},
}
}
};
}

public Action<TrackingRecord> Received { get; set; }

protected override void Track( TrackingRecord record, TimeSpan timeout )
{
if( Received != null )
{
Received.BeginInvoke( record, BeginInvokeCallback, Received );
}
}

private void BeginInvokeCallback( IAsyncResult ar )
{
((Action<TrackingRecord>)ar.AsyncState).EndInvoke( ar );
}
}

public class EventTrackingParticipant1 : TrackingParticipant
{
public EventTrackingParticipant1()
{
this.TrackingProfile = new TrackingProfile
{
Name = "CustomTrackingProfile1",
Queries =
{
new WorkflowInstanceQuery
{
// Limit workflow instance tracking records for started and
// completed workflow states.
States = {WorkflowInstanceStates.Started, WorkflowInstanceStates.Completed},
}
}
};
}

public Action<TrackingRecord> Received { get; set; }

protected override void Track( TrackingRecord record, TimeSpan timeout )
{
if( Received != null )
{
Received.BeginInvoke( record, BeginInvokeCallback, Received );
}
}

private void BeginInvokeCallback( IAsyncResult ar )
{
((Action<TrackingRecord>)ar.AsyncState).EndInvoke( ar );
}

}
}

对于两个配置文件,代码基本相同。

现在进入主程序:

namespace WorkflowTrackingTestApp
{
using System;
using System.Activities;
using ActivityLibrary;

class Program
{
static void Main( string[] args )
{
EventTrackingParticipant0 tp0 = new EventTrackingParticipant0();
tp0.Received = tr => Console.WriteLine( "tracker0 tracking " + tr.EventTime.ToString( CultureInfo.InvariantCulture ) + " " + tr.RecordNumber);

EventTrackingParticipant1 tp1 = new EventTrackingParticipant1();
tp1.Received = tr => Console.WriteLine( "tracker1 tracking " + tr.EventTime.ToString( CultureInfo.InvariantCulture ) + " " + tr.RecordNumber );

Activity workflow1 = new CodeActivity1();
WorkflowInvoker wfInvoker = new WorkflowInvoker( workflow1 );

//add tracking participants
wfInvoker.Extensions.Add( tp0 );
wfInvoker.Extensions.Add( tp1 );

wfInvoker.Invoke( );
}
}
}

它添加两个跟踪参与者并执行代码事件。这是一些可能的结果:

executing code activity v4.0.30319
tracker0 tracking 08/26/2015 09:18:50 0
tracker1 tracking 08/26/2015 09:18:50 0
Press any key to continue . . .

executing code activity v4.0.30319
tracker0 tracking 08/26/2015 09:29:41 0
tracker0 tracking 08/26/2015 09:29:41 4
tracker1 tracking 08/26/2015 09:29:41 0
tracker1 tracking 08/26/2015 09:29:41 4
Press any key to continue . . .

executing code activity v4.0.30319
tracker0 tracking 08/26/2015 09:30:08 0
tracker1 tracking 08/26/2015 09:30:08 0
tracker0 tracking 08/26/2015 09:30:08 4
tracker1 tracking 08/26/2015 09:30:08 4
Press any key to continue . . .

executing code activity v4.0.30319
tracker0 tracking 08/26/2015 09:30:25 0
tracker1 tracking 08/26/2015 09:30:25 0
tracker1 tracking 08/26/2015 09:30:25 4
Press any key to continue . . .

有谁知道这种行为的原因是什么。

最佳答案

跟踪参与者是异步执行的,通常与事件执行不在同一个线程中。您的控制台应用程序有时会在参与者之一有时间执行之前退出。例如,如果您添加简单的 系统.Threading.Thread.Sleep(1000);在 wfInvoker.Invoke( ) 之后;您几乎总是会执行所有 4 个处理程序。

关于c# - 工作流基础多个跟踪参与者不能一致工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32222923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com