Occasionally in the web browser or a client application I am seeing a 500 Internal Server Error that appears to come from IIS itself reading its web.config
file. The server I saw this happen on is running Windows Server 2019 with IIS 10.0.17763.1.
As far as I can tell, we never modify the config file directly, only using Microsoft.Web.Administration.ServerManager
with some services that run as their own ASP.NET process.
Can ServerManager
cause this, or would it be some other rogue process on the server locking the file occasionally?
If it does cause it, is there any way other to update an IIS rewrite rule without triggering downtime? Maybe write a new web.config
file without “locking” the existing one then swap the active config?
using (ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetWebConfiguration("Softdial");
ConfigurationSection rulesSection = config.GetSection("system.webServer/rewrite/rules");
ConfigurationElementCollection rulesCollection = rulesSection.GetCollection();
ConfigurationElement ruleElement = rulesCollection.CreateElement("rule");
ruleElement["name"] = "My Rule";
ruleElement["stopProcessing"] = "true";
ConfigurationElement matchElement = ruleElement.GetChildElement("match");
matchElement["url"] = @"example/(.*)";
ConfigurationElement actionElement = ruleElement.GetChildElement("action");
actionElement["type"] = @"Rewrite";
actionElement["url"] = "http://localhost:8999/{R:1}";
actionElement["logRewrittenUrl"] = "true";
rulesCollection.Add(ruleElement);
serverManager.CommitChanges();
}
Server Error in '/' Application.
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: An error occurred loading a configuration file: The process cannot access the file 'C:MyWebSiteweb.config' because it is being used by another process.
Source Error:
[No relevant source lines]
Source File: C:MyWebSiteweb.config Line: 0
Click here to show additional error information:
Exception Details: System.IO.IOException: The process cannot access the file 'C:MyWebSiteweb.config' because it is being used by another process.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[IOException: The process cannot access the file 'C:MyWebSiteweb.config' because it is being used by another process.]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +935
System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +1242
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) +126
System.Configuration.Internal.InternalConfigHost.StaticOpenStreamForRead(String streamName) +102
System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.OpenStreamForRead(String streamName, Boolean assertPermissions) +149
System.Configuration.BaseConfigurationRecord.InitConfigFromFile() +470
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.8.4718.0