locked
Stop and Start IIS programatically. Quickly and safe way. RRS feed

  • Question

  • My situation: when I deploy assemblies .NET in GAC, I get errors (Cannot access to xxx.dll because is in use for another process). The IIS use those dll (assemblies).

    Which is the best way (more performance,quick and safe way) or all ways to stop, start IIS 6.0 Windows 2003 ? (for C#, .NET 3.5)

    options, I think:

     0). First step, detect IIS installed in machine.

     1). Process.Start using commands: iisreset /stop, and iisreset /start

     2). Use ServiceController class for get  "World Wide Web Publishing Service" ( "W3SVC" )
    and do stop


        controller.Stop();
        controller.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(timeoutSeconds));


    and do start

        controller.Start();
        controller.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(timeoutSeconds));


    3). Process.Start using tasklist command: taskkill /IM aspnet_wp.exe /F (use w3wp.exe in Win2003)

    4). another options that I dont know ??


    Thanks in advanced.


    Should "Hi", "Thanks" and taglines and salutations be removed from posts? http://meta.stackoverflow.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts
    Tuesday, August 17, 2010 12:39 PM

Answers

  • I have a couple of problems with your solutions:

    1) If you are installing files into the GAC then they are strongly named and versioned.  Unless you are reinstalling the same version then you shouldn't have a conflict.  Each time you need to update your assemblies and install them the version # should be going up.  The GAC doesn't overwrite older versions with newer versions.  Hence there shouldn't be a conflict at all.  It seems like you are not updating your version # on your GACced assemblies which, IMHO, is a major no-no. 

    2) IIS can be running multiple apps so stopping them all just to update a single set of files seems like overkill.  You should instead just stop your web app and let IIS continue running the others.  In that case none of the methods you specified will work as they all work at the IIS level.

    IMO to properly stop just your app you can use System.DirectoryServices to find, stop and start your web app.  The documentation is here: http://msdn.microsoft.com/en-us/library/ms524452(v=VS.90).aspx.  More specifically you'll want this: http://msdn.microsoft.com/en-us/library/ms525303(v=VS.90).aspx  The biggest issue you'll have is knowing which app is yours and that, I think, will just have to be something that you'll have to figure out how best to solve for your case.

    Michael Taylor - 8/17/2010
    http://msmvps.com/blogs/p3net

    • Marked as answer by Bin-ze Zhao Monday, August 23, 2010 8:02 AM
    Tuesday, August 17, 2010 2:11 PM

All replies

  • I have a couple of problems with your solutions:

    1) If you are installing files into the GAC then they are strongly named and versioned.  Unless you are reinstalling the same version then you shouldn't have a conflict.  Each time you need to update your assemblies and install them the version # should be going up.  The GAC doesn't overwrite older versions with newer versions.  Hence there shouldn't be a conflict at all.  It seems like you are not updating your version # on your GACced assemblies which, IMHO, is a major no-no. 

    2) IIS can be running multiple apps so stopping them all just to update a single set of files seems like overkill.  You should instead just stop your web app and let IIS continue running the others.  In that case none of the methods you specified will work as they all work at the IIS level.

    IMO to properly stop just your app you can use System.DirectoryServices to find, stop and start your web app.  The documentation is here: http://msdn.microsoft.com/en-us/library/ms524452(v=VS.90).aspx.  More specifically you'll want this: http://msdn.microsoft.com/en-us/library/ms525303(v=VS.90).aspx  The biggest issue you'll have is knowing which app is yours and that, I think, will just have to be something that you'll have to figure out how best to solve for your case.

    Michael Taylor - 8/17/2010
    http://msmvps.com/blogs/p3net

    • Marked as answer by Bin-ze Zhao Monday, August 23, 2010 8:02 AM
    Tuesday, August 17, 2010 2:11 PM

  • Hi,

    Something like following...

    ProcessStartInfo startInfo = new ProcessStartInfo("iisreset.exe", " /stop");
    System.Diagnostics.Process.Start(startInfo);
    
    //DO SOMETHING
    //DO SOMETHING
    //DO SOMETHING
    
    ProcessStartInfo stopInfo = new ProcessStartInfo("iisreset.exe", " /start");
    System.Diagnostics.Process.Start(stopInfo);
    

    I hope this was helpful.

    Cheers,


    Mubi | www.mrmubi.com | Mark The Best Replies As Answers!
    Tuesday, August 17, 2010 2:58 PM
  • Mister Mubi, using iisreset command is better than use ServiceController for "World Wide Web Publishing Service" ( "W3SVC" ) ? thx

    Should "Hi", "Thanks" and taglines and salutations be removed from posts? http://meta.stackoverflow.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts
    Wednesday, August 18, 2010 6:26 AM
  • Mister CoolDad,

    1.) development in my company not ideal. The actual situation is reinstall the same version in GAC. And steps are:  uninstall assembly GAC, reinstall it. Problem: some assemblies in use. Solution: stop iis, reinstall in GAC, start iis.

    2.) Many applications use GAC, the best solution is stop all IIS services, I think.

    pd: how can I install IIS ADSI Provider. I use winXP in local development, and Win2003 server.

    thx


    Should "Hi", "Thanks" and taglines and salutations be removed from posts? http://meta.stackoverflow.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts
    Wednesday, August 18, 2010 6:34 AM