WF4.5- Expression Activity type 'CSharpValue`1' requires compilation in order to run

985 Views Asked by At

Background

What I'm trying to do is to have a scoped variable of one of my models in the xaml.

In my workflow project "MyProject.Workflows" I have created model classes, code activities and Xaml files. They are all under same namespace. In another project ("Engine"), I load and execute these workflows.

To load the workflows in the "Engine", I use ActivityXamlServices with ActivityXamlServicesSettings including CompileExpressions = true.

When loading the ActivityXamlServices, I use a XamlXmlReader with XamlXmlReaderSettings where I actually point to the "MyProject.Workflows" dll.

Since Both these projects are in the same solution I actually referred MyProject.Workflows in the "Engine".

Because Earlier, they were in different solutions, So when I tried to do this It gave me It cant find the "MyProject.Workflows" dll even though I point it in the XamlXmlReaderSettings.

Then I tried to load the dll to the app domain and then it worked.But I did not want to deal with App Domains so I decided to get both projects under one solution so I can refer the "MyProject.Workflows" in the "Engine".

Issue:

If I use one of those models inside of the Xaml as an expression like "Assign Activity" the Workflow isn't getting compiled when I try to execute this.

For example if I use this in an "Assign" activity having a scoped variable of type MyObject

Newtonsoft.Json.JsonConvert.DeserializeObject<MyProject.Workflows.Models.MyObject>(inputString);

I will get the below error message when I run the workflow.

NotSupportedException:'Expression Activity type 'CSharpValue`1' requires compilation in order to run.  Please ensure that the workflow has been compiled.

If I remove these objects and just deal with strings or ints, it works fine.

Things I found in my research:

  1. I found this was a bug in .Net Framework 4.5. But Im using 4.6
  2. Even though I used CompileExpressions = true , I tried this compile method I found. But did not change a thing.

     private static void Compile(DynamicActivity dynamicActivity)
    {
        TextExpressionCompilerSettings settings = new TextExpressionCompilerSettings
        {
            Activity = dynamicActivity,
            Language = "C#",
            ActivityName = dynamicActivity.Name.Split('.').Last() + "_CompiledExpressionRoot",
            ActivityNamespace = string.Join(".", dynamicActivity.Name.Split('.').Reverse().Skip(1).Reverse()),
            RootNamespace = null,
            GenerateAsPartialClass = false,
            AlwaysGenerateSource = true,
        };
    
        TextExpressionCompilerResults results =
            new TextExpressionCompiler(settings).Compile();
        if (results.HasErrors)
        {
            throw new Exception("Compilation failed.");
        }
    
        ICompiledExpressionRoot compiledExpressionRoot =
            Activator.CreateInstance(results.ResultType,
                new object[] { dynamicActivity }) as ICompiledExpressionRoot;
        CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(
            dynamicActivity, compiledExpressionRoot);
    }
    
  3. I read that some people faced this problem and they had to actually move the models to a diffrent namespace. I did that too. Didn't fix the problem.

My Xaml file has this entry added at the top.

 xmlns:local="clr-namespace:MyProject.Workflows.Models"

Can someone please help me to get through this?

0

There are 0 best solutions below