Esempio n. 1
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);
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        /// <summary>
        /// Compiled the activity and get the complied expressions type
        /// </summary>
        /// <param name="activity">The activity for compiled</param>
        /// <returns>The complied expressions type</returns>
        static Type GetType(DynamicActivity activity)
        {
            TextExpressionCompilerSettings settings = GetCompilerSettings(activity);
            TextExpressionCompilerResults  results  =
                new TextExpressionCompiler(settings).Compile();

            if (results.HasErrors)
            {
                throw new Exception("Compilation failed.");
            }
            return(results.ResultType);
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
        void GenerateSource(bool isSupportedVersion, string filePath, AssemblyBuilder assemblyBuilder, WorkflowService workflowService, out string codeFileName, out bool generatedSource, out string activityName)
        {
            // Get unique file and type name for the workflowservice
            codeFileName = assemblyBuilder.GetTempFilePhysicalPath(assemblyBuilder.CodeDomProvider.FileExtension);

            if (isSupportedVersion)
            {
                activityName = WorkflowServiceHostFactory.GetSupportedVersionGeneratedTypeName(filePath);
            }
            else
            {
                activityName = workflowService.Name.LocalName + "_" + Guid.NewGuid().ToString().Replace("-", "_");
            }

            TextExpressionCompilerSettings settings = new TextExpressionCompilerSettings
            {
                Activity               = workflowService.Body,
                ActivityName           = activityName,
                ActivityNamespace      = GeneratedNamespace,
                Language               = CodeDomProvider.GetLanguageFromExtension(assemblyBuilder.CodeDomProvider.FileExtension),
                GenerateAsPartialClass = false,
                AlwaysGenerateSource   = false,
                ForImplementation      = false
            };

            TextExpressionCompiler compiler = new TextExpressionCompiler(settings);

            generatedSource = false;
            using (StreamWriter fileStream = new StreamWriter(codeFileName))
            {
                try
                {
                    generatedSource = compiler.GenerateSource(fileStream);
                }
                catch (Exception ex)
                {
                    if (Fx.IsFatal(ex))
                    {
                        throw;
                    }

                    throw FxTrace.Exception.AsError(new HttpCompileException(SR.XamlBuildProviderExtensionException(ex.Message)));
                }
            }
        }
Esempio n. 7
0
        private static void Compile(Activity activity)
        {
            string activityName      = activity.GetType().ToString();
            string activityType      = activityName.Split('.').Last() + "_CompiledExpressionRoot";
            string activityNamespace = string.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse());
            var    settings          = new TextExpressionCompilerSettings
            {
                Activity               = activity,
                Language               = "C#",
                ActivityName           = activityType,
                ActivityNamespace      = activityNamespace,
                RootNamespace          = null,
                GenerateAsPartialClass = false,
                AlwaysGenerateSource   = true,
                ForImplementation      = false,
#if NET5_0
                Compiler = new CSharpAotCompiler()
#endif
            };

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

            if (results.HasErrors)
            {
                foreach (var m in results.CompilerMessages)
                {
                    System.Diagnostics.Debug.WriteLine(m.Message);
                }

                throw new Exception("Compilation failed.");
            }

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

            CompiledExpressionInvoker.SetCompiledExpressionRoot(activity, compiledExpressionRoot);
        }
        static void CompileExpressions(Activity activity)
        {
            // activityName is the Namespace.Type of the activity that contains the C# expressions.
            var 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.
            var activityType = activityName.Split('.').Last() + "_CompiledExpressionRoot";
            // Take everything before the last . for the namespace.
            var activityNamespace = string.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse());
            // Create a TextExpressionCompilerSettings.
            var settings = new TextExpressionCompilerSettings {
                Activity               = activity,
                Language               = "C#",
                ActivityName           = activityType,
                ActivityNamespace      = activityNamespace,
                RootNamespace          = null,
                GenerateAsPartialClass = false,
                AlwaysGenerateSource   = true,
                ForImplementation      = false
            };
            // Compile the C# expression.
            var 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.
            var compiledExpressionRoot = Activator.CreateInstance(results.ResultType, new object[] { activity }) as ICompiledExpressionRoot;

            // Attach it to the activity.
            CompiledExpressionInvoker.SetCompiledExpressionRoot(activity, compiledExpressionRoot);
        }
Esempio n. 9
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);
            }
        }