|
| 1 | + |
| 2 | +# Using with ASP.NET Core |
| 3 | + |
| 4 | +This sample will use `docker-compose` to fire up instances of MongoDB and Elasticsearch to which the sample application will connect. |
| 5 | + |
| 6 | +## How to configure within an ASP.NET Core application |
| 7 | + |
| 8 | +In your startup class, use the `AddWorkflow` extension method to configure workflow core services, and then register your workflows and start the host when you configure the app. |
| 9 | +```c# |
| 10 | +public class Startup |
| 11 | +{ |
| 12 | + public Startup(IConfiguration configuration) |
| 13 | + { |
| 14 | + Configuration = configuration; |
| 15 | + } |
| 16 | + |
| 17 | + public IConfiguration Configuration { get; } |
| 18 | + |
| 19 | + public void ConfigureServices(IServiceCollection services) |
| 20 | + { |
| 21 | + services.AddMvc(); |
| 22 | + services.AddWorkflow(cfg => |
| 23 | + { |
| 24 | + cfg.UseMongoDB(@"mongodb://mongo:27017", "workflow"); |
| 25 | + cfg.UseElasticsearch(new ConnectionSettings(new Uri("http://elastic:9200")), "workflows"); |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + public void Configure(IApplicationBuilder app, IHostingEnvironment env) |
| 30 | + { |
| 31 | + if (env.IsDevelopment()) |
| 32 | + { |
| 33 | + app.UseDeveloperExceptionPage(); |
| 34 | + } |
| 35 | + |
| 36 | + app.UseMvc(); |
| 37 | + |
| 38 | + var host = app.ApplicationServices.GetService<IWorkflowHost>(); |
| 39 | + host.RegisterWorkflow<TestWorkflow, MyDataClass>(); |
| 40 | + host.Start(); |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +## Usage |
| 46 | + |
| 47 | +Now simply inject the services you require into your controllers |
| 48 | +* IWorkflowController |
| 49 | +* IWorkflowHost |
| 50 | +* ISearchIndex |
| 51 | +* IPersistenceProvider |
| 52 | + |
| 53 | +```c# |
| 54 | +public class WorkflowsController : Controller |
| 55 | +{ |
| 56 | + private readonly IWorkflowController _workflowService; |
| 57 | + private readonly IWorkflowRegistry _registry; |
| 58 | + private readonly IPersistenceProvider _workflowStore; |
| 59 | + private readonly ISearchIndex _searchService; |
| 60 | + |
| 61 | + public WorkflowsController(IWorkflowController workflowService, ISearchIndex searchService, IWorkflowRegistry registry, IPersistenceProvider workflowStore) |
| 62 | + { |
| 63 | + _workflowService = workflowService; |
| 64 | + _workflowStore = workflowStore; |
| 65 | + _registry = registry; |
| 66 | + _searchService = searchService; |
| 67 | + } |
| 68 | + |
| 69 | + public Task<bool> Suspend(string id) |
| 70 | + { |
| 71 | + return _workflowService.SuspendWorkflow(id); |
| 72 | + } |
| 73 | + |
| 74 | + ... |
| 75 | +} |
| 76 | +``` |
0 commit comments