diff --git a/src/samples/WebApiSample/README.md b/src/samples/WebApiSample/README.md new file mode 100644 index 000000000..cbe406b03 --- /dev/null +++ b/src/samples/WebApiSample/README.md @@ -0,0 +1,76 @@ + +# Using with ASP.NET Core + +This sample will use `docker-compose` to fire up instances of MongoDB and Elasticsearch to which the sample application will connect. + +## How to configure within an ASP.NET Core application + +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. +```c# +public class Startup +{ + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + public void ConfigureServices(IServiceCollection services) + { + services.AddMvc(); + services.AddWorkflow(cfg => + { + cfg.UseMongoDB(@"mongodb://mongo:27017", "workflow"); + cfg.UseElasticsearch(new ConnectionSettings(new Uri("http://elastic:9200")), "workflows"); + }); + } + + public void Configure(IApplicationBuilder app, IHostingEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseMvc(); + + var host = app.ApplicationServices.GetService(); + host.RegisterWorkflow(); + host.Start(); + } +} +``` + +## Usage + +Now simply inject the services you require into your controllers +* IWorkflowController +* IWorkflowHost +* ISearchIndex +* IPersistenceProvider + +```c# +public class WorkflowsController : Controller +{ + private readonly IWorkflowController _workflowService; + private readonly IWorkflowRegistry _registry; + 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; + } + + public Task Suspend(string id) + { + return _workflowService.SuspendWorkflow(id); + } + + ... +} +``` \ No newline at end of file diff --git a/src/samples/WebApiSample/WebApiSample.sln b/src/samples/WebApiSample/WebApiSample.sln index c37bd7b35..f2e46941d 100644 --- a/src/samples/WebApiSample/WebApiSample.sln +++ b/src/samples/WebApiSample/WebApiSample.sln @@ -3,10 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.28307.168 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiSample", "WebApiSample\WebApiSample.csproj", "{14489389-A65D-4993-8DE2-F51701A5AF60}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApiSample", "WebApiSample\WebApiSample.csproj", "{14489389-A65D-4993-8DE2-F51701A5AF60}" EndProject Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{2D5D708D-7EA1-48A9-ABF0-64CCC6026435}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{6545FD2F-EBA5-4C65-8257-2C1E34AD2FAA}" + ProjectSection(SolutionItems) = preProject + README.md = README.md + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/src/samples/WebApiSample/WebApiSample/Controllers/WorkflowsController.cs b/src/samples/WebApiSample/WebApiSample/Controllers/WorkflowsController.cs index 694d9b05b..b61d50d76 100644 --- a/src/samples/WebApiSample/WebApiSample/Controllers/WorkflowsController.cs +++ b/src/samples/WebApiSample/WebApiSample/Controllers/WorkflowsController.cs @@ -28,8 +28,7 @@ public WorkflowsController(IWorkflowController workflowService, ISearchIndex sea _registry = registry; _searchService = searchService; } - - + [HttpGet] public async Task Get(string terms, WorkflowStatus? status, string type, DateTime? createdFrom, DateTime? createdTo, int skip, int take = 10) {