WF 4.5 Compile CSharpValue error. What is the correct method? RRS feed

  • Question

  • I've been uniting testing two simple examples of compiling CSharpValue activities. One works and the other doesn't. I can't figure out why. I've read all the posts on this topic but can't seem to find an example/discussion that would explain this behavior.If someone could point out the issue and optionally a change to correct it if possible.


    The first unit test works SequenceActivityCompile() the second CodeActivityCompile fails with a NotSupportedException (Expression Activity type CSharpValue requires compilation in order to run. Please ensure that the workflow has been compiled.)

    I heard somewhere this can be related to ForImplementation but CodeActivityCompile has the same error whether its value is true or false.

    This example is a basic adaption of the Microsoft example at:

    This example blog post discussing compiling C# expressions in WF 4+ at length. If anyone reaching this question needs a basic introduction to the topic:

    Related Code:

        public void SequenceActivityCompile()
            Activity sequence = new Sequence
                Activities = { new CSharpValue<string>("\"Hello World \"") }
            var result = WorkflowInvoker.Invoke(sequence);
        public void CodeActivityCompile()
            var code = new CSharpValue<String>("\"Hello World\"");
            var result = WorkflowInvoker.Invoke(code);
        void CompileExpressions(Activity activity)
            // activityName is the Namespace.Type of the activity that contains the
            // C# expressions.
            string activityName = activity.GetType().ToString();
            // Split activityName into Namespace and Type.Append _CompiledExpressionRoot to the type name
            // to represent the new type that represents the compiled expressions.
            // Take everything after the last . for the type name.
            //string activityType = activityName.Split('.').Last() + "_CompiledExpressionRoot";
            string activityType = "TestType";
            // Take everything before the last . for the namespace.
            //string activityNamespace = string.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse());
            string activityNamespace = "TestSpace";
            // Create a TextExpressionCompilerSettings.
            TextExpressionCompilerSettings settings = new TextExpressionCompilerSettings
                Activity = activity,
                Language = "C#",
                ActivityName = activityType,
                ActivityNamespace = activityNamespace,
                RootNamespace = null,
                GenerateAsPartialClass = false,
                AlwaysGenerateSource = true,
                ForImplementation = false
            // Compile the C# expression.
            TextExpressionCompilerResults results =
                new TextExpressionCompiler(settings).Compile();
            // Any compilation errors are contained in the CompilerMessages.
            if (results.HasErrors)
                throw new Exception("Compilation failed.");
            // Create an instance of the new compiled expression type.
            ICompiledExpressionRoot compiledExpressionRoot =
                    new object[] { activity }) as ICompiledExpressionRoot;
            // Attach it to the activity.
                activity, compiledExpressionRoot);
    Monday, November 23, 2015 6:18 PM

All replies

  • Hi,

    Please try to set ForImplementation as true in TextExpressionCompilerSettings. And please refer to the similar discussion:

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, November 25, 2015 9:16 AM
  • Thank you so much for the reply Pengzhen and the suggestions.

    Setting for Implementation doesn't appear to have any effect on the CodeActivityCompile unit test in my testing. Are you willing/able to confirm in Visual Studio that your seeing the same behavior?

    Also thank you for the link to the stack overflow discussion. The question doesn't appear to have an accepted answer or have been resolved. What would you suggest I could apply to this issue?


    • Edited by LinksDeveloper Wednesday, November 25, 2015 3:55 PM Finished Reviewing Link
    Wednesday, November 25, 2015 2:57 PM