Recently want to be a OA-related web site development, workflow has been heard something, before also studied intermittently Workflow Foundation 4.0, still do not understand what in the end be able to do with it
But still feel workflow should be able to apply under certain circumstances, although there is no answer, the online search floor a pass and found a workflow-core stuff, I feel quite interesting, then stopped, wondering what, now share with you.
* Introduction
As introduced on the home page, workflow core as a lightweight workflow engine that can be embedded into the project, with its underlying .net standard 2.0 developers can use to track the status of long-running tasks, functions are relatively strong, supported plug-ins persistent form, and multi-node parallel processing, it looks like a cow. And currently has to a Conductor project is to use workflow core as the core workflow server (running the original workflow, requires a separate server ah), Conductor where not carried out. workflow core support fluent grammar, writing is also very beautiful, although not as WF has a graphical user interface, but it feels the code is relatively clean.
- spots .Net Standard 2.0 Introduction
At first I do not understand what is .Net Standard 2.0, the article put it more clearly, .Net and the .Net Framework Standard relations as well as the .NET Core 2.0 is your best choice for you , in order to unify the original Microsoft's .Net a variety of platforms, out of a .Net standard standard library, based on the development of this library can be applied to .net framework 4.6.1 above, it can also be applied .net core 2.0 above
After the understanding of the relevant content, to open direct explanation, born a shining example.
* Example 1
Create a new project, that using .net framework 4.6.1 above, the new project, the Package Manager Console, the installation Core Workflow: Install-Package WorkflowCore
install this package installed by default a series of dependencies
May be due to the relationship version, you also need to install two packages: Microsoft.Extensions.Logging and Microsoft.Extensions.Logging.Debug. So that you can start coding in accordance with Sample 01
Sample01 is a helloworld, contains several sections:
1. Build StepBody, is content to be executed workflow, each class inherits from StepBody virtual classes, overloaded ExecutionResult the Run (IStepExecutionContext context) , this function to complete the required work
1 public class HelloWorld : StepBody 2 { 3 private ILogger logger; 4 5 public HelloWorld(ILoggerFactory loggerFactory) 6 { 7 logger = loggerFactory.CreateLogger<HelloWorld>(); 8 } 9 10 public override ExecutionResult Run(IStepExecutionContext context) 11 { 12 Console.WriteLine("Hello world, workflow"); 13 logger.LogInformation("Helloworld workflow"); 14 15 return ExecutionResult.Next(); 16 } 17 } 18 19 20 public class GoodbyeWorld : StepBody 21 { 22 private ILogger logger; 23 24 public GoodbyeWorld(ILoggerFactory loggerFactory) 25 { 26 logger = loggerFactory.CreateLogger<GoodbyeWorld>(); 27 } 28 29 public override ExecutionResult Run(IStepExecutionContext context) 30 { 31 Console.WriteLine("Workflow, Goodbye"); 32 logger.LogInformation("Goodbye workflow"); 33 34 return ExecutionResult.Next(); 35 } 36 } 37 38 public class SleepStep : StepBody 39 { 40 private ILogger logger; 41 42 public SleepStep(ILoggerFactory loggerFactory) 43 { 44 logger = loggerFactory.CreateLogger("SleepStep"); 45 } 46 47 public override ExecutionResult Run(IStepExecutionContext context) 48 { 49 Thread.Sleep(1000); 50 51 logger.LogInformation("Sleeped"); 52 53 return ExecutionResult.Next(); 54 } 55 }
2. Construction of workflow, achieve IWorkflow interfaces, each have a workflow Id and a Version, indicating the identity of the workflow, here are two ways to build a HelloWorkflow,
1 public class HelloWorkflow : IWorkflow 2 { 3 public string Id => "HelloWorkflow"; 4 5 public int Version => 1; 6 7 public void Build(IWorkflowBuilder<object> builder) 8 { 9 builder.StartWith(context => 10 { 11 Console.WriteLine("Hello world"); 12 return ExecutionResult.Next(); 13 }) 14 .Then(context => 15 { 16 Thread.Sleep(500); 17 Console.WriteLine("sleeped"); 18 return ExecutionResult.Next(); 19 }) 20 .Then(context => 21 { 22 Console.WriteLine("Goodbye world"); 23 return ExecutionResult.Next(); 24 }); 25 } 26 } 27 28 public class HelloWorkflow2 : IWorkflow 29 { 30 public string Id => "HelloWorkflow"; 31 32 public int Version => 2; 33 34 public void Build(IWorkflowBuilder<object> builder) 35 { 36 builder.StartWith<HelloWorld>() 37 .Then<SleepStep>() 38 .Then<GoodbyeWorld>(); 39 } 40 }
3. Everything is ready, ready to make workflow up and running. The first step of course, is the need to build a service, Workflow Core added Workflow related services through ServiceCollection Injection namespace for StepBody have parameters, you need to register by AddTransient service function, so as to correctly construct objects
1 /// <summary> 2 /// 配置workflow 3 /// </summary> 4 /// <returns></returns> 5 private IServiceProvider ConfigureServices() 6 { 7 //setup dependency injection 8 IServiceCollection services = new ServiceCollection(); 9 services.AddLogging(); 10 services.AddWorkflow(); 11 //services.AddWorkflow(x => x.UseMongoDB(@"mongodb://localhost:27017", "workflow")); 12 13 // These constructor parameters needs to be added to the transient 14 services.AddTransient <the HelloWorld> (); 15 services.AddTransient <GoodByeWorld> (); 16 services.AddTransient <SleepStep> (); . 17 18 is var the serviceProvider = services.BuildServiceProvider (); . 19 20 is // config the logging 21 is var LoggerFactory = serviceProvider.GetService <ILoggerFactory> (); 22 is loggerFactory.AddProvider ( new new DebugLoggerProvider ()); 23 is 24 return the serviceProvider; 25 }
Next, start workflow host and initiate a workflow, the entire form code stickers, this is more clear
1 using Microsoft.Extensions.DependencyInjection; 2 using Microsoft.Extensions.Logging; 3 using Microsoft.Extensions.Logging.Console; 4 using Microsoft.Extensions.Logging.Debug; 5 using System; 6 using System.Collections.Generic; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 using System.Windows; 11 using System.Windows.Controls; 12 using System.Windows.Data; 13 using System.Windows.Documents; 14 using System.Windows.Input; 15 using System.Windows.Media; 16 using System.Windows.Media.Imaging; 17 using System.Windows.Navigation; 18 using System.Windows.Shapes; 19 using WorkflowCore.Interface; 20 using WorkflowCore.Services; 21 using WorkFlowCoreTest.MyWorkflow; 22 23 namespace WorkFlowCoreTest 24 { 25 /// <summary> 26 /// Interaction logic for MainWindow.xaml 27 /// </summary> 28 public partial class MainWindow : Window 29 { 30 IServiceProvider serviceProvider = null; 31 bool serviceStarted = false; 32 33 public MainWindow() 34 { 35 InitializeComponent(); 36 } 37 38 private void StartWorkflow() 39 { 40 41 if (serviceProvider == null) 42 { 43 serviceProvider = ConfigureServices(); 44 var host1 = serviceProvider.GetService<IWorkflowHost>(); 45 46 host1.RegisterWorkflow<HelloWorkflow>(); 47 host1.RegisterWorkflow<HelloWorkflow2>(); 48 } 49 50 51 var host = serviceProvider.GetService<IWorkflowHost>(); 52 varhost.Registry.GetDefinition = WD ( " HelloWorkflow " ); 53 is 54 is // If host started, can not be started again, but there is no method of determining 55 IF (! serviceStarted) 56 is { 57 is host.Start (); 58 serviceStarted = to true ; 59 } 60 61 is 62 is // start workflow workflow 63 is host.StartWorkflow ( " HelloWorkflow " , . 1 , Data: null ); // 64 //host.StartWorkflow ( "HelloWorkflow"); // , 2, Data: null, is enabled by default high version 65 } 66 67 Private void StopWorkflow () 68 { 69 var Host serviceProvider.GetService = <IWorkflowHost> (); 70 71 is host.Stop (); 72 serviceStarted = to false ; 73 is } 74 75 /// <Summary> 76 /// configuration Workflow 77 /// </ Summary> 78 /// <Returns> </ Returns> 79 Private the IServiceProvider ConfigureServices () 80 { 81 // Setup dependency Injection 82 IServiceCollection Services = new new ServiceCollection (); 83 services.AddLogging (); 84 services.AddWorkflow (); 85 // services.AddWorkflow (X => x.UseMongoDB ( @ "MongoDB: // localhost: 27017", "Workflow")); 86 87 // these constructor parameters needs to be added to the transient 88 services.AddTransient <the HelloWorld> (); 89 services.AddTransient <GoodByeWorld > (); 90 services.AddTransient<SleepStep>(); 91 92 var serviceProvider = services.BuildServiceProvider(); 93 94 //config logging 95 var loggerFactory = serviceProvider.GetService<ILoggerFactory>(); 96 loggerFactory.AddProvider(new DebugLoggerProvider()); 97 98 return serviceProvider; 99 } 100 101 private void startButton_Click(object sender, RoutedEventArgs e) 102 { 103 StartWorkflow (); 104 } 105 106 private void stopButton_Click ( object sender, RoutedEventArgs e) 107 { 108 StopWorkflow (); 109 } 110 } 111 }
So a simple example of Core Workflow is complete, in general, is very simple and clear.