7.持久化和在Asp.net core中的使用

流程持久化

  • sqlite
  1. nuget安装WorkflowCore.Persistence.Sqlite
  2. services.AddWorkflow(x => x.UseSqlite(@"Data Source=database.db;", true));
  • sqlServer
  1. nuget安装WorkflowCore.Persistence.SqlServer
  2. services.AddWorkflow(x => x.UseSqlServer(@"Server=.;Database=WorkflowCore;Trusted_Connection=True;", true, true));

在Asp.net core中使用

  • workflow
public class MyDataClass
{
    public string Value1 { get; set; }
}
public class TestWorkflow : IWorkflow<MyDataClass>
{
    public string Id => "TestWorkflow";

    public int Version => 1;

    public void Build(IWorkflowBuilder<MyDataClass> builder)
    {
        builder
            .StartWith(context => ExecutionResult.Next())
            .WaitFor("MyEvent", (data, context) => context.Workflow.Id, data => DateTime.Now)
                .Output(data => data.Value1, step => step.EventData)
            .Then(context => Console.WriteLine("workflow complete"));
    }
}
  • program
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
//设置持久化和Elastic搜索
builder.Services.AddWorkflow(config => { 
    config.UseSqlite("Data Source=MySqLite.db",true);
    config.UseElasticsearch(new ConnectionSettings(new Uri("http://elastic:9200")), "workflows");
});

var app = builder.Build();
IWorkflowHost? host= app.Services.GetService<IWorkflowHost>();
host!.RegisterWorkflow<TestWorkflow, MyDataClass>();
host.Start();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseAuthorization();
app.MapControllers();
app.Run();
  • controllers
[Route("api/[controller]")]
[Controller]
public class WorkflowsController : Controller
{
    private readonly IWorkflowController _workflowService; //workflow服务和host相似
    private readonly IWorkflowRegistry _registry;//获取注册过的workflow
    private readonly IPersistenceProvider _workflowStore;//持久化相关,从数据库中获取
    private readonly ISearchIndex _searchService;//查询相关

    public WorkflowsController(IWorkflowController workflowService, ISearchIndex searchService, IWorkflowRegistry registry, IPersistenceProvider workflowStore)
    {
        _workflowService = workflowService;
        _workflowStore = workflowStore;
        _registry = registry;
        _searchService = searchService;
    }

    [HttpGet]
    public async Task<IActionResult> Get(string terms, WorkflowStatus? status, string type, DateTime? createdFrom, DateTime? createdTo, int skip, int take = 10)
    {
        var filters = new List<SearchFilter>();

        if (status.HasValue)
            filters.Add(StatusFilter.Equals(status.Value));

        if (createdFrom.HasValue)
            filters.Add(DateRangeFilter.After(x => x.CreateTime, createdFrom.Value));

        if (createdTo.HasValue)
            filters.Add(DateRangeFilter.Before(x => x.CreateTime, createdTo.Value));

        if (!string.IsNullOrEmpty(type))
            filters.Add(ScalarFilter.Equals(x => x.WorkflowDefinitionId, type));

        var result = await _searchService.Search(terms, skip, take, filters.ToArray());

        return Json(result);
    }

    [HttpGet("{id}")]
    public async Task<IActionResult> Get(string id)
    {
        var result = await _workflowStore.GetWorkflowInstance(id);
        return Json(result);
    }

    [HttpPost("{id}")]
    public async Task<IActionResult> Post1(string id, int? version)
    {
        string workflowId = null;
        var def = _registry.GetDefinition(id, version);
        if (def == null)
            return BadRequest(String.Format("Workflow defintion {0} for version {1} not found", id, version));

        
            workflowId = await _workflowService.StartWorkflow(id, version);
        

        return Ok(workflowId);
    }

    [HttpPut("{id}/suspend")]
    public Task<bool> Suspend(string id)
    {
        return _workflowService.SuspendWorkflow(id);
    }

    [HttpPut("{id}/resume")]
    public Task<bool> Resume(string id)
    {
        return _workflowService.ResumeWorkflow(id);
    }

    [HttpDelete("{id}")]
    public Task<bool> Terminate(string id)
    {
        return _workflowService.TerminateWorkflow(id);
    }
}
[Route("api/[controller]")]
[Controller]
public class EventsController : Controller
{
    private readonly IWorkflowController _workflowService;

    public EventsController(IWorkflowController workflowService)
    {
        _workflowService = workflowService;
    }

    [HttpPost("{eventName}/{eventKey}")]
    public async Task<IActionResult> Post(string eventName, string eventKey, [FromBody] MyDataClass eventData)
    {
        await _workflowService.PublishEvent(eventName, eventKey, eventData.Value1);
        return Ok();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

步、步、为营

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值