0

我有一个自定义跟踪参与者:

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 获得正确的上下文?

4

1 回答 1

1

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

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

我不明白您为什么要在跟踪参与者中添加“上下文”。您指的是什么“上下文”?同步上下文?代码活动上下文?

于 2012-08-30T19:25:08.913 回答