public void SaveCode(HttpApplication httpApplication) { ManagementService managementService = new ManagementService(); Project project = managementService.GetProject(); HttpRequest httpRequest = httpApplication.Request; //string serviceName = httpRequest.QueryString["service"]; //string uid = httpRequest.QueryString["uid"]; string location = httpRequest.QueryString["location"]; string templateFolder = Path.Combine("templates", location); templateFolder = HttpContext.Current.Server.MapPath(templateFolder); string serviceName = httpRequest.QueryString["service"]; TemplateInfo templateInfo = new TemplateInfo(string.Empty, templateFolder); Hashtable context = new Hashtable(); context["Project"] = project; context["Service"] = serviceName; string trace = httpRequest.QueryString["trace"]; TemplateEngineSettings settings = TemplateEngineSettings.Default; if( trace != null ) settings.Trace = true; WebTemplateGeneratorHost host = new WebTemplateGeneratorHost(context); try { // See if we're running in full trust new SecurityPermission(PermissionState.Unrestricted).Demand(); TemplateEngineProxy proxy = new TemplateEngineProxy(); proxy.Execute(templateInfo, host, settings); } catch (SecurityException) { host.Open(); host.AddFile("", "error.txt", "Security error. Code generation is not available in Medium Trust environment."); host.Close(); } byte[] buffer = host.GetBuffer(); httpApplication.Response.Clear(); httpApplication.Response.Buffer = true; httpApplication.Response.ContentType = "application/zip";//"binary/octet-stream"; httpApplication.Response.AppendHeader("Content-Length", buffer.Length.ToString()); httpApplication.Response.AppendHeader("Content-Disposition", "attachment; filename=template.zip"); httpApplication.Response.Cache.SetCacheability(HttpCacheability.NoCache); if( buffer.Length > 0 ) httpApplication.Response.OutputStream.Write( buffer, 0, buffer.Length ); try { httpApplication.Response.Flush(); } catch (SecurityException) { } }
public void Execute(TemplateInfo templateInfo, ITemplateGeneratorHost host, TemplateEngineSettings templateEngineSettings) { _templateEngineSettings = templateEngineSettings; string relativePath = ""; try { host.Open(); ProcessTemplates(relativePath, templateInfo.Directory, host); } finally { host.Close(); } }
public TemplateInfo[] ReadTemplates() { ArrayList result = new ArrayList(); if(Directory.Exists(_templateFolder)) { foreach(string dir in Directory.GetDirectories(_templateFolder)) { string templateFile = Path.Combine(dir, "template.xml"); if( File.Exists(templateFile ) ) { XmlTextReader reader = new XmlTextReader(templateFile); try { reader.Read(); reader.ReadStartElement("Template"); reader.ReadStartElement("TemplateData"); reader.ReadStartElement("Name"); string name = reader.ReadString(); DirectoryInfo directoryInfo = new DirectoryInfo(dir); TemplateInfo templateInfo = new TemplateInfo(name, directoryInfo.Name ); result.Add(templateInfo); } finally { reader.Close(); } } } } return (TemplateInfo[])result.ToArray(typeof(TemplateInfo)); }
public void Execute(TemplateInfo templateInfo, ITemplateGeneratorHost host) { Execute(templateInfo, host, TemplateEngineSettings.Default); }
public string Preview(TemplateInfo templateInfo, ITemplateGeneratorHost host, TemplateEngineSettings templateEngineSettings) { ValidationUtils.ArgumentNotNull(templateInfo, "templateInfo"); ValidationUtils.ArgumentNotNull(host, "host"); ValidationUtils.ArgumentNotNull(templateEngineSettings, "templateEngineSettings"); log.Debug(string.Format("Running template {0} from {1}", templateInfo.Name, templateInfo.Directory)); string result; try { _templateEngineSettings = templateEngineSettings; string relativePath = ""; string templateFile = Path.Combine(templateInfo.Directory, "preview.template"); TemplateContext templateContext = new TemplateContext(relativePath, templateInfo.Directory, host); PushContext(templateContext); result = InternalProcessTemplate(relativePath, templateFile, "preview", host); PopContext(); } catch(Exception ex) { log.Error("Failed generating template preview", ex); throw ex; } //log.Debug(result); return result; }
public void Execute(TemplateInfo templateInfo, ITemplateGeneratorHost host, TemplateEngineSettings settings) { AppDomain temporaryDomain = CreateTempAppDomain(); try { string baseDirectory = temporaryDomain.BaseDirectory; string assembly = Assembly.GetExecutingAssembly().GetName().FullName; string type = this.GetType().FullName; TemplateEngineProxy proxy = (TemplateEngineProxy)temporaryDomain.CreateInstanceAndUnwrap(assembly, type); proxy._Execute(templateInfo, host, settings); } finally { AppDomain.Unload(temporaryDomain); } }
internal string _Preview(TemplateInfo templateInfo, ITemplateGeneratorHost host, TemplateEngineSettings settings) { StartLogging(); ILog log = LogManager.GetLogger(typeof(TemplateEngineProxy)); log.Debug(string.Format("Entering AppDomain: {0} {1}", AppDomain.CurrentDomain.FriendlyName, AppDomain.CurrentDomain.BaseDirectory)); TemplateEngine templateEngine = new TemplateEngine(); ValidationUtils.ObjectNotNull(templateEngine, "templateEngine"); return templateEngine.Preview(templateInfo, host, settings); }
public string Preview(TemplateInfo templateInfo, ITemplateGeneratorHost host, TemplateEngineSettings settings) { AppDomain temporaryDomain = CreateTempAppDomain(); try { string assembly = Assembly.GetExecutingAssembly().GetName().FullName; string type = this.GetType().FullName; log.Debug(string.Format("CreateInstanceAndUnwrap {0} {1}", assembly, type)); TemplateEngineProxy proxy = (TemplateEngineProxy)temporaryDomain.CreateInstanceAndUnwrap(assembly, type); if( proxy == null ) { string msg = string.Format("Failed to locate the requested type {0}", type); log.Debug(msg); throw new ServiceBrowserException(msg); } else { string result = proxy._Preview(templateInfo, host, settings); //log.Debug(result); return result; } } finally { AppDomain.Unload(temporaryDomain); } }
internal void _Execute(TemplateInfo templateInfo, ITemplateGeneratorHost host, TemplateEngineSettings settings) { StartLogging(); ILog log = LogManager.GetLogger(typeof(TemplateEngineProxy)); log.Debug(string.Format("Entering AppDomain: {0} {1}", AppDomain.CurrentDomain.FriendlyName, AppDomain.CurrentDomain.BaseDirectory)); TemplateEngine templateEngine = new TemplateEngine(); templateEngine.Execute(templateInfo, host, settings); }
public static ASObject GetCodePreview(string location, string serviceName, string methodName) { ManagementService managementService = new ManagementService(); Project project = managementService.GetProject(); string templateFolder = Path.Combine("templates", location); templateFolder = HttpContext.Current.Server.MapPath(templateFolder); TemplateInfo templateInfo = new TemplateInfo(string.Empty, templateFolder); Hashtable context = new Hashtable(); context["Project"] = project; context["Service"] = serviceName; context["Method"] = methodName; string preview; try { // See if we're running in full trust new SecurityPermission(PermissionState.Unrestricted).Demand(); WebTemplateGeneratorHost host = new WebTemplateGeneratorHost(context); TemplateEngineProxy proxy = new TemplateEngineProxy(); preview = proxy.Preview(templateInfo, host, TemplateEngineSettings.Default); } catch (SecurityException) { preview = "Security error. Code generation is not available in Medium Trust environment."; } ASObject asoCodePreview = new ASObject(); asoCodePreview["code"] = preview; return asoCodePreview; }