Ejemplo n.º 1
0
        private static void AttachNewInstance(Type compiledResultType, DynamicActivity dynamicActivity)
        {
            /*
             * https://docs.microsoft.com/en-us/dotnet/framework/windows-workflow-foundation/csharp-expressions
             */
            // Create an instance of the new compiled expression type.
            ICompiledExpressionRoot
                compiledExpressionRoot =
                Activator
                .CreateInstance
                (
                    compiledResultType
                    , new object[]
            {
                dynamicActivity
            }
                ) as ICompiledExpressionRoot;

            // Attach it to the activity.
            CompiledExpressionInvoker
            .SetCompiledExpressionRootForImplementation
            (
                dynamicActivity
                , compiledExpressionRoot
            );
        }
Ejemplo n.º 2
0
    public static void CompileExpressions(DynamicActivity dynamicActivity, params Assembly[] references)
    {
        // See https://docs.microsoft.com/en-us/dotnet/framework/windows-workflow-foundation/csharp-expressions
        string activityName      = dynamicActivity.Name;
        string activityType      = activityName.Split('.').Last() + "_CompiledExpressionRoot";
        string activityNamespace = string.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse());
        TextExpressionCompilerSettings settings = new TextExpressionCompilerSettings
        {
            Activity               = dynamicActivity,
            Language               = "C#",
            ActivityName           = activityType,
            ActivityNamespace      = activityNamespace,
            RootNamespace          = null,
            GenerateAsPartialClass = false,
            AlwaysGenerateSource   = true,
            ForImplementation      = true
        };

        // add assembly references
        TextExpression.SetReferencesForImplementation(dynamicActivity, references.Select(a => (AssemblyReference)a).ToList());
        // Compile the C# expression.
        var results = new TextExpressionCompiler(settings).Compile();

        if (results.HasErrors)
        {
            throw new Exception("Compilation failed.");
        }
        // attach compilation result to live activity
        var compiledExpression = (ICompiledExpressionRoot)Activator.CreateInstance(results.ResultType, new object[] { dynamicActivity });

        CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(dynamicActivity, compiledExpression);
    }
        static void CompileExpressions(DynamicActivity dynamicActivity)
        {
            var activityName      = dynamicActivity.Name;
            var activityType      = activityName.Split('.').Last() + "_CompiledExpressionRoot";
            var activityNamespace = string.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse());

            var settings = new TextExpressionCompilerSettings
            {
                Activity               = dynamicActivity,
                Language               = "C#",
                ActivityName           = activityType,
                ActivityNamespace      = activityNamespace,
                RootNamespace          = "CSharpExpression",
                GenerateAsPartialClass = false,
                AlwaysGenerateSource   = true,
                ForImplementation      = true
            };

            var results = new TextExpressionCompiler(settings).Compile();

            if (results.HasErrors)
            {
                throw new Exception("Compilation failed.");
            }

            var compiledExpressionRoot = Activator.CreateInstance(results.ResultType, new object[] { dynamicActivity }) as ICompiledExpressionRoot;

            CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(dynamicActivity, compiledExpressionRoot);
        }
