ServerManager CommitChanges makes changes with a slight delay
Asked Answered
H

1

5

i have a little problem with the ServerManager class (from the Microsoft.Web.Administration assembly) and i hope some of you can help me out.

Basically I need to create a new application inside a site (using IIS 7.5) and redirect the user to the new application, inside the same event.This functionality is implemented inside a .net web app using mvc 3/c#.

Here is my code:

ServerManager iisManager = new ServerManager();
Site mySite = iisManager.Sites["mySitesName"];            
ApplicationCollection applications = mySite.Applications;

Application app = applications.Add(newapp, physicalPath);
app.ApplicationPoolName = "myAppPool";

iisManager.CommitChanges();                    
iisManager.Dispose();

return new RedirectResult("http://localhost/" + newapp);

I think the problem with this code is that ServerManager seems to submit changes with a slight delay and the redirect to the newly added application in IIS returns "HTTP Error 404.0 - Not Found" because the changes commited from ServerManager are not finished (i think). If I refresh the page after the 404, the new application loads.

Any help or idea is greatly appreciated.

Cheers!

Hynda answered 16/9, 2011 at 15:1 Comment(4)
Redirect to a placeholder page with a timer on it (or a META REFRESH)? Then redirect to the new site?Zennas
Thanks for the suggestion, this is a good workaround, but not quite what I was hoping for.I'll keep digging and return with a solution....if I find one. :)Hynda
forums.iis.net/t/1181715.aspxTanner
It never occurred to Microsoft that we need to block until the state change has occurred. Using this API will result in many race conditions, lots of Thread.Sleep and hope for the best. Quality!Omnivore
H
6

Seems that documentation about this "delay" is very hard to find...or not existent. In the end I ended up using "Thread.Sleep" like it was suggested on iis.net.

Hope this helps others in need.

Cheers!

Hynda answered 28/9, 2011 at 19:51 Comment(3)
Thread.Sleep is an 'OK' solution. However I decided to scan the servermanager and wait for the objects I just created before I attempt to use them. (In my case I was setting folder permissions for an IIS User that I had just created)Trogon
I wish there were a way to wait for the actual changes to complete. Thread.Sleep() could work most of the time and then fail once just because the server is busy and it takes longer that time, making this a fragile solution. I guess running a loop until the changes seem to be committed is probably good.Springy
I had the issue that after a site was created, serverManager.Sites[siteName].State would throw an error. I added a loop which continuously tries to access that property and breaks after a timeout or after the error goes away. For this one run, it took 14 loops or 400ms before the site was ready.Springy

© 2022 - 2024 — McMap. All rights reserved.