3

I'm calling the following code from Windows service that was written with C#:

try
{
    ServerManager m = new ServerManager();
    if(m != null)
    {
            SiteCollection sites = m.Sites;          //I get exception here
    }
}
catch (Exception ex)
{
}

I get this exception:

{"Filename: redirection.config\r\nError: Cannot read configuration file\r\n\r\n":null}

What does that mean? And is there any way to predict it in ServerManager or my m variable before it's thrown?

c00000fd
  • 18,074
  • 19
  • 132
  • 318
  • Possible duplicate of [http://stackoverflow.com/questions/5615296/cannot-read-configuration-file-due-to-insufficient-permissions][1] [1]: http://stackoverflow.com/questions/5615296/cannot-read-configuration-file-due-to-insufficient-permissions – Artru Apr 04 '13 at 04:00
  • @Artru: It is not an ASP.NET project. I'm calling it from a Windows service. – c00000fd Apr 04 '13 at 04:08

1 Answers1

9

Update: After looking at your comment now I can answer the question fully, the problem is your application is referencing the wrong Microsoft.Web.Administration.dll, seeing the error tells me you are referencing the IIS Express version and not the "full" IIS Version (7.0.0.0). So please modify your application code to add a reference to the one that is in c:\windows\system32\inetsrv\Microsoft.Web.Administration.dll instead.

This is a permissions problem.

You will need to make sure to run the Windows Service as an identity that is either a member of the Administrators group or SYSTEM. My guess is you might be running the service as Local Serivce or Network Service and those do not have permission to read the configuration files that sit in %windows%\system32\inetsrv\config.

Other Info:
Redirection.config is a file that IIS uses to determine if it should read its configuration from the normal path (%windir%\system32\inetsrv\config\applicationHost.config) or should read it from say an external UNC file share when centralized configuration is used for many servers. That is why this is one of the first files to be parsed and hence you get that access denied error for that specific file.

Regarding the predicting, the best thing to do would be to create it within a try/catch and handle that. There are many exceptions that could happen when reading configuration, such as permissions (you could predict this one by making sure you can read (say File.OpenText()) to Redirection.config, ApplicationHost.config in %windir%\system32\inetsrv\config but that is guessing and there are others such as access to encryption keys for passwords, invalid config, etc, etc.)

Carlos Aguilar Mares
  • 12,951
  • 2
  • 35
  • 35
  • Thanks for your answer. I checked and my service is installed as `process.Account = ServiceAccount.LocalSystem;` I also checked its process in Task Manager and it runs as SYSTEM as 64-bit process. I also checked the `applicationHost.config` file as you pointed it out and it is there and seems to be a regular xml file. – c00000fd Apr 04 '13 at 08:47
  • Indeed both redirection.config and applicationHost.config are simple XML files. What I would recommend at this point is to use ProcMon to see what files are causing issues and you'll probably see an issue there – Carlos Aguilar Mares Apr 04 '13 at 18:30
  • Thanks. Tried running ProcMon on my service but what exactly do I need to get from it? It outputs a ton of information.... – c00000fd Apr 04 '13 at 20:50
  • After having reviewed the output from ProcMon, for some reason it tries to CreateFile `C:\Windows\system32\config\systemprofile\Documents\IISExpress\config\redirection.config` and fails. Any idea what that means? – c00000fd Apr 04 '13 at 21:40
  • 1
    After looking at your comment now I can answer the question fully, the problem is your application is referencing the wrong Microsoft.Web.Administration.dll, seeing the error tells me you are referencing the IIS Express version (probably 7.9.0.0, or so) and not the "full" IIS Version (7.0.0.0). So please modify your application code to add a reference to the one that is in c:\windows\system32\inetsrv\Microsoft.Web.Administration.dll instead. – Carlos Aguilar Mares Apr 05 '13 at 01:39
  • Dude, thanks. That was the problem. Just from curiosity, since I'm running this service as 64-bit process do I need to use a 64-bit version of `Microsoft.Web.Administration.dll`? – c00000fd Apr 05 '13 at 02:50
  • There is only one version of Microsoft.Web.Administration.dll that is "Any CPU" so you are fine, it will run in 64 bit. – Carlos Aguilar Mares Apr 05 '13 at 06:22
  • Thanks a lot for the answer, my application was working well on my pc but don't work on servers(ex:Cannot read configuration file) which is caused by the incorrect loaded dll version of Microsoft.Web.Administration – Yucel Aug 06 '15 at 14:11
  • Setting an AppPool user with higher permissions worked for me. – Bruno L. Aug 25 '17 at 15:32