.net core工作流

前一篇介绍了如何使用office 365 Power Automate+Form实现公司审批流需求。然而实际和业务方讨论下来发现流程的参与者会有公司AD域以外的用户参与。那么Office 365就不适合此业务场景,因为Power Automate + Form其背后存储用的one drive,身份验证用的AAD,并且也没有组织架构。 所以综合考虑下来,只能采用代码实现workflow的解决方案。

操作步骤如下:
mkdir workflow-demo
cd workflow-demo
dotnet new console
dotnet add package WorkflowCore
dotnet add package Microsoft.Extensions.DependencyInjection
dotnet add package Microsoft.Extensions.DependencyInjection.Abstractions
dotnet add package Microsoft.Extensions.Logging
dotnet add package Microsoft.Extensions.Logging.Abstractions
dotnet add package Microsoft.Extensions.Logging.Console
dotnet add package Microsoft.Extensions.Logging.Debug

新建HelloWorld步骤

using System;
using System.Linq;
using WorkflowCore.Interface;
using WorkflowCore.Models;

namespace workflow_demo
{
    public class HelloWorld : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            Console.WriteLine("Hello world");
            return ExecutionResult.Next();
        }
    }
}

新建GoodByeWorld步骤

using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using WorkflowCore.Interface;
using WorkflowCore.Models;

namespace workflow_demo
{
    public class GoodbyeWorld : StepBody
    {

        private ILogger _logger;

        public GoodbyeWorld(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<GoodbyeWorld>();
        }

        public override ExecutionResult Run(IStepExecutionContext context)
        {
            Console.WriteLine("Goodbye world");
            _logger.LogInformation("Hi there!");
            return ExecutionResult.Next();
        }
    }
}

新建Workflow

using System;
using System.Linq;
using WorkflowCore.Interface;
using WorkflowCore.Models;

namespace workflow_demo
{
    public class HelloWorldWorkflow : IWorkflow
    {
        public void Build(IWorkflowBuilder<object> builder)
        {
            builder                
                .UseDefaultErrorBehavior(WorkflowErrorHandling.Suspend)
                .StartWith<HelloWorld>()                
                .Then<GoodbyeWorld>();
        }

        public string Id => "HelloWorld";
            
        public int Version => 1;
                 
    }
}

Program.cs

using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using WorkflowCore.Interface;

namespace workflow_demo
{
    class Program
    {
        public static void Main(string[] args)
        {
            IServiceProvider serviceProvider = ConfigureServices();

            //start the workflow host
            var host = serviceProvider.GetService<IWorkflowHost>();
            host.RegisterWorkflow<HelloWorldWorkflow>();        
            host.Start();            

            host.StartWorkflow("HelloWorld");
            
            Console.ReadLine();
            host.Stop();
        }
                
        private static IServiceProvider ConfigureServices()
        {
            //setup dependency injection
            IServiceCollection services = new ServiceCollection();
            services.AddLogging();
            services.AddWorkflow();
            //services.AddWorkflow(x => x.UseMongoDB(@"mongodb://localhost:27017", "workflow"));
            services.AddTransient<GoodbyeWorld>();
            
            var serviceProvider = services.BuildServiceProvider();

            return serviceProvider;
        }
    }
}

最后dotnet run,可以看到执行后果。

参考:
https://github.com/danielgerlag/workflow-core
https://workflow-core.readthedocs.io/en/latest/

posted @ 2022-02-26 22:40  老胡Andy  阅读(966)  评论(0编辑  收藏  举报