快捷搜索:

WF4:宿主No-Service Workflow

我们在应用事情流办事时,一样平常会在事情流设计中应用Receive等消息活动,然后应用WorkflowServiceHost进行宿主,假如没有 Receive活动,事情流就无法吸收发送来的消息,这个时刻我们可以应用WorkflowHostingEndpoint类:

要宿主一个没有办事的事情流,必要承袭WorkflowHostingEndpoint并重写 OnGetInstanceId,OnGetCreationContext,OnResolveBookmark。首先我们要声明一个办事左券,该左券包孕一个创建新实例的办事操作,这个操作要传一个IDictionary参数,这个左券隐式被 WorkflowHostingEndpoint实现。当宿主这个事情流时,应用WorkflowServiceHost的 AddServiceEndpoint措施添加。下面举例阐明:

1.下面是项目布局:

2.首先定义办事左券,如下:

[ServiceContract(Name = "IWorkflowCreation")]

public interface IWorkflowCreation

{

[OperationContract(Name = "Create")]

Guid Create(IDictionary inputs);

[OperationContract(Name = "CreateWithInstanceId", IsOneWay = true)]

void CreateWithInstanceId(Guid instanceId, IDictionary inputs);

[OperationContract(Name = "ResumeBookmark", IsOneWay = true)]

void ResumeBookmark(Guid instanceId, string bookmarkName, string message);

}

3.下面是ResumeBookmarkEndpoint的实现:

namespace CaryCommon

{

public class ResumeBookmarkEndpoint : WorkflowHostingEndpoint

{

public ResumeBookmarkEndpoint(Binding binding, EndpointAddress address)

: base(typeof(IWorkflowCreation), binding, address)

{

}

protected override Guid OnGetInstanceId(object[] inputs, OperationContext operationContext)

{

if (operationContext.IncomingMessageHeaders.Action.EndsWith("Create"))

{

return Guid.Empty;

}

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 arguments = (Dictionary)inputs[0];

if (arguments != null && arguments.Count > 0)

{

foreach (KeyValuePair pair in arguments)

{

creationContext.WorkflowArguments.Add(pair.Key, pair.Value);

}

}

responseContext.SendResponse(instanceId, null);

}

else if (operationContext.IncomingMessageHeaders.Action.EndsWith("CreateWithInstanceId"))

{

Dictionary arguments = (Dictionary)inputs[0];

if (arguments != null && arguments.Count > 0)

{

foreach (KeyValuePair pair in arguments)

{

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 = new Bookmark((string)inputs[1]);

value = (string)inputs[2];

}

else

{

throw new NotImplementedException(operationContext.IncomingMessageHeaders.Action);

}

return bookmark;

}

}

}

您可能还会对下面的文章感兴趣: