Esempio n. 1
0
        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);
            }
        }
Esempio n. 2
0
        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);
            }
        }
		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 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;
		}