locked
Expression Activity type 'CSharpValue`1' requires compilation in order to run RRS feed

  • Question

  • Hi,

    I'm trying to get an Activity to run but I get this error at runtime.

    The set up is the following: I have an Activity built around another Activity built around a NativeActivity.

    the first level encapsulation runs, not the second. I just don't understand why, nor what I should do to solve the issue.

    I used the code from there to compile my 3 activities:

    http://msdn.microsoft.com/en-us/library/jj591618%28v=vs.110%29.aspx#CodeWorkflows

    please help?

    here is a code sample that should highlight the problem...

    public class SimpleActivity : NativeActivity<SimpleData>
        {
            [RequiredArgument]
            public InArgument<StringContainer> StringContainer { get; set; }
    
            [RequiredArgument]
            public InArgument<IntContainer> IntContainer { get; set; }
    
            protected override void Execute(NativeActivityContext context)
            {
                this.Result.Set(context, new SimpleData(context.GetValue(IntContainer).Value,
                                                        context.GetValue(StringContainer).Value));
            }
        }
    
    
        public class MotherActivity : Activity
        {
            [RequiredArgument]
            public InArgument<IntContainer> IntToAdd { get; set; }
    
            [RequiredArgument]
            public InArgument<SortedList<IntContainer, SimpleData>> DataToProcess { get; set; }
    
            [RequiredArgument]
            public OutArgument<SortedList<IntContainer, SimpleData>> ProcessedData { get; set; }
    
            
            private DelegateInArgument<SimpleData> simpleDataIterator;
            private Variable<SimpleData> wfvar_tmpsimpledata;
            private SimpleData native_tmpsimpledata;
    
            protected override Func<Activity> Implementation
            {
                get
                {
                    return () =>
                    {
                        Sequence seq = new Sequence
                        {
                            Variables =
                            {
                               
                            },
                            Activities = 
                            {
                                new WriteLine
                                {
                                    Text = new InArgument<String>((env) => "I'm entering the mother activity processing nb = " + DataToProcess.Get(env).Count)
                                },
                                new Assign<SortedList<IntContainer, SimpleData>>
                                {
                                    To = new CSharpReference<SortedList<IntContainer, SimpleData>>("ProcessedData"),
                                    Value = new CSharpValue<SortedList<IntContainer, SimpleData>>("new SortedList<IntContainer, SimpleData>()"),
                                },
                                new ForEach<SimpleData>
                                {
                                    Values = new InArgument<IEnumerable<SimpleData>>((env) => (IList<SimpleData>)DataToProcess.Get(env).Values),
                                    Body = new ActivityAction<SimpleData>()
                                    {
                                         Argument = simpleDataIterator,
                                         Handler = new Sequence()
                                         {
                                             Variables = 
                                             {
                                                  wfvar_tmpsimpledata
                                             },
                                             
                                             Activities = 
                                             {
                                                 new Assign<SimpleData>
                                                 {
                                                     To = new CSharpReference<SimpleData>("WFVAR_tmpsimpledata"),
                                                     Value = new InArgument<SimpleData>((env) => simpleDataIterator.Get(env))
                                                 },
                                                 new SimpleActivity
                                                 {
                                                      IntContainer = new CSharpValue<IntContainer>("new WFCS.IntContainer(IntToAdd.Value + WFVAR_tmpsimpledata.ID.Value);"),
                                                      StringContainer = new CSharpValue<StringContainer>("new WFCS.StringContainer(\"toto\");"),
                                                      Result = new CSharpReference<SimpleData>("WFVAR_tmpsimpledata")
                                                 },
                                                 new AddToCollection<KeyValuePair<IntContainer, SimpleData>>
                                                 {
                                                     Collection = new InArgument<ICollection<KeyValuePair<IntContainer,SimpleData>>>((env) => ProcessedData.Get(env)),
                                                     Item = new InArgument<KeyValuePair<IntContainer, SimpleData>>( (env) => (new KeyValuePair<IntContainer, SimpleData>(wfvar_tmpsimpledata.Get(env).ID, 
                                                                                                                                                                         wfvar_tmpsimpledata.Get(env))))
                                                 }
                                             }
                                         }
                                    }
                                }
                            }
                            
                        };
                        return seq;
                    };
                }
                set
                {
                    base.Implementation = value;
                }
             }
        
        
            public MotherActivity()
            {
                simpleDataIterator = new DelegateInArgument<SimpleData>();
                native_tmpsimpledata = new SimpleData(0, "notset");
                wfvar_tmpsimpledata = new Variable<SimpleData>("WFVAR_tmpsimpledata", ctx => native_tmpsimpledata);
            }
        
        
        }
    
    
        public class GrandMotherActivity : Activity
        {
            // remove this "fake" input
            //[RequiredArgument]
            //public InArgument<SortedList<IntContainer, SimpleData>> DataToProcess { get; set; }
    
            [RequiredArgument]
            public OutArgument<SortedList<IntContainer, SimpleData>> ProcessedData { get; set; }
    
            IntContainer native_intcontainer;
            Variable<IntContainer> wfvar_intcontainer;
            SortedList<IntContainer, SimpleData> native_dataToProcess;
            Variable<SortedList<IntContainer, SimpleData>> wfvar_dataToProcess;
    
            protected override Func<Activity> Implementation
            {
                get
                {
                    return () => 
                    {
                        Sequence seq = new Sequence
                        {
                            Variables =
                            {
                                wfvar_intcontainer, wfvar_dataToProcess
                            },
                            Activities = 
                            { 
                                new Assign<SortedList<IntContainer, SimpleData>>
                                {
                                    To = new CSharpReference<SortedList<IntContainer, SimpleData>>("WFVAR_dataToProcess"),
                                    Value = new CSharpValue<SortedList<IntContainer, SimpleData>>("new SortedList<IntContainer, SimpleData>()"),
                                },
                                new Assign<SortedList<IntContainer, SimpleData>>
                                {
                                    To = new CSharpReference<SortedList<IntContainer, SimpleData>>("ProcessedData"),
                                    Value = new CSharpValue<SortedList<IntContainer, SimpleData>>("new SortedList<IntContainer, SimpleData>()"),
                                },
                                new AddToCollection<KeyValuePair<IntContainer, SimpleData>>
                                {
                                    Collection = wfvar_dataToProcess,
                                    Item = new InArgument<KeyValuePair<IntContainer, SimpleData>>( (env) => (new KeyValuePair<IntContainer, SimpleData>(new IntContainer(1), 
                                                                                                                                                        new SimpleData(1, "toto"))))
                                },
                                new AddToCollection<KeyValuePair<IntContainer, SimpleData>>
                                {
                                    Collection = wfvar_dataToProcess,
                                    Item = new InArgument<KeyValuePair<IntContainer, SimpleData>>( (env) => (new KeyValuePair<IntContainer, SimpleData>(new IntContainer(2), 
                                                                                                                                                        new SimpleData(2, "titi"))))
                                },
                                new WriteLine
                                {
                                    Text = new InArgument<String>((env) => "I'm in the grandma activity processing nb = " + wfvar_dataToProcess.Get(env).Count)
                                },
                                new MotherActivity
                                {
                                    IntToAdd = new CSharpValue<IntContainer>("WFVAR_intcontainer"),
                                    DataToProcess = new CSharpValue<SortedList<IntContainer, SimpleData>>("WFVAR_dataToProcess"),
                                    ProcessedData = new CSharpReference<SortedList<IntContainer,SimpleData>>("ProcessedData")
                                }
                            }
                        };
                        return seq;
                    };
                }
                set
                {
                    base.Implementation = value;
                }
            }
        
        
            public GrandMotherActivity()
            {
                native_intcontainer = new IntContainer(42);
                wfvar_intcontainer = new Variable<IntContainer>("WFVAR_intcontainer", ctx => native_intcontainer);
    
                //native_dataToProcess = new SortedList<IntContainer, SimpleData>();
                //wfvar_dataToProcess = new Variable<SortedList<IntContainer, SimpleData>>("WFVAR_dataToProcess", ctx => native_dataToProcess);
    
                wfvar_dataToProcess = new Variable<SortedList<IntContainer, SimpleData>>("WFVAR_dataToProcess");
           
            }
        
        }
    
    
        public sealed class ActivityCompiler 
        {
            private static readonly ActivityCompiler instance_ = new ActivityCompiler();
    
                public static ActivityCompiler Instance
                {
                    get
                    {
                        return instance_;
                    }
                }
    
                public SimpleActivity GetSimpleActivity()
                {
                    SimpleActivity act = new SimpleActivity();
                    AttachableMemberIdentifier impl = new AttachableMemberIdentifier(typeof(TextExpression), "Namespaces");
                    AttachablePropertyServices.SetProperty(act, impl, ActivityCompiler.Instance.GetSimpleActivityNameSpaceList());
                    TextExpression.SetReferencesForImplementation(act, ActivityCompiler.Instance.GetSimpleActivityAssemblyReferenceList().ToList());
                    ActivityCompiler.Instance.Compile(act);
                    return act;
                }
    
    
                public Boolean Compile(Activity aActivity)
                {
                    try
                    {
                        // activityName is the Namespace.Type of the activity that contains the
                        // C# expressions.
                        string activityName = aActivity.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";
                        // Take everything before the last . for the namespace.
                        string activityNamespace = string.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse());
    
                        // Create a TextExpressionCompilerSettings.
                        TextExpressionCompilerSettings settings = new TextExpressionCompilerSettings
                        {
                            Activity = aActivity,
                            Language = "C#",
                            ActivityName = activityType,
                            ActivityNamespace = activityNamespace,
                            RootNamespace = "CSharpExpression",
                            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[] { aActivity }) as ICompiledExpressionRoot;
    
                        // Attach it to the activity.
                        CompiledExpressionInvoker.SetCompiledExpressionRoot(
                            aActivity, compiledExpressionRoot);
                    }
                    catch (Exception e)
                    {
                        string err = e.ToString();
                        return false;
                    }
                    return true;
                }
    
    
                public Boolean CompileDynamicActivity(Activity aDynamicActivity)
                {
                    try
                    {
                        // activityName is the Namespace.Type of the activity that contains the
                        // C# expressions.
                        string activityName = aDynamicActivity.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";
                        // Take everything before the last . for the namespace.
                        string activityNamespace = string.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse());
    
                        // Create a TextExpressionCompilerSettings.
                        TextExpressionCompilerSettings settings = new TextExpressionCompilerSettings
                        {
                            Activity = aDynamicActivity,
                            Language = "C#",
                            ActivityName = activityType,
                            ActivityNamespace = activityNamespace,
                            RootNamespace = "CSharpExpression",
                            GenerateAsPartialClass = false,
                            AlwaysGenerateSource = true,
                            ForImplementation = true
                        };
    
                        // 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 in ActivityCompiler");
                        }
    
                        // Create an instance of the new compiled expression type.
                        ICompiledExpressionRoot compiledExpressionRoot =
                            Activator.CreateInstance(results.ResultType,
                                new object[] { aDynamicActivity }) as ICompiledExpressionRoot;
    
                        // Attach it to the activity.
                        CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(
                            aDynamicActivity, compiledExpressionRoot);
                    }
                    catch (Exception e)
                    {
                        string err = e.ToString();
                        throw e ;
                    }
                    return true;
                }
    
                private HashSet<AssemblyReference> GetSimpleActivityAssemblyReferenceList()
                {
                    HashSet<AssemblyReference> ass = new HashSet<AssemblyReference>();
                    ass.Add(new AssemblyReference { Assembly = typeof(StringContainer).Assembly, AssemblyName = typeof(StringContainer).Assembly.GetName() });
                    ass.Add(new AssemblyReference { Assembly = typeof(IntContainer).Assembly, AssemblyName = typeof(IntContainer).Assembly.GetName() });
                    ass.Add(new AssemblyReference { Assembly = typeof(System.Runtime.Serialization.IExtensibleDataObject).Assembly, AssemblyName = typeof(IExtensibleDataObject).Assembly.GetName() });
                    return ass;
                }
    
                private HashSet<String> GetSimpleActivityNameSpaceList()
                {
                    HashSet<String> ns = new HashSet<String>();
                    ns.Add(typeof(StringContainer).Namespace);
                    ns.Add(typeof(IntContainer).Namespace);
                    return ns;
                }
    
                public MotherActivity GetMotherActivity()
                {
                    MotherActivity act = new MotherActivity();
                    AttachableMemberIdentifier impl = new AttachableMemberIdentifier(typeof(TextExpression), "NamespacesForImplementation");
                    AttachablePropertyServices.SetProperty(act, impl, ActivityCompiler.Instance.GetMotherActivityNameSpaceList());
                    TextExpression.SetReferencesForImplementation(act, ActivityCompiler.Instance.GetMotherActivityAssemblyReferenceList().ToList());
                    ActivityCompiler.Instance.CompileDynamicActivity(act);
                    return act;
                }
    
                private HashSet<AssemblyReference> GetMotherActivityAssemblyReferenceList()
                {
                    HashSet<AssemblyReference> ass = new HashSet<AssemblyReference>();
                    ass.UnionWith(ActivityCompiler.Instance.GetSimpleActivityAssemblyReferenceList());
                    ass.Add(new AssemblyReference { Assembly = typeof(System.Runtime.Serialization.IExtensibleDataObject).Assembly, AssemblyName = typeof(IExtensibleDataObject).Assembly.GetName() });
                    return ass;
                }
    
                private HashSet<String> GetMotherActivityNameSpaceList()
                {
                    HashSet<String> ns = new HashSet<String>();
                    ns.UnionWith(ActivityCompiler.Instance.GetSimpleActivityNameSpaceList());
                    return ns;
                }
    
                public GrandMotherActivity GetGrandMotherActivity()
                {
                    GrandMotherActivity act = new GrandMotherActivity();
                    AttachableMemberIdentifier impl = new AttachableMemberIdentifier(typeof(TextExpression), "NamespacesForImplementation");
                    AttachablePropertyServices.SetProperty(act, impl, ActivityCompiler.Instance.GetGrandMotherActivityNameSpaceList());
                    TextExpression.SetReferencesForImplementation(act, ActivityCompiler.Instance.GetGrandMotherActivityAssemblyReferenceList().ToList());
                    ActivityCompiler.Instance.CompileDynamicActivity(act);
                    return act;
                }
    
                private HashSet<AssemblyReference> GetGrandMotherActivityAssemblyReferenceList()
                {
                    HashSet<AssemblyReference> ass = new HashSet<AssemblyReference>();
                    ass.UnionWith(ActivityCompiler.Instance.GetMotherActivityAssemblyReferenceList());
                    return ass;
                }
    
                private HashSet<String> GetGrandMotherActivityNameSpaceList()
                {
                    HashSet<String> ns = new HashSet<String>();
                    ns.UnionWith(ActivityCompiler.Instance.GetMotherActivityNameSpaceList());
                    return ns;
                }
        }
    
    

    Monday, November 24, 2014 1:08 PM

All replies

  • Hi,

    I find similar discussion which has been resolved, so please refer to

    https://social.msdn.microsoft.com/Forums/en-US/2d66ecfc-be1f-42d4-9b81-564117ca9f87/expression-activity-type-csharpvalue1-requires-compilation-in-order-to-run?forum=TFService


    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.

    Tuesday, November 25, 2014 10:31 AM
  • Hi,

    thank you for the answer, but sadly it does not answer my problem.

    I had seen this other post, I have been looking for a solution for quite some time and went through all the links I've found.

    Now obviously I might have missed something, but as far as I understand, the post you are refering to and the two links it contains are describing how to compile dynamic activities using the CSharpValue, which is what I do.

    Also, not sure if you had a look at my test case, but my MotherActivity and my GrandMotherActivity are compiled the same way, so I do not understand why one would run successfully and not the other.

    Tuesday, November 25, 2014 1:19 PM