Exemplo n.º 1
0
        /// <summary>
        /// 重启ASP.NET应用域,包括Web站点和BundleRuntime,并填写向用户展示的HTML页面。
        /// </summary>
        /// <param name="writeHtmlContent">向用户展示的HTML页面信息的delegate。</param>
        /// <example>
        /// <para>以下代码是在一个页面中重启了ASP.NET应用域。</para>
        ///
        /// <code>
        /// <![CDATA[
        /// using System;
        /// using System.Collections.Generic;
        /// using System.Web;
        /// using System.Web.UI;
        /// using System.Web.UI.WebControls;
        /// using UIShell.OSGi.Core.Service;
        /// using UIShell.OSGi.WebExtension;
        /// using System.Xml;
        /// using System.Reflection;
        ///
        /// namespace UIShell.OSGi.WebShell
        /// {
        ///     public partial class _Default : System.Web.UI.Page
        ///     {
        ///         protected void RestartAppDomain_Clicked(object sender, EventArgs e)
        ///         {
        ///             IBundleRuntimeHttpHost bundleRuntimeHttpHost = (IBundleRuntimeHttpHost)Context.ApplicationInstance;
        ///             bundleRuntimeHttpHost.RestartAppDomain(WriteMessageOnly);
        ///         }
        ///     }
        /// }
        ///
        /// private void WriteMessageOnly(StreamWriter sw)
        /// {
        ///     sw.Write("<HTML><HEAD> <meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\"> <TITLE>关闭浏览器</TITLE> </HEAD>");
        ///     sw.Write("<style>body{TEXT-ALIGN:center;} .center{MARGIN-RIGHT:auto;MARGIN-LEFT:auto;margin-top:200px;height:200px;width:400px;vertical-align:middle;line-height:40px;}</style>");
        ///     sw.Write(string.Format("<BODY><div class=\"center\"><p>操作已成功,请关闭浏览器重新访问。</p><p></p></div></BODY></HTML>"));
        /// }
        /// ]]>
        /// </code>
        /// </example>
        public static void RestartAppDomain(WriteHtmlContentAfterReboot writeHtmlContent)
        {
            FileLogUtility.Debug("Restarting the website by write bin forder or web config.");
            if (!TryWriteBinFolder() && !TryWriteWebConfig())
            {
                throw new BundleException(
                          string.Format(
                              "UIShell.OSGi needs to be restarted due to bundle uninstalling or updating, but was unable to do so.\r\nTo prevent this issue in the future, a change to the web server configuration is required:\r\n- run the application in a full trust environment, or\r\n- give the application write access to the '{0}' folder, or\r\n- give the application write access to the '{1}' file.",
                              HostRestartPhysicalPath,
                              WebConfigPhysicalPath));
            }
            HttpContext current = HttpContext.Current;

            if (current != null)
            {
                if (current.Request.RequestType == "GET")
                {
                    current.Response.Redirect(current.Request.Url.ToString(), true);
                }
                else
                {
                    string refreshHtmlPhysicalPath = RefreshHtmlPhysicalPath;
                    try
                    {
                        if (File.Exists(refreshHtmlPhysicalPath))
                        {
                            File.Delete(refreshHtmlPhysicalPath);
                        }
                        using (StreamWriter streamWriter = File.CreateText(refreshHtmlPhysicalPath))
                        {
                            if (writeHtmlContent != null)
                            {
                                writeHtmlContent(streamWriter);
                            }
                        }
                    }
                    catch
                    {
                        throw new BundleException(
                                  string.Format(
                                      "UIShell.OSGi needs to be restarted due to bundle uninstalling or updating, but was unable to do so.\r\nTo prevent this issue in the future, a change to the web server configuration is required:\r\n- give the application create/write access to the '{0}' file.",
                                      refreshHtmlPhysicalPath));
                    }
                    current.Response.WriteFile(RefreshHtmlPhysicalPath);
                    current.Response.End();
                }
                FileLogUtility.Debug("Restart website successfully.");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 重启ASP.NET应用域,包括BundleRuntime。当卸载一个模块或者更新一个模块时,需要重新启动应用域,因为旧的程序集会一直保留在BundleRuntime所在应用域直到应用域重启。
        /// </summary>
        public virtual void RestartAppDomain(WriteHtmlContentAfterReboot writeHtmlContent)
        {
            FileLogUtility.Debug("Restarting the website by write bin forder or web config.");
            bool success = TryWriteBinFolder() || TryWriteWebConfig();

            if (!success)
            {
                throw new BundleException(
                          string.Format("UIShell.OSGi needs to be restarted due to bundle uninstalling or updating, but was unable to do so.\r\n" +
                                        "To prevent this issue in the future, a change to the web server configuration is required:\r\n" +
                                        "- run the application in a full trust environment, or\r\n" +
                                        "- give the application write access to the '{0}' folder, or\r\n" +
                                        "- give the application write access to the '{1}' file.",
                                        _hostRestartPhysicalPath,
                                        _webConfigPhysicalPath));
            }

            // If setting up extensions/modules requires an AppDomain restart, it's very unlikely the
            // current request can be processed correctly.  So, we redirect to the same URL, so that the
            // new request will come to the newly started AppDomain.
            HttpContext httpContext = HttpContext.Current;

            if (httpContext != null)
            {
                // Don't redirect posts...
                if (httpContext.Request.RequestType == "GET")
                {
                    httpContext.Response.Redirect(httpContext.Request.Url.ToString(), true /*endResponse*/);
                }
                else
                {
                    string refreshHtmlFullPath = _refreshHtmlPhysicalPath;
                    try
                    {
                        // AppStore will create a refresh.html with different content.
                        if (File.Exists(refreshHtmlFullPath))
                        {
                            File.Delete(refreshHtmlFullPath);
                        }
                        using (StreamWriter sw = File.CreateText(refreshHtmlFullPath))
                        {
                            if (writeHtmlContent != null)
                            {
                                writeHtmlContent(sw);
                            }
                        }
                    }
                    catch
                    {
                        throw new BundleException(
                                  string.Format("UIShell.OSGi needs to be restarted due to bundle uninstalling or updating, but was unable to do so.\r\n" +
                                                "To prevent this issue in the future, a change to the web server configuration is required:\r\n" +
                                                "- give the application create/write access to the '{0}' file.",
                                                refreshHtmlFullPath));
                    }
                    httpContext.Response.WriteFile(_refreshHtmlPhysicalPath);
                    httpContext.Response.End();
                }

                FileLogUtility.Debug("Restart website successfully.");
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// 重启ASP.NET应用域,包括BundleRuntime。当卸载一个模块或者更新一个模块时,需要重新启动应用域,因为旧的程序集会一直保留在BundleRuntime所在应用域直到应用域重启。
 /// </summary>
 public virtual void RestartAppDomain(WriteHtmlContentAfterReboot writeHtmlContent)
 {
     BundleRuntimeHttpHostHelper.RestartAppDomain(writeHtmlContent);
 }