void CreateWorkflowManagementEndpoint(WorkflowServiceHost workflowServiceHost)
        {
            Binding controlEndpointBinding; 
            if (workflowServiceHost.InternalBaseAddresses.Contains(Uri.UriSchemeNetPipe))
            {
                controlEndpointBinding = NamedPipeControlEndpointBinding;
            }
            else if (workflowServiceHost.InternalBaseAddresses.Contains(Uri.UriSchemeHttp))
            {
                controlEndpointBinding = HttpControlEndpointBinding;
            }
            else
            {
                return;
            }

            Uri controlEndpointAddress = ServiceHost.GetVia(controlEndpointBinding.Scheme, new Uri(ControlEndpointAddress, UriKind.Relative), workflowServiceHost.InternalBaseAddresses);            
            XmlQualifiedName contractName = new XmlQualifiedName(XD2.WorkflowInstanceManagementService.ContractName, XD2.WorkflowServices.Namespace);
            //Create the Endpoint Dispatcher
            EndpointAddress address = new EndpointAddress(controlEndpointAddress.AbsoluteUri);
            EndpointDispatcher endpointDispatcher = new EndpointDispatcher(address,
                XD2.WorkflowInstanceManagementService.ContractName,
                XD2.WorkflowServices.Namespace, true)
            {
                ContractFilter = new ActionMessageFilter(
                    NamingHelper.GetMessageAction(contractName, XD2.WorkflowInstanceManagementService.Abandon, null, false),
                    NamingHelper.GetMessageAction(contractName, XD2.WorkflowInstanceManagementService.Cancel, null, false),
                    NamingHelper.GetMessageAction(contractName, XD2.WorkflowInstanceManagementService.Run, null, false),
                    NamingHelper.GetMessageAction(contractName, XD2.WorkflowInstanceManagementService.Suspend, null, false),
                    NamingHelper.GetMessageAction(contractName, XD2.WorkflowInstanceManagementService.Terminate, null, false),
                    NamingHelper.GetMessageAction(contractName, XD2.WorkflowInstanceManagementService.TransactedCancel, null, false),
                    NamingHelper.GetMessageAction(contractName, XD2.WorkflowInstanceManagementService.TransactedRun, null, false),
                    NamingHelper.GetMessageAction(contractName, XD2.WorkflowInstanceManagementService.TransactedSuspend, null, false),
                    NamingHelper.GetMessageAction(contractName, XD2.WorkflowInstanceManagementService.TransactedTerminate, null, false),
                    NamingHelper.GetMessageAction(contractName, XD2.WorkflowInstanceManagementService.TransactedUnsuspend, null, false),
                    NamingHelper.GetMessageAction(contractName, XD2.WorkflowInstanceManagementService.TransactedUpdate, null, false),
                    NamingHelper.GetMessageAction(contractName, XD2.WorkflowInstanceManagementService.Unsuspend, null, false),
                    NamingHelper.GetMessageAction(contractName, XD2.WorkflowInstanceManagementService.Update, null, false)),
            };

            //Create Listener
            ServiceEndpoint endpoint = new ServiceEndpoint(WorkflowControlEndpoint.WorkflowControlServiceContract, controlEndpointBinding, address);
            BindingParameterCollection parameters = workflowServiceHost.GetBindingParameters(endpoint);

            IChannelListener listener;
            if (controlEndpointBinding.CanBuildChannelListener<IDuplexSessionChannel>(controlEndpointAddress, parameters))
            {
                listener = controlEndpointBinding.BuildChannelListener<IDuplexSessionChannel>(controlEndpointAddress, parameters);
            }
            else if (controlEndpointBinding.CanBuildChannelListener<IReplySessionChannel>(controlEndpointAddress, parameters))
            {
                listener = controlEndpointBinding.BuildChannelListener<IReplySessionChannel>(controlEndpointAddress, parameters);
            }
            else
            {
                listener = controlEndpointBinding.BuildChannelListener<IReplyChannel>(controlEndpointAddress, parameters);
            }

            //Add the operations
            bool formatRequest;
            bool formatReply;
            foreach (OperationDescription operation in WorkflowControlEndpoint.WorkflowControlServiceContract.Operations)
            {
                DataContractSerializerOperationBehavior dataContractSerializerOperationBehavior = new DataContractSerializerOperationBehavior(operation);

                DispatchOperation operationDispatcher = new DispatchOperation(endpointDispatcher.DispatchRuntime, operation.Name,
                    NamingHelper.GetMessageAction(operation, false), NamingHelper.GetMessageAction(operation, true))
                {
                    Formatter = (IDispatchMessageFormatter)dataContractSerializerOperationBehavior.GetFormatter(operation, out formatRequest, out formatReply, false),
                    Invoker = new ControlOperationInvoker(
                        operation,
                        new WorkflowControlEndpoint(controlEndpointBinding, address),
                        null,
                        workflowServiceHost),
                };
                endpointDispatcher.DispatchRuntime.Operations.Add(operationDispatcher);

                OperationBehaviorAttribute operationAttribute = operation.Behaviors.Find<OperationBehaviorAttribute>();
                ((IOperationBehavior)operationAttribute).ApplyDispatchBehavior(operation, operationDispatcher);
            }

            DispatchRuntime dispatchRuntime = endpointDispatcher.DispatchRuntime;
            dispatchRuntime.ConcurrencyMode = ConcurrencyMode.Multiple;
            dispatchRuntime.InstanceContextProvider = new DurableInstanceContextProvider(workflowServiceHost);
            dispatchRuntime.InstanceProvider = new DurableInstanceProvider(workflowServiceHost);
            dispatchRuntime.ServiceAuthorizationManager = new WindowsAuthorizationManager(this.WindowsGroup);

            //Create the Channel Dispatcher
            ServiceDebugBehavior serviceDebugBehavior = workflowServiceHost.Description.Behaviors.Find<ServiceDebugBehavior>();
            ServiceBehaviorAttribute serviceBehaviorAttribute = workflowServiceHost.Description.Behaviors.Find<ServiceBehaviorAttribute>();

            bool includeDebugInfo = false;
            if (serviceDebugBehavior != null)
            {
                includeDebugInfo |= serviceDebugBehavior.IncludeExceptionDetailInFaults;
            }
            if (serviceBehaviorAttribute != null)
            {
                includeDebugInfo |= serviceBehaviorAttribute.IncludeExceptionDetailInFaults;
            }

            ChannelDispatcher channelDispatcher = new ChannelDispatcher(listener, controlEndpointBinding.Name, controlEndpointBinding)
            {
                MessageVersion = controlEndpointBinding.MessageVersion,
                Endpoints = { endpointDispatcher },
                ServiceThrottle = workflowServiceHost.ServiceThrottle
            };
            workflowServiceHost.ChannelDispatchers.Add(channelDispatcher);
        }
 private void CreateWorkflowManagementEndpoint(WorkflowServiceHost workflowServiceHost)
 {
     Binding namedPipeControlEndpointBinding;
     IChannelListener listener;
     if (workflowServiceHost.InternalBaseAddresses.Contains(Uri.UriSchemeNetPipe))
     {
         namedPipeControlEndpointBinding = NamedPipeControlEndpointBinding;
     }
     else if (workflowServiceHost.InternalBaseAddresses.Contains(Uri.UriSchemeHttp))
     {
         namedPipeControlEndpointBinding = HttpControlEndpointBinding;
     }
     else
     {
         return;
     }
     Uri listenUriBaseAddress = ServiceHostBase.GetVia(namedPipeControlEndpointBinding.Scheme, new Uri("System.ServiceModel.Activities_IWorkflowInstanceManagement", UriKind.Relative), workflowServiceHost.InternalBaseAddresses);
     XmlQualifiedName contractName = new XmlQualifiedName("IWorkflowInstanceManagement", "http://schemas.datacontract.org/2008/10/WorkflowServices");
     EndpointAddress address = new EndpointAddress(listenUriBaseAddress.AbsoluteUri);
     EndpointDispatcher item = new EndpointDispatcher(address, "IWorkflowInstanceManagement", "http://schemas.datacontract.org/2008/10/WorkflowServices", true) {
         ContractFilter = new ActionMessageFilter(new string[] { NamingHelper.GetMessageAction(contractName, "Abandon", null, false), NamingHelper.GetMessageAction(contractName, "Cancel", null, false), NamingHelper.GetMessageAction(contractName, "Run", null, false), NamingHelper.GetMessageAction(contractName, "Suspend", null, false), NamingHelper.GetMessageAction(contractName, "Terminate", null, false), NamingHelper.GetMessageAction(contractName, "TransactedCancel", null, false), NamingHelper.GetMessageAction(contractName, "TransactedRun", null, false), NamingHelper.GetMessageAction(contractName, "TransactedSuspend", null, false), NamingHelper.GetMessageAction(contractName, "TransactedTerminate", null, false), NamingHelper.GetMessageAction(contractName, "TransactedUnsuspend", null, false), NamingHelper.GetMessageAction(contractName, "Unsuspend", null, false) })
     };
     BindingParameterCollection parameters = new BindingParameterCollection();
     VirtualPathExtension extension = workflowServiceHost.Extensions.Find<VirtualPathExtension>();
     if (extension != null)
     {
         parameters.Add(extension);
     }
     ChannelProtectionRequirements requirements = new ChannelProtectionRequirements();
     requirements.Add(ChannelProtectionRequirements.CreateFromContract(WorkflowControlEndpoint.WorkflowControlServiceContract, ProtectionLevel.EncryptAndSign, ProtectionLevel.EncryptAndSign, false));
     parameters.Add(requirements);
     if (namedPipeControlEndpointBinding.CanBuildChannelListener<IDuplexSessionChannel>(new object[] { listenUriBaseAddress, parameters }))
     {
         listener = namedPipeControlEndpointBinding.BuildChannelListener<IDuplexSessionChannel>(listenUriBaseAddress, parameters);
     }
     else if (namedPipeControlEndpointBinding.CanBuildChannelListener<IReplySessionChannel>(new object[] { listenUriBaseAddress, parameters }))
     {
         listener = namedPipeControlEndpointBinding.BuildChannelListener<IReplySessionChannel>(listenUriBaseAddress, parameters);
     }
     else
     {
         listener = namedPipeControlEndpointBinding.BuildChannelListener<IReplyChannel>(listenUriBaseAddress, parameters);
     }
     foreach (OperationDescription description in WorkflowControlEndpoint.WorkflowControlServiceContract.Operations)
     {
         bool flag;
         bool flag2;
         DataContractSerializerOperationBehavior behavior = new DataContractSerializerOperationBehavior(description);
         DispatchOperation operation = new DispatchOperation(item.DispatchRuntime, description.Name, NamingHelper.GetMessageAction(description, false), NamingHelper.GetMessageAction(description, true)) {
             Formatter = (IDispatchMessageFormatter) behavior.GetFormatter(description, out flag, out flag2, false),
             Invoker = new ControlOperationInvoker(description, new WorkflowControlEndpoint(namedPipeControlEndpointBinding, address), null, workflowServiceHost)
         };
         item.DispatchRuntime.Operations.Add(operation);
         OperationBehaviorAttribute attribute = description.Behaviors.Find<OperationBehaviorAttribute>();
         ((IOperationBehavior) attribute).ApplyDispatchBehavior(description, operation);
         if (attribute.TransactionScopeRequired)
         {
             ((ITransactionChannelManager) listener).Dictionary.Add(new DirectionalAction(MessageDirection.Input, NamingHelper.GetMessageAction(description, false)), TransactionFlowOption.Allowed);
         }
     }
     DispatchRuntime dispatchRuntime = item.DispatchRuntime;
     dispatchRuntime.ConcurrencyMode = ConcurrencyMode.Multiple;
     dispatchRuntime.InstanceContextProvider = new DurableInstanceContextProvider(workflowServiceHost);
     dispatchRuntime.InstanceProvider = new DurableInstanceProvider(workflowServiceHost);
     dispatchRuntime.ServiceAuthorizationManager = new WindowsAuthorizationManager(this.WindowsGroup);
     ServiceDebugBehavior behavior2 = workflowServiceHost.Description.Behaviors.Find<ServiceDebugBehavior>();
     ServiceBehaviorAttribute attribute2 = workflowServiceHost.Description.Behaviors.Find<ServiceBehaviorAttribute>();
     bool flag3 = false;
     if (behavior2 != null)
     {
         flag3 |= behavior2.IncludeExceptionDetailInFaults;
     }
     if (attribute2 != null)
     {
         flag3 |= attribute2.IncludeExceptionDetailInFaults;
     }
     ChannelDispatcher dispatcher4 = new ChannelDispatcher(listener, namedPipeControlEndpointBinding.Name, namedPipeControlEndpointBinding) {
         MessageVersion = namedPipeControlEndpointBinding.MessageVersion
     };
     dispatcher4.Endpoints.Add(item);
     dispatcher4.ServiceThrottle = workflowServiceHost.ServiceThrottle;
     ChannelDispatcher dispatcher2 = dispatcher4;
     workflowServiceHost.ChannelDispatchers.Add(dispatcher2);
 }