|
using System;
using System.Xml;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Reflection;
using System.Text;
namespace Contract
{
public class CreateWorkflowMessageInspector : IDispatchMessageInspector
{
DispatchOperation _dispatch = null;
XmlQualifiedName _durableDispatchContextName = null;
public CreateWorkflowMessageInspector(DispatchOperation dispatch)
{
_dispatch = dispatch;
_durableDispatchContextName = new XmlQualifiedName("InstanceId", "http://schemas.microsoft.com/ws/2006/05/context");
}
#region IDispatchMessageInspector Members
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
string strInstanceId = null;
ContextMessageProperty cmp = null;
if (_dispatch.Action == request.Headers.Action &&
ContextMessageProperty.TryGet(request, out cmp) &&
cmp.TryGetValue(_durableDispatchContextName, out strInstanceId) &&
instanceContext.Extensions.Count == 1)
{
PropertyInfo pi = _dispatch.Invoker.GetType().GetProperty("CanCreateInstance");
if (pi != null)
{
// get property from WorkflowOperationInvoker
bool canCreateInstance = (bool)pi.GetValue(_dispatch.Invoker, null);
if (canCreateInstance)
{
// update WorkflowDurableInstance field
IExtension<InstanceContext>[] arr = new IExtension<InstanceContext>[1];
instanceContext.Extensions.CopyTo(arr, 0);
FieldInfo fi = arr[0].GetType().GetField("shouldCreateNew", BindingFlags.Instance | BindingFlags.NonPublic);
if (fi != null)
{
fi.SetValue(arr[0], true);
request.Properties["newDurableInstanceIdProperty"] = new Guid(strInstanceId);
return strInstanceId;
}
}
}
}
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState) {}
#endregion
}
[AttributeUsage(AttributeTargets.Method)]
public class CreateWorkflowAttribute : Attribute, IOperationBehavior
{
public void AddBindingParameters(OperationDescription description, BindingParameterCollection parameters){}
public void ApplyClientBehavior(OperationDescription description, ClientOperation proxy) {}
public void Validate(OperationDescription description) { }
public void ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch)
{
if (description.Behaviors.Contains(typeof(CreateWorkflowAttribute)))
{
dispatch.Parent.MessageInspectors.Add(new CreateWorkflowMessageInspector(dispatch));
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.