1

I've encountered this problem earlier already. This time I was moving my development environment from one desktop to another and the issue came back. Note that to move my solution from the old environment to the new one I copied its entire solution folder. When opened in a new desktop, using the same version of the Visual Studio 2010, the following code:

using (ServerManager serverManager = new ServerManager())
{
    //Go through all the sites
    for (int s = 0; s < serverManager.Sites.Count; s++) //EXCEPTION HERE!!!
    {
        //Do work
    }
}

Produced this exception:

GetAdminSection; GetSectionInternal; SitesSectionCreator; Initialize;
SitesCollectionCreator; Initialize; collectData.
Filename: redirection.config 
Error: Cannot read configuration file

Why would it do so if it was running absolutely fine on an old desktop... Anyway, my question:

What is the proper way to include the Microsoft.Web.Administration assembly reference?

enter image description here

The way I currently did it is by linking to this dll via Solution -> References -> Add reference -> Browser and then point to this file:

C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll

My concern is that this exception will pop up on a production machine when my solution is deployed there.

Community
  • 1
  • 1
c00000fd
  • 20,994
  • 29
  • 177
  • 400

2 Answers2

1

Yes, it is correct way of referencing the Microsoft.Web.Administration assembly. It is only available for IIS 7.0 and later though. Do you have IIS features installed (go to Turn Windows features on or off wizard in your control panel).

http://www.iis.net/learn/manage/scripting/how-to-use-microsoftwebadministration

This is how I instantiate the ServerManager, had similar issues similar to yours:

ServerManager iisManager = ServerManager.OpenRemote(Environment.MachineName.ToLower());
Andrew
  • 3,648
  • 1
  • 15
  • 29
  • Yes, I have IIS 8.5 running on Windows 8.1. I migrated it from Windows 8. That is the only difference. So why would it reset itself then? – c00000fd Apr 16 '14 at 07:59
  • what identity is your code running under? I've implemented web installer application that performs multiple operations with IIS, and added manifest file to make it require administrative access `` – Andrew Apr 16 '14 at 08:03
  • It starts as a local service using this command: `"%SYSTEMROOT%\Microsoft.NET\Framework\v4.0.30319\installutil" myapp.exe` – c00000fd Apr 16 '14 at 08:08
  • Try running under NETWORK SERVICE – Andrew Apr 16 '14 at 08:24
  • I can't run it as a network service. This will open a whole new can of worms. – c00000fd Apr 16 '14 at 08:55
  • Just curious, what would that do? – c00000fd Apr 16 '14 at 08:56
  • Will do pretty much the same, but you'll have no issues with IIS 8 – Andrew Apr 16 '14 at 08:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/50763/discussion-between-andrew-and-c00000fd) – Andrew Apr 16 '14 at 09:01
0

The correct way to use this assembly is to add the reference after installing IIS. So what you have done is right.

The deployment concern is unnecessary, as you can always create a deployment package (MSI for example), so make sure that IIS is set as prerequisite and the expected assembly is present when your application finally runs. (Microsoft does register this assembly to GAC.)

About the exception you need to collect more information,

  1. At runtime which copy of Microsoft.Web.Administration is loaded? There can be multiple versions in GAC.
  2. Does the redirection.config file contain proper data? Are the file permissions correct?
  3. Does the issue remain if you run Visual Studio as administrator?
Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • Thanks. Is there any way to collect all this info at run-time when the exception happens? – c00000fd Apr 16 '14 at 08:55
  • Of course you can test your app out on a machine with IIS to see where the exception comes from. You should be able to properly handle it, as it is simply a normal exception you can try catch. – Lex Li Apr 16 '14 at 13:48
  • Well, I get that. That's how I got the exception description I posted above. What I'm asking is how do I get the info that you suggested in your post? – c00000fd Apr 17 '14 at 07:46
  • In Visual Studio you can see which modules are loaded via Debug | Modules panel. That can help you check step 1. Of course you also need to run `gacutil -l Microsoft.Web.Administration` at VS command prompt to see registered versions. – Lex Li Apr 17 '14 at 08:18
  • I'm sorry. I don't have a luxury of running VS on a client's machine. It must be collected via the C# and .Net capabilities. – c00000fd Apr 17 '14 at 08:21
  • If your app is .NET 4 and above, ask the client to capture a process hang dump using SysInternals procdump, and then you can in VS or WinDbg analyze the dump. If you want to do step 1 purely in code, you can also catch the exception and then dump out `AppDomain.CurrentDomain.GetAssemblies()`. – Lex Li Apr 17 '14 at 09:14