我有一个自定义跟踪参与者:
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 获得正确的上下文?