/// <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()); }
// start the service static void Main(string[] args) { string persistenceConnectionString = ConfigurationManager.ConnectionStrings["WorkflowPersistence"].ConnectionString; string baseAddr = "http://localhost:8080/Contoso/HiringRequestService"; using (WorkflowServiceHost host = new WorkflowServiceHost(new HiringRequestProcessServiceDefinition(), new Uri(baseAddr))) { SqlWorkflowInstanceStoreBehavior instanceStoreBehavior = new SqlWorkflowInstanceStoreBehavior(persistenceConnectionString); instanceStoreBehavior.InstanceCompletionAction = InstanceCompletionAction.DeleteAll; instanceStoreBehavior.InstanceEncodingOption = InstanceEncodingOption.GZip; host.Description.Behaviors.Add(instanceStoreBehavior); host.Description.Behaviors.Add(new WorkflowIdleBehavior() { TimeToPersist = new TimeSpan(0) }); host.WorkflowExtensions.Add(new HiringRequestInfoPersistenceParticipant()); // configure the unknown message handler host.UnknownMessageReceived += new EventHandler<System.ServiceModel.UnknownMessageReceivedEventArgs>(Program.UnknownMessageReceive); // add the control endpoint WorkflowControlEndpoint publicEndpoint = new WorkflowControlEndpoint( new BasicHttpBinding(), new EndpointAddress(new Uri("http://127.0.0.1/hiringProcess"))); host.AddServiceEndpoint(publicEndpoint); host.AddDefaultEndpoints(); // start the service Console.WriteLine("Starting ..."); host.Open(); // end when the user hits enter Console.WriteLine("Service is waiting at: " + baseAddr); Console.WriteLine("Press [Enter] to exit"); Console.ReadLine(); host.Close(); } }