我正在努力实现以下目标。我创建了长时间运行的工作流,它们按预定的时间间隔唤醒,即延迟活动并执行它们的工作,填充缓存。这些工作流从主应用程序开始,这是一个 IIS 7 托管的自定义 WCF 数据服务。启动工作流的触发器因用户登录、特定实体的请求而异。
目前,我创建了一个 WCF 工作流服务,它侦听作为标准请求合同一部分的触发器。收到触发器后,它会使用活动定义实例化 WorkflowApplication 并启动工作流。然而,这有其局限性,因为必须手动完成持久性和补液。
我遇到了 WorkflowServiceHost 的示例,它可以充当在故障转移的情况下支持持久性和再水化的主机,因为我们有一个网络农场环境。但是,当我尝试执行以下操作时,它会因各种错误而失败。所以只是想知道,如果我朝着正确的方向前进。或者这是不可能的。请找到基于 MSDN 示例的 IWorkflowCreation 合约实现代码:
` public Guid StartWorkflowReturnGuid(Dictionary<string,object> parameters)
{
Guid id = Guid.Empty;
try
{
System.ServiceModel.Activities.WorkflowServiceHost host = new System.ServiceModel.Activities.WorkflowServiceHost(this.workflow, new Uri("net.pipe://localhost"));
//string myConnectionString =
// "Data Source=localhost\\SQLEXPRESS;Initial Catalog=DefaultSampleStore;Integrated Security=True;Asynchronous Processing=True";
//SqlWorkflowInstanceStoreBehavior sqlWorkflowInstanceStoreBehavior =
// new SqlWorkflowInstanceStoreBehavior(myConnectionString);
//sqlWorkflowInstanceStoreBehavior.HostLockRenewalPeriod = TimeSpan.FromSeconds(30);
//sqlWorkflowInstanceStoreBehavior.RunnableInstancesDetectionPeriod = TimeSpan.FromSeconds(30);
//host.Description.Behaviors.Add(sqlWorkflowInstanceStoreBehavior);
WorkflowUnhandledExceptionBehavior workflowUnhandledExceptionBehavior =
new WorkflowUnhandledExceptionBehavior();
workflowUnhandledExceptionBehavior.Action = WorkflowUnhandledExceptionAction.Terminate;
//Set the TimeToUnload to 0 to force the WF to be unloaded. To have a durable delay, the WF needs to be unloaded otherwise it will be thread as an in-memory delay.
//WorkflowIdleBehavior workflowIdleBehavior = new WorkflowIdleBehavior()
//{
// TimeToUnload = TimeSpan.FromSeconds(0)
//};
//host.Description.Behaviors.Add(workflowIdleBehavior);
host.WorkflowExtensions.Add(new LoginExtensions());
host.WorkflowExtensions.Add(new MarketDataExtensions());
ResumeBookmarkEndpoint endpoint = new ResumeBookmarkEndpoint(new NetNamedPipeBinding(NetNamedPipeSecurityMode.None), new EndpointAddress(string.Format("net.pipe://localhost/workflowCreationEndpoint/{0}",Guid.NewGuid())));
host.AddServiceEndpoint(endpoint);
host.Open();
IWorkflowCreation client = new ChannelFactory<IWorkflowCreation>(endpoint.Binding, endpoint.Address).CreateChannel();
//create an instance
id = client.Create(parameters);
Helper.LogInfo(string.Format("Workflow instance {0} created", id));
}
catch (Exception exception)
{
Helper.LogError(string.Format("Exception starting BatchWorkflowServiceHost for type {0}", this.workflow.GetType().FullName), exception);
}
return id;
}
public class ResumeBookmarkEndpoint : WorkflowHostingEndpoint
{
public ResumeBookmarkEndpoint(Binding binding, EndpointAddress address)
: base(typeof(IWorkflowCreation), binding, address)
{
}
protected override Guid OnGetInstanceId(object[] inputs, OperationContext operationContext)
{
//Create called
if (operationContext.IncomingMessageHeaders.Action.EndsWith("Create"))
{
return Guid.Empty;
}
//CreateWithInstanceId or ResumeBookmark called. InstanceId is specified by client
else if (operationContext.IncomingMessageHeaders.Action.EndsWith("CreateWithInstanceId")||
operationContext.IncomingMessageHeaders.Action.EndsWith("ResumeBookmark"))
{
return (Guid)inputs[0];
}
else
{
throw new InvalidOperationException("Invalid Action: " + operationContext.IncomingMessageHeaders.Action);
}
}
protected override WorkflowCreationContext OnGetCreationContext(object[] inputs, OperationContext operationContext, Guid instanceId, WorkflowHostingResponseContext responseContext)
{
WorkflowCreationContext creationContext = new WorkflowCreationContext();
if (operationContext.IncomingMessageHeaders.Action.EndsWith("Create"))
{
Dictionary<string, object> arguments = (Dictionary<string, object>)inputs[0];
if (arguments != null && arguments.Count > 0)
{
foreach (KeyValuePair<string, object> pair in arguments)
{
//arguments for the workflow
creationContext.WorkflowArguments.Add(pair.Key, pair.Value);
}
}
//reply to client with the InstanceId
responseContext.SendResponse(instanceId, null);
}
else if (operationContext.IncomingMessageHeaders.Action.EndsWith("CreateWithInstanceId"))
{
Dictionary<string, object> arguments = (Dictionary<string, object>)inputs[0];
if (arguments != null && arguments.Count > 0)
{
foreach (KeyValuePair<string, object> pair in arguments)
{
//arguments for the workflow
creationContext.WorkflowArguments.Add(pair.Key, pair.Value);
}
}
}
else
{
throw new InvalidOperationException("Invalid Action: " + operationContext.IncomingMessageHeaders.Action);
}
return creationContext;
}
protected override System.Activities.Bookmark OnResolveBookmark(object[] inputs, OperationContext operationContext, WorkflowHostingResponseContext responseContext, out object value)
{
Bookmark bookmark = null;
value = null;
if (operationContext.IncomingMessageHeaders.Action.EndsWith("ResumeBookmark"))
{
//bookmark name supplied by client as input to IWorkflowCreation.ResumeBookmark
bookmark = new Bookmark((string)inputs[1]);
//value supplied by client as argument to IWorkflowCreation.ResumeBookmark
value = (string) inputs[2];
}
else
{
throw new NotImplementedException(operationContext.IncomingMessageHeaders.Action);
}
return bookmark;
}
}
//ServiceContract exposed on the endpoint
[ServiceContract(Name = "IWorkflowCreation")]
public interface IWorkflowCreation
{
[OperationContract(Name = "Create")]
Guid Create(IDictionary<string, object> inputs);
[OperationContract(Name = "CreateWithInstanceId", IsOneWay=true)]
void CreateWithInstanceId(Guid instanceId, IDictionary<string, object> inputs);
[OperationContract(Name = "ResumeBookmark", IsOneWay = true)]
void ResumeBookmark(Guid instanceId, string bookmarkName, string message);
}