none
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.

    Details:

    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: https://msdn.microsoft.com/en-us/library/jj591618(v=vs.110).aspx

    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:http://blogs.msdn.com/b/tilovell/archive/2012/05/25/wf4-5-using-csharpvalue-lt-t-gt-and-csharpreference-lt-t-gt-in-net-4-5-compiling-expressions-and-changes-in-visual-studio-generated-xaml.aspx

    Related Code:

        [TestMethod]
        public void SequenceActivityCompile()
        {
    
    
    
            Activity sequence = new Sequence
            {
                Activities = { new CSharpValue<string>("\"Hello World \"") }
            };
    
    
            CompileExpressions(sequence);
            var result = WorkflowInvoker.Invoke(sequence);
    
    
        }
    
    
    
        [TestMethod]
        public void CodeActivityCompile()
        {
    
            var code = new CSharpValue<String>("\"Hello World\"");
            CompileExpressions(code);
            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 =
                Activator.CreateInstance(results.ResultType,
                    new object[] { activity }) as ICompiledExpressionRoot;
    
            // Attach it to the activity.
            System.Activities.Expressions.CompiledExpressionInvoker.SetCompiledExpressionRoot(
                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:

    http://stackoverflow.com/questions/27062890/wf-4-5-1-expression-activity-type-csharpvalue1-requires-compilation-in-order


    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