public void CompileAssemblyAsync(string outputAssemblyName, string[] sourceContent, string[] referenceAssemblies, Action <Assembly, Exception> callback, params string[] resourceFiles) { #if WINDOWS_PHONE throw new TaupoNotSupportedException("This method is not supported on the Windows Phone platform"); #endif #if !SILVERLIGHT if (this.CompilerService == null) { try { this.EnsureTempDirectoriesExist(); string outputFile = Path.Combine(this.TempAssemblyDirectory, outputAssemblyName + ".dll"); this.Logger.WriteLine(LogLevel.Trace, "Compiling {0} files to {1} using {2} reference assemblies:", sourceContent.Length, outputFile, referenceAssemblies.Length); string[] resolvedReferenceAssemblies = this.ResolveReferenceAssemblies(referenceAssemblies); this.CompileAssemblyFromSource(outputFile, sourceContent, resolvedReferenceAssemblies, resourceFiles); var uniqueReferenceAssemblies = resolvedReferenceAssemblies .Select(c => Path.GetDirectoryName(c)) .Concat(new[] { this.TempAssemblyDirectory }) .Where(c => !string.IsNullOrEmpty(c)) .Distinct() .ToArray(); var assembly = AssemblyHelpers.LoadAssembly(outputFile, uniqueReferenceAssemblies); callback(assembly, null); } catch (Exception ex) { callback(null, ex); } return; } #endif #if !WINDOWS_PHONE try { if (resourceFiles != null && resourceFiles.Length > 0) { callback(null, new TaupoArgumentException("cannot handle compiling embedded resources, resource files should not be specified. ")); return; } ICompilerService client = this.CreateRemoteCompiler(); this.Logger.WriteLine(LogLevel.Trace, "Remotely compiling {0} files to {1} using {2} reference assemblies:", sourceContent.Length, outputAssemblyName, referenceAssemblies.Length); foreach (var refAsm in referenceAssemblies) { this.Logger.WriteLine(LogLevel.Trace, " {0}", refAsm); } #if WIN8 var compileAssemblyRequest = new CompileAssemblyRequest { outputAssemblyName = outputAssemblyName, projectFileExtension = this.ProjectFileExtension, projectType = this.RemoteCompilerType, referenceAssemblies = new System.Collections.ObjectModel.ObservableCollection <string>(referenceAssemblies), sourceFileExtension = this.FileExtension, sources = new System.Collections.ObjectModel.ObservableCollection <string>(sourceContent), }; var compileAssemblyTask = client.CompileAssemblyAsync(compileAssemblyRequest); compileAssemblyTask.Wait(); var compileAssemblyResponse = compileAssemblyTask.Result; byte[] assemblyBytes = compileAssemblyResponse.CompileAssemblyResult; if (assemblyBytes == null) { callback(null, new TaupoInvalidOperationException("Error compiling assembly: " + compileAssemblyResponse.errorLog)); return; } Assembly clientTypesAssembly = this.LoadDynamicAssembly(assemblyBytes); callback(clientTypesAssembly, null); #else client.BeginCompileAssembly( this.RemoteCompilerType, this.FileExtension, this.ProjectFileExtension, outputAssemblyName, sourceContent, referenceAssemblies, result => { try { string errorLog; byte[] assemblyBytes = client.EndCompileAssembly(out errorLog, result); if (assemblyBytes == null) { callback(null, new TaupoInvalidOperationException("Error compiling assembly: " + errorLog)); return; } #if SILVERLIGHT System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { Assembly clientTypesAssembly = this.LoadDynamicAssembly(assemblyBytes); callback(clientTypesAssembly, null); }); #else string cachePath = Path.Combine(this.TempAssemblyDirectory, outputAssemblyName + ".dll"); this.EnsureTempDirectoriesExist(); File.WriteAllBytes(cachePath, assemblyBytes); var assembly = AssemblyHelpers.LoadAssembly(cachePath, this.TempAssemblyDirectory); callback(assembly, null); #endif } catch (Exception ex) { callback(null, ex); } }, null); #endif } catch (Exception ex) { callback(null, ex); } #endif }
public void CompileAssemblyAsync(string outputAssemblyName, string[] sourceContent, string[] referenceAssemblies, Action<Assembly, Exception> callback, params string[] resourceFiles) { #if WINDOWS_PHONE throw new TaupoNotSupportedException("This method is not supported on the Windows Phone platform"); #endif #if !SILVERLIGHT if (this.CompilerService == null) { try { this.EnsureTempDirectoriesExist(); string outputFile = Path.Combine(this.TempAssemblyDirectory, outputAssemblyName + ".dll"); this.Logger.WriteLine(LogLevel.Trace, "Compiling {0} files to {1} using {2} reference assemblies:", sourceContent.Length, outputFile, referenceAssemblies.Length); string[] resolvedReferenceAssemblies = this.ResolveReferenceAssemblies(referenceAssemblies); this.CompileAssemblyFromSource(outputFile, sourceContent, resolvedReferenceAssemblies, resourceFiles); var uniqueReferenceAssemblies = resolvedReferenceAssemblies .Select(c => Path.GetDirectoryName(c)) .Concat(new[] { this.TempAssemblyDirectory }) .Where(c => !string.IsNullOrEmpty(c)) .Distinct() .ToArray(); var assembly = AssemblyHelpers.LoadAssembly(outputFile, uniqueReferenceAssemblies); callback(assembly, null); } catch (Exception ex) { callback(null, ex); } return; } #endif #if !WINDOWS_PHONE try { if (resourceFiles != null && resourceFiles.Length > 0) { callback(null, new TaupoArgumentException("cannot handle compiling embedded resources, resource files should not be specified. ")); return; } ICompilerService client = this.CreateRemoteCompiler(); this.Logger.WriteLine(LogLevel.Trace, "Remotely compiling {0} files to {1} using {2} reference assemblies:", sourceContent.Length, outputAssemblyName, referenceAssemblies.Length); foreach (var refAsm in referenceAssemblies) { this.Logger.WriteLine(LogLevel.Trace, " {0}", refAsm); } #if WIN8 var compileAssemblyRequest = new CompileAssemblyRequest { outputAssemblyName = outputAssemblyName, projectFileExtension = this.ProjectFileExtension, projectType = this.RemoteCompilerType, referenceAssemblies = new System.Collections.ObjectModel.ObservableCollection<string>(referenceAssemblies), sourceFileExtension = this.FileExtension, sources = new System.Collections.ObjectModel.ObservableCollection<string>(sourceContent), }; var compileAssemblyTask = client.CompileAssemblyAsync(compileAssemblyRequest); compileAssemblyTask.Wait(); var compileAssemblyResponse = compileAssemblyTask.Result; byte[] assemblyBytes = compileAssemblyResponse.CompileAssemblyResult; if (assemblyBytes == null) { callback(null, new TaupoInvalidOperationException("Error compiling assembly: " + compileAssemblyResponse.errorLog)); return; } Assembly clientTypesAssembly = this.LoadDynamicAssembly(assemblyBytes); callback(clientTypesAssembly, null); #else client.BeginCompileAssembly( this.RemoteCompilerType, this.FileExtension, this.ProjectFileExtension, outputAssemblyName, sourceContent, referenceAssemblies, result => { try { string errorLog; byte[] assemblyBytes = client.EndCompileAssembly(out errorLog, result); if (assemblyBytes == null) { callback(null, new TaupoInvalidOperationException("Error compiling assembly: " + errorLog)); return; } #if SILVERLIGHT System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { Assembly clientTypesAssembly = this.LoadDynamicAssembly(assemblyBytes); callback(clientTypesAssembly, null); }); #else string cachePath = Path.Combine(this.TempAssemblyDirectory, outputAssemblyName + ".dll"); this.EnsureTempDirectoriesExist(); File.WriteAllBytes(cachePath, assemblyBytes); var assembly = AssemblyHelpers.LoadAssembly(cachePath, this.TempAssemblyDirectory); callback(assembly, null); #endif } catch (Exception ex) { callback(null, ex); } }, null); #endif } catch (Exception ex) { callback(null, ex); } #endif }