将SynchronizationContext与TrackingParticipant一起使用

时间:2012-08-29 12:24:18

标签: asp.net-mvc-3 nhibernate workflow-foundation-4 s#arp-architecture

我有自定义跟踪参与者:

public class ErsTrackingParticipant : TrackingParticipant, IErsTrackingParticipant
{
    private readonly INHibernateRepository<ErsTrackingRecord> _ersTrackingRecordRepository;

    public ErsTrackingParticipant(INHibernateRepository<ErsTrackingRecord> ersTrackingRecordRepository)
    {
        this._ersTrackingRecordRepository = ersTrackingRecordRepository;
    }

    protected override void Track(TrackingRecord record, TimeSpan timeout)
    {
        ErsTrackingRecord ersTrackingRecord = TrackingCommand.GetTrackingRecord(record);
        this._ersTrackingRecordRepository.Save(ersTrackingRecord);
    }
}

public interface IErsTrackingParticipant
{
}

和工作流程主持人:

public class WorkflowHostHelper: IWorkflowHostHelper
{
    private WorkflowApplication _workflowApplication;
    AutoResetEvent _instanceUnloaded = new AutoResetEvent(false);

    private IErsTrackingParticipant _ersTrackingParticipant;

    private bool _isCompleted = false;

    public string InstanceStoreConnectionString { get { return ConfigurationManager.AppSettings["InstanceStoreConnectionString"]; } }

    public WorkflowHostHelper(IErsTrackingParticipant ersTrackingParticipant)
    {
        _ersTrackingParticipant = ersTrackingParticipant;
    }

    public void Initialize(Activity workflowType)
    {
        _workflowApplication = new WorkflowApplication(workflowType);
        TrackingProfile trackingProfile = new TrackingProfile() { Name = "TroubleshootingProfile", ActivityDefinitionId = "*" };
        trackingProfile.Queries.Add(new WorkflowInstanceQuery { States = { "*" } });
        trackingProfile.Queries.Add(new ActivityStateQuery { States = { "*" } });
        trackingProfile.Queries.Add(new ActivityScheduledQuery());
        trackingProfile.Queries.Add(new BookmarkResumptionQuery() { Name = "*" });

        _workflowApplication.Extensions.Add(_ersTrackingParticipant);

        SynchronizationContext synchronizationContext = SynchronizationContext.Current;
        _workflowApplication.SynchronizationContext = synchronizationContext;
        _workflowApplication.InstanceStore = new SqlWorkflowInstanceStore(InstanceStoreConnectionString);

        _workflowApplication.PersistableIdle = (e) =>
            { return PersistableIdleAction.Persist; };
        _workflowApplication.Completed = (e) =>
            {
                _isCompleted = true;
                _instanceUnloaded.Set();
            };
        _workflowApplication.Idle = (e) =>
            { _instanceUnloaded.Set(); };

    } //Rest ommiitted...

当存储库尝试保存回数据库时,它会出错:

'((SharpArch.NHibernate.NHibernateRepositoryWithTypedId<EmployeeRequestSystem.Domain.ErsTrackingRecord,int>)(this._ersTrackingRecordRepository)).Session' threw an exception of type 'System.NullReferenceException'

SharpArch.NHibernate.Contracts.Repositories.INHibernateRepositoryWithTypedId<T,TId>.DbContext = {SharpArch.NHibernate.DbContext}

在工作流活动中,我可以引用上下文并使用服务定位器,例如:

    protected override void CacheMetadata(CodeActivityMetadata metadata)
    {
        base.CacheMetadata(metadata);
        metadata.AddDefaultExtensionProvider<INHibernateRepository<AuthorisationRequest>>(
            () => new NHibernateRepository<AuthorisationRequest>());
    }

...

    protected override void Execute(CodeActivityContext context)
    {
        var authorisationRequestRepoistory = context.GetExtension<INHibernateRepository<AuthorisationRequest>>();

如何通过我的ErsTrackingParticipant获取正确的上下文?

1 个答案:

答案 0 :(得分:1)

从你的问题来看,我不清楚SynchronizationContext与null引用有什么关系。如果你能确切地追踪到什么是null,那将会有所帮助。

在大多数情况下,SynchronizationContext.Current属性为null。只有在托管环境中通过某些东西进行设置时,它才会有值。我不会设置WorkflowApplication.Synchronization上下文属性,除非我发现SynchronizationContext.Current有一个值。

我不明白为什么你想要跟踪参与者中的“上下文”。你指的是什么“背景”?的SynchronizationContext? CodeActivityContext?