public static WorkflowServiceHost CreateWorkflowServiceHost() { // add the workflow implementation and application endpoint to the host WorkflowService service = new WorkflowService() { Body = PurchaseOrderWorkflow.CreateBody(), Endpoints = { // adds an application endpoint new System.ServiceModel.Endpoint { Binding = new System.ServiceModel.NetMsmqBinding("NetMsmqBindingTx"), AddressUri = new Uri("net.msmq://localhost/private/ReceiveTx"), ServiceContractName = XName.Get(PurchaseOrderWorkflow.poContractDescription.Name) } } }; WorkflowServiceHost workflowServiceHost = new WorkflowServiceHost(service); // add the workflow management behaviors IServiceBehavior idleBehavior = new WorkflowIdleBehavior { TimeToUnload = TimeSpan.Zero }; workflowServiceHost.Description.Behaviors.Add(idleBehavior); IServiceBehavior workflowUnhandledExceptionBehavior = new WorkflowUnhandledExceptionBehavior() { Action = WorkflowUnhandledExceptionAction.AbandonAndSuspend // this is also the default }; workflowServiceHost.Description.Behaviors.Add(workflowUnhandledExceptionBehavior); // add the instance store SqlWorkflowInstanceStoreBehavior instanceStoreBehavior = new SqlWorkflowInstanceStoreBehavior() { ConnectionString = "Server=localhost\\SQLEXPRESS;Integrated Security=true;Initial Catalog=DefaultSampleStore;" }; workflowServiceHost.Description.Behaviors.Add(instanceStoreBehavior); // add a workflow management endpoint ServiceEndpoint workflowControlEndpoint = new WorkflowControlEndpoint() { Binding = new System.ServiceModel.NetNamedPipeBinding(System.ServiceModel.NetNamedPipeSecurityMode.None), Address = new System.ServiceModel.EndpointAddress("net.pipe://workflowInstanceControl") }; workflowServiceHost.AddServiceEndpoint(workflowControlEndpoint); // add the tracking participant workflowServiceHost.WorkflowExtensions.Add(new TrackingListenerConsole()); foreach (ServiceEndpoint ep in workflowServiceHost.Description.Endpoints) { Console.WriteLine(ep.Address); } return workflowServiceHost; }
/// <summary> /// Adds the standard behaviors and endpoints to our workflow service host. /// </summary> /// <param name="workflowServiceHost">The workflow service host.</param> private void AddBehaviorsAndEndpoints(WorkflowServiceHost workflowServiceHost) { // Check whether we have already initialised the service host if (workflowServiceHost.Description.Endpoints.Where(endpoint => endpoint is EnterpriseWorkflowCreationEndpoint).Any()) { return; } // Add endpoints for any services that have been defined in the workflow workflowServiceHost.AddDefaultEndpoints(); ServiceEndpoint firstEndpoint = (from endpoint in workflowServiceHost.Description.Endpoints where endpoint.IsSystemEndpoint == false select endpoint).FirstOrDefault(); BasicHttpBinding binding = new BasicHttpBinding(); EndpointAddress endpointAddress = new EndpointAddress(workflowServiceHost.BaseAddresses[0]); // Add the creation endpoint EnterpriseWorkflowCreationEndpoint creationEndpoint = new EnterpriseWorkflowCreationEndpoint(firstEndpoint != null ? firstEndpoint.Binding : binding, firstEndpoint != null ? firstEndpoint.Address : endpointAddress); workflowServiceHost.AddServiceEndpoint(creationEndpoint); // Add the SQL workflow instance store //SqlWorkflowInstanceStore store = new SqlWorkflowInstanceStore("myConnectionString"); //workflowServiceHost.DurableInstancingOptions.InstanceStore = store; // Add the idle behavior workflowServiceHost.Description.Behaviors.RemoveAll<WorkflowIdleBehavior>(); WorkflowIdleBehavior idleBehavior = new WorkflowIdleBehavior { TimeToPersist = TimeSpan.FromMilliseconds(10000), TimeToUnload = TimeSpan.FromMilliseconds(10000) }; workflowServiceHost.Description.Behaviors.Add(idleBehavior); // Add the unhandled exception behavior WorkflowUnhandledExceptionBehavior unhandledExceptionBehavior = new WorkflowUnhandledExceptionBehavior { Action = WorkflowUnhandledExceptionAction.AbandonAndSuspend }; workflowServiceHost.Description.Behaviors.Add(unhandledExceptionBehavior); // Add tracking behavior EnterpriseWorkflowTrackingBehavior trackingBehavior = new EnterpriseWorkflowTrackingBehavior(); workflowServiceHost.Description.Behaviors.Add(trackingBehavior); // Add a custom extension workflowServiceHost.WorkflowExtensions.Add(() => new WorkflowHostingEnvironmentExtension()); }