Ejemplo n.º 4
0
        public void SetCompiledExpressionRootForImplementation()
        {
            var writeLine = new WriteLine {
                Text = new InArgument <string>(new VisualBasicValue <string>("[s]"))
            };

            CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(writeLine, new Expressions());
            WorkflowInvoker.Invoke(writeLine);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Compile the Expressions in workflow
        /// </summary>
        /// <param name="id">The workflow Id</param>
        /// <param name="activity">The workflow activity for build the expressions</param>
        static void CompileExpressions(string id, Activity activity)
        {
            Type t = GetType(id, activity);
            ICompiledExpressionRoot compiledExpressionRoot =
                Activator.CreateInstance(t,
                                         new object[] { activity }) as ICompiledExpressionRoot;

            CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(
                activity, compiledExpressionRoot);
        }
Ejemplo n.º 6
0
        static void CreateCompiledActivity(DynamicActivity dynamicActivity, Type resultType)
        {
            ICompiledExpressionRoot compiledExpressionRoot =
                Activator.CreateInstance(resultType,
                                         new Object[] { dynamicActivity }) as ICompiledExpressionRoot;

            // Attach it to the activity.
            CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(
                dynamicActivity, compiledExpressionRoot);
        }
Ejemplo n.º 7
0
        // NOTE : Static method here causes memory leak in the TextExpressioncCompiler ;)
        void CompileExpressionsImpl(Activity dynamicActivity, Guid resourceID)
        {
            // http://msdn.microsoft.com/en-us/library/jj591618.aspx

            // activity.Name is the Namespace.Type of the activity that contains the C# expressions.
            // activity.Name must be in the form Namespace.Type
            string activityName = ToNamespaceTypeString(dynamicActivity.GetType());

            // Split activityName into Namespace and Type and 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.
            var activityType = activityName.Replace(" ", "_").Split('.').Last() + "_CompiledExpressionRoot";

            // Take everything before the last . for the namespace.
            var activityNamespace = string.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse());

            var settings = new TextExpressionCompilerSettings
            {
                Activity               = dynamicActivity,
                Language               = "C#",
                ActivityName           = activityType,
                ActivityNamespace      = activityNamespace,
                RootNamespace          = null,
                GenerateAsPartialClass = false,
                AlwaysGenerateSource   = true,
                ForImplementation      = true
            };
            TextExpressionCompilerResults results;

            if (Resultscache.ContainsKey(resourceID))
            {
                results = Resultscache[resourceID];
            }
            //// Compile the C# expression.
            else
            {
                var compiler = new TextExpressionCompiler(settings);
                results = compiler.Compile(); // Nasty MS memory leak ;(
                Resultscache.TryAdd(resourceID, results);
            }

            if (results.HasErrors)
            {
                var err = new StringBuilder("Compilation failed.\n");
                foreach (var message in results.CompilerMessages)
                {
                    err.AppendFormat("{0} : {1} ({2}) --> {3}\n", message.Number, message.IsWarning ? "WARNING" : "ERROR  ", message.SourceLineNumber, message.Message);
                }
                throw new Exception(err.ToString());
            }

            var compiledExpressionRoot = Activator.CreateInstance(results.ResultType, dynamicActivity) as ICompiledExpressionRoot;

            CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(dynamicActivity, compiledExpressionRoot);
        }
Ejemplo n.º 8
0
        static Type CompileExpressions(DynamicActivity dynamicActivity)
        {
            // activityName is the Namespace.Type of the activity that contains the
            // C# expressions. For Dynamic Activities this can be retrieved using the
            // name property , which must be in the form Namespace.Type.
            String activityName = dynamicActivity.Name;
            // 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               = dynamicActivity,
                Language               = "C#",
                ActivityName           = activityType,
                ActivityNamespace      = activityNamespace,
                RootNamespace          = null,
                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.");
            }

            // Create an instance of the new compiled expression type.
            ICompiledExpressionRoot compiledExpressionRoot =
                Activator.CreateInstance(results.ResultType,
                                         new Object[] { dynamicActivity }) as ICompiledExpressionRoot;

            // Attach it to the activity.
            CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(
                dynamicActivity, compiledExpressionRoot);
            return(results.ResultType);
        }
Ejemplo n.º 9
0
        private static void Compile(IDynamicActivity dynamicActivity, LocationReferenceEnvironment environment)
        {
#if NET45
            string language = null;
            if (RequiresCompilation(dynamicActivity, environment, out language))
            {
                TextExpressionCompiler        compiler = new TextExpressionCompiler(GetCompilerSettings(dynamicActivity, language));
                TextExpressionCompilerResults results  = compiler.Compile();

                if (results.HasErrors)
                {
                    StringBuilder messages = new StringBuilder();
                    messages.Append("\r\n");
                    messages.Append("\r\n");

                    foreach (TextExpressionCompilerError message in results.CompilerMessages)
                    {
                        messages.Append("\t");
                        if (results.HasSourceInfo)
                        {
                            messages.Append(string.Concat(" ", SR.ActivityXamlServiceLineString, " ", message.SourceLineNumber, ": "));
                        }
                        messages.Append(message.Message);
                    }

                    messages.Append("\r\n");
                    messages.Append("\r\n");

                    InvalidOperationException exception = new InvalidOperationException(SR.ActivityXamlServicesCompilationFailed(messages.ToString()));

                    foreach (TextExpressionCompilerError message in results.CompilerMessages)
                    {
                        exception.Data.Add(message, message.Message);
                    }
                    throw FxTrace.Exception.AsError(exception);
                }

                Type compiledExpressionRootType = results.ResultType;

                ICompiledExpressionRoot compiledExpressionRoot = Activator.CreateInstance(compiledExpressionRootType, new object[] { dynamicActivity }) as ICompiledExpressionRoot;
                CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(dynamicActivity, compiledExpressionRoot);
            }
#endif
        }
