Example #1
0
    static 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";
        // 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               = 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.
        CompiledExpressionInvoker.SetCompiledExpressionRoot(
            activity, compiledExpressionRoot);
    }
        static WorkflowService CreatetWorkflowServiceAndSetCompiledExpressionRoot(string supportedVersionXamlxfilePath, Stream activityStream, XName defaultServiceName)
        {
            WorkflowService service = CreatetWorkflowService(activityStream, defaultServiceName);

            if (service != null)
            {
                // CompiledExpression is not supported on Configuration Based Activation (CBA) scenario.
                if (ServiceHostingEnvironment.IsHosted && !ServiceHostingEnvironment.IsConfigurationBased)
                {
                    // We use ServiceHostingEnvironment.FullVirtualPath (instead of the constructor string) because we’re passing this path to BuildManager,
                    // which may not understand the file type referenced by the constructor string (e.g. .xaml).
                    ICompiledExpressionRoot expressionRoot = XamlBuildProviderExtension.GetExpressionRoot(supportedVersionXamlxfilePath, service, ServiceHostingEnvironment.FullVirtualPath);
                    if (expressionRoot != null)
                    {
                        CompiledExpressionInvoker.SetCompiledExpressionRoot(service.Body, expressionRoot);
                    }
                }
            }

            return(service);
        }
Example #3
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);
        }
Example #4
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);
            }
        }