Error while updating web.config via ServerManager - Cannot write configuration file - shows wrong path of web.config in error

157 views Asked by At

As part of installer code, we are trying to make changes to IIS (check and create virtual directory, followed by adding a section to its web.config file.

Installer was working fine till recently a strange error started haunting us and blocking the installer from proceeding

Error says /Core/Service/UserService failed. Reason: Filename: \?\ c:\website1\Core\Common\Service\userService\web.config Error: cannot write configuration file

Please note that "Default Web Site/Core/Service/UserService" is a virtual directory under "Default Web Site/Core" virtual directory in IIS.

"Default Web Site/Core" has physical path of c:\website1\Core\Common whereas "Default Web Site/Core/Service/UserService" is created with physical path of "c:\website1\Core\Service\UserService"

Not sure why error is pointing towards wrong folder path for web.config.

Code we have used is like this

using (ServerManager mgr = new ServerManager())
{
  //code to add virtual path to root website (default web site)
  mgr.CommitChanges();
}
using (ServerManager mgrpolicy = new ServerManager())
{
  Configuration config = mgrpolicy.GetWebConfiguration("Default Web Site","/Core/Service/UserService");
ConfigurationSection hndlesection = config.GetSection("system.webserver/handlers");
hndlesection["accessPolicy"] = "Read,Write"; //this value is dynamic based on installer input but hardcoded here for your reference.

  mgrpolicy.CommitChanges(); //issue comes after this line
}

we are running this installer on .net 4.8 windows application where the above code runs as part of a class library pointing to namespace System.Web.Administration.

Installer user is always system administrator and thus has all privileges.

0

There are 0 answers