how to restart asp.net application besides modifying web.config - c#

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?

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.
The documentation specifically states that UnloadAppDomain will shut the application down:
UnloadAppDomain allows programmatic shutdown of unused applications.
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).

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.
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.
Scott Guthrie's blog entry on it:
http://weblogs.asp.net/scottgu/archive/2005/10/06/426755.aspx

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.

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

You can do this by calling the HttpRuntime.ShutdownAppDomain method (you will need to use reflection to invoke it, since it is a private static method)
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

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.
the method is mentioned at
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);
}
}
}

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

Related

How do I communicate from one c# project to another c# project

From my first project, I check a checkmark, then this line of code runs:
if (checkbox1.Checked == true)
{
File.WriteAllText("C:\\True.txt", "fshfgyusfusd");
}
Then from my second project, when the form loads, I wanted it to sync the checkmark,
so here is my code:
if (File.Exists("C:\\True.txt"))
{
checkbox1.Checked = true;
}
Is there another easier way to do this, or this is the easier way, I basically want if the first projects checkmark is checked, then the second form loads, and only check the checkmark when the first form checkmark is checked
You do have to have some way to communicate.
If your two programs are going to be running at the same time, you can use a socket, or possibly a mutex. If they are not running at the same time, you will have to save stuff to a file, or a database. Possibly your first app could notify a third party -- most likely a webservice -- and the second app could inquire of the webservice whether it happened.
But for asynchronous communication like you've described, files are probably the easiest.
A few suggestions:
Your applications should not write to the root of your C: drive. They should probably stick to either your temporary folder, or a folder under your user profile (C:\Users\CNTowerGUN), or a folder dedicated to the program under C:\ProgramData. Each of the apps should check if the folder exists before proceeding.
It may be more useful to write the current time than to write a random string. This will assist if you are debugging. Likewise the second app may want to actually read from the file to check its contents.
The app doing the writing should be on guard for the possibility that the file cannot be written, due to insufficient permission or the file being in use.

How to avoid ASP MVC application from getting freeze and throwing exception while replacing its entire web files?

In our ASP.NET MVC application, we have a 'load' button which the user can click to update the current application to its newer version. During the loading of newer version, all the web files are removed and new files are copied back and this process will take 1-2 minutes.
Issue: Since the files are getting removed and replaced back, my application will stuck during the process and throw an exception. Once the files are replaced back, everything works fine after a simple reload.
Please give me some suggestions how we can avoid the freezing of the application during the loading process ? (like showing a model popup in between, and once the files are copied back, do a reload, etc). Any ideas/suggestions are welcome. Thanks
You need to do something like this.
ASP.NET 2.0 - How to use app_offline.htm
copy the app_offline.html to the root of your site.
copy the application out
delete the app_offline.html
this will return the offline page for all requests to your application while you are copying the files out.
Once you delete app_offline, your application will be available.

Running a C# FileSystemWatcher Service forever

I've developed a service that watches one directory and then if a file is created or changed it will spin up another small c# script that will determine what to do with that file. I need this to be constantly listening to the directory at all times(even on start up of the server) because a file could be generated in this directory at literally any time.
I've searched and tried a few things but each time. The service will run then a file will be generated...then it launches the other C# script and does what it needs to with that file and then the service stops. So next time something happens in that directory nothing happens. Is there something I'm missing? How would I go about doing this?
So, your C# service will be stopped working after the first time that it runs your C# script?
I think it should related to your service configuration.
Checkout these links to see how you should create a service through C# :
http://www.codeproject.com/Articles/3938/Creating-a-C-Service-Step-by-Step-Lesson-I
http://www.codeproject.com/Articles/106742/Creating-a-simple-Windows-Service
http://www.codeproject.com/Articles/400697/WCF-Service-Creation-With-Csharp
I hope it helps you to find your mistake.
(If there's sth else, tell in comment)

Using ClickOnce when i need to generate several large files?

I'm building a digital signage application and I want to deploy it using ClickOnce. (I feel this is the best approach.) When I start the application from Visual Studio (VS) it works great. The application downloads a lot of images from my web service and saves them to disk:
string saveDir = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
When I start my deployed application, it shows the splash screen and then disappears. The process keeps running, but the UI doesn't display. I'm wondering if my saveDir as shown above is giving me trouble?
How do I locate my installed application? (I need to make license files, etc.)
I'm not sure if this is the root of your problem, but I highly recommend you change the structure of how you store your application information.
When an application is installed through ClickOnce, the application is installed within the User's folder, and it's considerably obfuscated. Furthermore, locations may change with subsequent application updates, so you can not be guarantee than any cached, downloaded file will exist from update to update.
To solve this problem, ClickOnce does provide a Data directory, that is not obfuscated and can be used for caching local data. The only caveat is this directory is not available for non-ClickOnce instances of your application (like the version that is running within the VS debugger.)
To get around this, you should write a function that you can use to get your data directory, regardless of your method of distribution or execution. The following code is a sample of what the function should look like:
//This reference is necessary if you want to discover information regarding
// the current instance of a deployed application.
using System.Deployment.Application;
//Method to obtain your applications data directory
public static string GetAppDataDirectory()
{
//The static, IsNetworkDeployed property let's you know if
// an application has been deployed via ClickOnce.
if (ApplicationDeployment.IsNetworkDeployed)
//In case of a ClickOnce install, return the deployed apps data directory
// (This is located within the User's folder, but differs between
// versions of Windows.)
return ApplicationDeployment.CurrentDeployment.DataDirectory;
//Otherwise, return another location. (Application.StartupPath works well with debugging.)
else return Application.StartupPath;
}

Prevent ASP.Net AppDomain Unload

As described here, I'm writing a WinForms GUI that is run in an ASP.Net AppDomain.
However, whenever Web.config or the bin folder is modified, ASP.Net unloads the AppDomain, and the entire program dies.
Is there any way to prevent this?
2nd EDIT: In my EXE, I create the AppDomain by calling ApplicationHost.CreateApplicationHost and pass it a type in my EXE that launches the GUI.
EDIT: I'm already aware that this is a horrible design.
Does anyone have a sane alternatives?
The program tracks accounts for a non-profit organization in a typed dataset.
It needs to send bills by email, and I'm using ASPX files to generate the emails. (I'd rather not change that, unless there's a very nice alternative; the templates have already been written)
The email templates are ASPX files that are deployed in a subfolder; that subfolder becomes the ASP.Net application and has the executable in its bin directory for ASP.Net to load all of my code into its AppDomain.
The typed dataset must be accessed by both the UI and the ASPX files, and I don't want to download the data from SQL server twice
This is core to ASP.Net - if the web.config is changed, the AppDomain is recycled. If the machine.config is changed, all AppDomains are recycled.
However, you can disable this. Turn on "Disable Recycling on Configuration Changes" for the application pool you are interested in in the IIS control panel.
alt text http://img138.imageshack.us/img138/9938/iisdisablerecycle.png
See this question: How to prevent an ASP.NET application restarting when the web.config is modified?
I've noticed that your first question is dated Oct 29. I know it's far off on the development track...
But just out of curiosity: Why not use T4 templates?
It's simple, fast, you can edit pretty much like an ASPX page, and it runs in whatever AppDomain you are.

Categories