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);
        }
        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)
            {
            }
        }