Ejemplo n.º 10
0
        private static void Compile(IDynamicActivity dynamicActivity, ActivityXamlServicesSettings settings)
        {
            string language = null;

            if (!RequiresCompilation(dynamicActivity, settings.LocationReferenceEnvironment, out language))
            {
                return;
            }
            var cSharpCompiler = settings.CSharpCompiler ?? new CSharpAheadOfTimeCompiler();
            var compiler       = new TextExpressionCompiler(GetCompilerSettings(dynamicActivity, language, cSharpCompiler));
            var results        = compiler.Compile();

            if (results.HasErrors())
            {
                var messages = new StringBuilder();
                messages.Append("\r\n");
                messages.Append("\r\n");

                foreach (TextExpressionCompilerError message in results.CompilerMessages)
                {
                    messages.Append("\t");
                    messages.Append(string.Concat(" ", SR.ActivityXamlServiceLineString, " ", message.SourceLineNumber, ": "));
                    messages.Append(message.Message);
                }

                messages.Append("\r\n");
                messages.Append("\r\n");

                InvalidOperationException exception = new InvalidOperationException(SR.ActivityXamlServicesCompilationFailed(messages.ToString()));

                foreach (TextExpressionCompilerError message in results.CompilerMessages)
                {
                    exception.Data.Add(message, message.Message);
                }
                throw FxTrace.Exception.AsError(exception);
            }

            Type compiledExpressionRootType = results.ResultType;

            ICompiledExpressionRoot compiledExpressionRoot = Activator.CreateInstance(compiledExpressionRootType, new object[] { dynamicActivity }) as ICompiledExpressionRoot;

            CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(dynamicActivity, compiledExpressionRoot);
        }
        private static void AttachNewInstance(Type compiledResultType, DynamicActivity dynamicActivity)
        {
            // Create an instance of the new compiled expression type.
            ICompiledExpressionRoot
                compiledExpressionRoot =
                Activator
                .CreateInstance
                (
                    compiledResultType
                    , new object[]
            {
                dynamicActivity
            }
                ) as ICompiledExpressionRoot;

            // Attach it to the activity.
            CompiledExpressionInvoker
            .SetCompiledExpressionRootForImplementation
            (
                dynamicActivity
                , compiledExpressionRoot
            );
        }
Ejemplo n.º 12
0
        public void Compile(string activityNamespace, string activityName, Activity activity, bool forImplementation = true)
        {
            if (_refAssemblies != null)
            {
                var references = _refAssemblies.Select(a => new AssemblyReference {
                    Assembly = a
                }).ToArray();
                if (!forImplementation)
                {
                    TextExpression.SetReferences(activity, references);
                }
                else
                {
                    TextExpression.SetReferencesForImplementation(activity, references);
                }
            }

            if (_usingNamespaces != null)
            {
                var namespaces = _usingNamespaces.ToArray();
                if (!forImplementation)
                {
                    TextExpression.SetNamespaces(activity, namespaces);
                }
                else
                {
                    TextExpression.SetNamespacesForImplementation(activity, namespaces);
                }
            }

            var settings = new TextExpressionCompilerSettings
            {
                Activity               = activity,
                Language               = "C#",
                ActivityName           = activityName,
                ActivityNamespace      = activityNamespace,
                RootNamespace          = "dd",
                GenerateAsPartialClass = false,
                AlwaysGenerateSource   = false,
                ForImplementation      = forImplementation,
                Compiler               = new CSharpAheadOfTimeCompiler(),
            };
            var results = new TextExpressionCompiler(settings).Compile();

            if (results.HasErrors)
            {
                throw new Exception("Compilation failed.");
            }


            if (results.ResultType == null)
            {
                return;
            }
            var compiledExpressionRoot =
                Activator.CreateInstance(results.ResultType, activity) as ICompiledExpressionRoot;

            if (!forImplementation)
            {
                CompiledExpressionInvoker.SetCompiledExpressionRoot(activity, compiledExpressionRoot);
            }
            else
            {
                CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(activity, compiledExpressionRoot);
            }
        }