1

We are using Windows Workflow Foundation for our workflows. The XAMLX files that contain workflow definitions are very brittle (they often end-up unopenable for no apparent reason) and we can't work on them in parallel, because we won't be able to merge those changes (underlying XAML is nie-human-readable). I'm looking for ways to alleviate the pains of having to work with XAML via built-in Visual Studio editor. Is it possible to define Windows Workflow Foundation workflows in C#?

This article article in the "Workflow Structure" section suggests that it is possible, but does not give full example.

Final Update Check the answer below for a working sample of code-only C# workflow and config to host it via WCF.

Ivan Koshelev
  • 3,830
  • 2
  • 30
  • 50
  • Um - I've just realised that the example I posted is almost identical to the one in the article you linked. I promise I wrote it before reading the article! However, I'm wondering if I've misunderstood your question? – RB. May 16 '18 at 20:11

2 Answers2

4

Absolutely - just compose your activities exactly as you would in Xaml. For example, the following will create a Sequence containing two WriteLine activities.

// Create a sequence containing 2 WriteLine activities.
var sequence = new Sequence();

sequence.Activities.Add(new WriteLine()
{
    Text = "Hello!"
});

sequence.Activities.Add(new WriteLine()
{
    Text = "Goodbye!"
});

// Now execute the sequence.
var wfApplication = new WorkflowApplication(sequence);
wfApplication.Run();

If you want to create custom activities, you need to inherit from CodeActivity or NativeActivity - but there's lots of examples of this on the web :)

RB.
  • 36,301
  • 12
  • 91
  • 131
  • Your answer has a crucial workflow start detail, that the article lacked, it explains things much better. One final piece of the puzzle - how would I host such workflow with WCF? I added our current hosting setup description to the original question. I'm guessing, I should create my workflow as a class inheriting something specific and use its full name (with namespaces) in configuration instead of XAML file? – Ivan Koshelev May 16 '18 at 20:46
  • I think I've figured out the correct type to inherit in my workflow - System.ServiceModel.Activities.WorkflowService https://msdn.microsoft.com/en-us/library/system.servicemodel.activities.workflowservice(v=vs.110).aspx . Now I'm trying to figure out, how to host it. – Ivan Koshelev May 17 '18 at 18:27
0

I have compiled together information from different tutorials and came up with a sample project demonstrating Windows Workflow Foundation workflow authored via code-only and hosted as WCF service

Ivan Koshelev
  • 3,830
  • 2
  • 30
  • 50