Question

I created these properties in my static helper class.

private static ServerManager IISServerManager
    {
        get
        {
            if (mIISServerManager== null)
            {
                mIISServerManager= new ServerManager();
            }

            return mIISServerManager;
        }
     }


private static SiteCollection Sites
    {
        get
        {
            try
            {
                return IISServerManager.Sites;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }

When I'm calling Helper method

 public static bool VirtualDirectoryExists(string dirName, string siteName)
    {
        if (!String.IsNullOrEmpty(dirName) && (Sites != null))
        {
            Site site = Sites[siteName];
            ...

in code that uses this helper (in Main thread) it retrieves all sites and their properties correctly.

On the other hand, when I'm calling it in code that uses this helper (in background worker thread) SitesCollection is retrieved, but code freezes on getting site with indexer [siteName]

Site site = Sites[siteName];

(it looks like deadlock, but there is no locking from my side)

Was it helpful?

Solution

The ServerManager class is documented as

"Any instance members are not guaranteed to be thread safe".

Which generally means you can't call them from a background thread because there's no way to do any locking to ensure IIS isn't accessing data at the same time in the main thread.

Simply get what you need while in the main thread, cache it, then use it in a background thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top