Partager via


Building C# expressions support and IntelliSense in the rehosted workflow designer

The Workflow Foundation Designer can be rehosted in environments outside of Visual Studio for the purposes of creating, modifying, and monitoring workflows. Hosting the workflow designer inside a Windows Presentation Foundation (WPF) application is a common rehosting scenario for the workflow designer.

Prior to .NET Framework 4.5, all expressions in workflows could only be written in Visual Basic in both Visual Studio and Rehosted scenarios. Visual Studio 2012 introduced a fully functional C# expression editor with capabilities such as Syntax highlighting and IntelliSense that could be used for authoring workflow. Visual Basic expressions are used for projects created using Visual Basic and Visual C# projects use C# for expressions. When rehosting the workflow designer outside Visual Studio, the default expression editor doesn’t support IntelliSense or C# expressions. A custom expression editor can be implemented to build the support for IntelliSense and C# expressions.

The following high level steps describe the process to implement the custom expression editor:

  1. Implement the IExpressionEditorService interface. This interface manages the creation and destruction of expression editors.
  2. Implement the IExpressionEditorInstance interface. This interface implements the UI for expression editing UI.
  3. Publish the IExpressionEditorService in your rehosted workflow application.
  4. Implement a custom Expression Activity Editor with C# expression support using a WPF based text editor component like AvalonEdit (https://github.com/icsharpcode/AvalonEdit) and register it to the ExpressionTextBox used by the workflow designer.
  5. Use Roslyn to parse the code into syntax trees that can be used for IntelliSense.

A sample that shows how these steps can be used is at https://github.com/dmetzgar/wf-rehost-roslyn. While it is not a fully working custom expression editor implementation, it implements a custom Expression Activity Editor and shows how to work around the following .NET workflow runtime issues you hit when performing the previous steps.

Expression Editor defaults to VB expressions when implementing custom expression editor.

When rehosting the workflow designer, the workflow runtime uses .NET framework 4.0 by default and this should be set to .NET 4.5 or higher to use the C# expression support. You can do this by using the DesignerConfigurationService class as shown in the following example.

DesignerConfigurationService configurationService = wd.Context.Services.GetService<DesignerConfigurationService>(); configurationService.TargetFrameworkName = new FrameworkName(".NETFramework", new System.Version(4, 5));

By default, the workflow runtime does not allow loading XAML from an untrusted source.

You can again use the DesignerConfigurationService class to configure the workflow designer as shown below.

configurationService.LoadingFromUntrustedSourceEnabled = true;

When loading custom XAML, the workflow runtime defaults to VB in the rehosted designer.

Set the following XAML attached property to indicate workflow runtime that you are using C# expressions in your workflow.

sap2010:ExpressionActivityEditor.ExpressionActivityEditor="C#"