C# 除了修改web.config,如何重启asp.net应用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/407713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:12:01  来源:igfitidea点击:

how to restart asp.net application besides modifying web.config

c#asp.netrestart

提问by MikeJ

Is there a recommended way to bounce an asp.net application besides touching web.config from inside the application? is HttpRuntime.UnloadAppDomain(); the preferred way to do this ? and if so where do you do this? In the unload of a page or some other place in the application?

除了从应用程序内部触摸 web.config 之外,是否有推荐的方法来反弹 asp.net 应用程序?是HttpRuntime.UnloadAppDomain();这样做的首选方法是什么?如果是这样,你在哪里做这个?在卸载页面或应用程序的其他地方?

采纳答案by Stephen Wrighton

If this is .NET 2.0 or greater, then you can add in an "App_offline.htm" file, make a request to the server, remove it, and then make another request to the server.

如果这是 .NET 2.0 或更高版本,那么您可以添加“App_offline.htm”文件,向服务器发出请求,将其删除,然后向服务器发出另一个请求。

This sequence of events will force ASP.NET to unload the application for as long as the app_offline.htm file exists in the folder.

只要 app_offline.htm 文件存在于文件夹中,这一系列事件将强制 ASP.NET 卸载应用程序。

Scott Guthrie's blog entry on it: http://weblogs.asp.net/scottgu/archive/2005/10/06/426755.aspx

Scott Guthrie 的博客条目:http: //weblogs.asp.net/scottgu/archive/2005/10/06/426755.aspx

回答by John Sheehan

You can stop and start the Application Pool associated with the app as well.

您也可以停止和启动与应用程序关联的应用程序池。

回答by casperOne

Touching web.config from inside an application is a bad idea, IMO. Also, the idea of having a file that you modify is a little hackney, IMO.

从应用程序内部触摸 web.config 是一个坏主意,IMO。此外,拥有您修改的文件的想法有点老套,IMO。

The documentation specifically states that UnloadAppDomainwill shut the application down:

该文档特别指出UnloadAppDomain将关闭应用程序:

UnloadAppDomain allows programmatic shutdown of unused applications.

UnloadAppDomain 允许以编程方式关闭未使用的应用程序。

You should be able to make this call anywhere in the application. Mind you, you might get a SecurityException, so make sure that the runtime gives you the appropriate permissions (you might want to put this in a library and make a call and then set the library up in the GAC with evidence to give it full trust).

您应该能够在应用程序的任何位置进行此调用。请注意,您可能会得到一个SecurityException,因此请确保运行时为您提供适当的权限(您可能希望将其放入库中并进行调用,然后在 GAC 中设置库并提供充分信任的证据) .

回答by Andrew Hare

If you don't want to stop and start the app pool you can always recycle it.

如果您不想停止和启动应用程序池,您可以随时回收它。

回答by Dinis Cruz

You can do this by calling the HttpRuntime.ShutdownAppDomainmethod (you will need to use reflection to invoke it, since it is a private static method)

您可以通过调用HttpRuntime.ShutdownAppDomain方法来执行此操作(您需要使用反射来调用它,因为它是一个私有静态方法)

See How to restart an IIS Worker Process programmatically (i.e. shutdown the current ASP.NET Domain)for an example of how I use this method in a 'Restart' REST API

有关我如何在“重新启动”REST API 中使用此方法的示例,请参阅如何以编程方式重新启动 IIS 工作进程(即关闭当前的 ASP.NET 域)

回答by user2431693

You could safely restart a web application by creating or renaming a folder at run time under the application directory. Obviously you need to give the user assigned to run the application "modify" rights to the web directory or to a sub directory under it.

您可以通过在运行时在应用程序目录下创建或重命名文件夹来安全地重新启动 Web 应用程序。显然,您需要授予分配给运行该应用程序的用户对 Web 目录或其下的子目录的“修改”权限。

the method is mentioned at http://www.bartlannoeye.be/blog/restarting-a-.net-web-application-without-restarting-iis

该方法在http://www.bartlannoeye.be/blog/restarting-a-.net-web-application-without-restarting-iis 中提到

I used the following code to do it in my case. Modify it to work on a "writable" sub-directory

在我的情况下,我使用以下代码来完成它。修改它以在“可写”子目录上工作

protected void RestartButton_Click(object sender, EventArgs e)
{
    //restart web app (instead of iisreset)
    DirectoryInfo dir = new DirectoryInfo(Server.MapPath("restart"));
    if (dir.Exists)
    {
        Directory.Move(dir.FullName, dir.FullName + "ed");
    }
    else
    {
        DirectoryInfo dired = new DirectoryInfo(Server.MapPath("restarted"));
        if (dired.Exists)
        {
            Directory.Move(dired.FullName, dir.FullName);
        }
        else
        {
            Directory.CreateDirectory(dir.FullName);
        }
    }
}

回答by Fred

this code work for me. just call it to reload application.

这段代码对我有用。只需调用它即可重新加载应用程序。

System.Web.HttpRuntime.UnloadAppDomain();

Read more

阅读更多

This method will just unload our application. If you just put this method in an ASP.NET web button you are totally done. So when will our application reloaded? Actually if you click your button it will first launch our method and unload application. Further on the web page we are on at that moment will be reloaded as well, because we just clicked a button and the web page should refresh. After launching our method the page refresh process will cause our application to reload as well.

这个方法只会卸载我们的应用程序。如果您只是将此方法放在 ASP.NET Web 按钮中,那么您就大功告成了。那么我们的应用程序何时会重新加载?实际上,如果您单击按钮,它将首先启动我们的方法并卸载应用程序。进一步在我们当时所在的网页上也会重新加载,因为我们刚刚点击了一个按钮,网页应该刷新。启动我们的方法后,页面刷新过程也会导致我们的应用程序重新加载。