我正在努力实现以下目标。我创建了长时间运行的工作流,它们按计划的时间间隔唤醒,即延迟活动并执行其工作,填充缓存。这些工作流程从主应用程序启动,主应用程序是IIS 7托管的自定义WCF数据服务。启动工作流程的触发器因用户登录,特定实体的请求而异。
目前,我已经创建了一个WCF工作流服务,用于侦听属于标准请求协定的触发器。收到触发器后,它会使用“活动定义”实例化WorkflowApplication并启动工作流程。但这有其局限性,因为持久性和补液必须手动完成。
我遇到了WorkflowServiceHost示例,它可以作为主机,在发生故障转移时支持持久性和补水,因为我们有一个Web场环境。但是,当我尝试执行类似下面的操作时,它会因各种错误而失败。所以我只是想知道,如果我朝着正确的方向前进的话。或者这是不可能的。请查找基于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);
}
答案 0 :(得分:1)
你要做的事当然是可能的。但是你的问题提出了一个问题。您说您有一个WCF工作流服务,这意味着您已经在使用WorkflowServiceHost。
就使用WorkfowServiceHost而言。可以使用IWorkflowCreation自行创建和管理工作流,但有一些示例可以做,但通常的方法是使用WCF-WF4基础结构为您创建工作流。创建工作流,添加Receive活动并将CanCreateInstance设置为true,并向该活动发送WCF请求将为您创建新的工作流实例。
如果您希望使用具有持久性的WorklflowApplication,但您必须自己管理每个实例。没有自动重新加载,如果您必须管理单个实例,那么您将检查过期的计时器并恢复实例。故障转移也可以正常工作,它使用相同的SqlWorkflowInstanceStore来实际保存和加载工作流状态。