private static bool TryGetCompiledResultType ( DynamicActivity activity , out Type type ) { type = null; var settings = GetCompilerSettings(activity); var results = new TextExpressionCompiler (settings) .Compile(); var r = results.HasErrors; if (!r) { type = results .ResultType; r = (type == null); } if (r) { throw new Exception("Compilation failed."); } return(r); }
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); }
// 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); }
/// <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); }
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); }
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))); } } }
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 }
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 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); }
bool Execute() { Assembly localAssembly = Utilities.GetLocalAssembly(buildContext, SR.LocalAssemblyNotLoaded_Expressions); foreach (Type type in Utilities.GetTypes(localAssembly)) { if (Utilities.IsTypeAuthoredInXaml(type)) { Exception ctorException; Activity activity = Utilities.CreateActivity(type, out ctorException); if (ctorException != null) { LogError(SR.ExpressionBuildExtensionConstructorFailed(type.FullName, ctorException != null ? ctorException.Message : string.Empty)); } else if (activity != null) { string activityName = activity.GetType().Name; string activityNamespace = ""; string fullActivityNamespace = activity.GetType().Namespace; if (string.Equals(buildContext.Language, "VB", StringComparison.OrdinalIgnoreCase)) { if (string.IsNullOrWhiteSpace(buildContext.RootNamespace)) { activityNamespace = fullActivityNamespace; } else { int firstIndex = fullActivityNamespace.IndexOf(buildContext.RootNamespace, StringComparison.Ordinal); if (firstIndex != -1) { int subStringIndex = firstIndex + buildContext.RootNamespace.Length + 1; if (subStringIndex < fullActivityNamespace.Length) { activityNamespace = fullActivityNamespace.Substring(subStringIndex); } else { activityNamespace = ""; } } else { activityNamespace = ""; } } } else { activityNamespace = fullActivityNamespace; } TextExpressionCompiler compiler = new TextExpressionCompiler( new TextExpressionCompilerSettings() { Activity = activity, ActivityName = activityName, ActivityNamespace = activityNamespace, Language = buildContext.Language, RootNamespace = buildContext.RootNamespace, LogSourceGenerationMessage = LogMessage, AlwaysGenerateSource = false }); string filePath = Path.GetFullPath(buildContext.OutputPath); string codeFileName = Path.Combine(filePath, activityNamespace + "_" + activityName + fileNameSuffix + "." + CodeDomProvider.CreateProvider(this.buildContext.Language).FileExtension); bool fileWritten = false; using (StreamWriter fileStream = new StreamWriter(codeFileName)) { try { fileWritten = compiler.GenerateSource(fileStream); } catch (Exception ex) { if (Fx.IsFatal(ex)) { throw; } LogError(ex.Message); } } if (fileWritten) { // Batch up all file generation until the end, because we don't want to emit // any source if we can't complete compilation due to an unloadable reference. this.generatedFiles.Add(codeFileName); } } } } return(true); }
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); } }