OK here it goes, posting my first ever SO question after hours of googling. Be gentle please :)
Question: Any ideas how to get past the error: "[System.Configuration.ConfigurationProperty]' is inaccessible due to its protection level", when attempting to modify web.config system.webServer/webSocket section in C#?
Background & what I've tried: I have an ASP.NET Web Application written in C# which utilizes Microsoft SignalR for server<->client function calls. SignalR uses Websocket protocol when possible and falls back to other transport methods when it's not available.
I have this entry in my web.config, within system.webServer section:
<webSocket enabled="true" pingInterval="00:00:10" />
That enables my application to use WebSocket so I need this line. Also, because my users are often operating in difficult network conditions, I've added a rather short pingInterval. This works great, no problem.
It's well known that Windows 2008 R2 / IIS 7.5 (and lower versions) do not support WebSocket. However sometimes I need to run my application on an older Windows Server version. In that case I need to manually remove the line shown above, to avoid nasty IIS errors about incorrect Web.config configuration. This also works just fine, but I don't like the extra work of having to remove this line depending on which server I'm running on.
So, I added code in my Global.asax to detect OS and IIS version, to know if WebSocket is supported or not. Next I want to dynamically add or remove that WebSocket configuration line on runtime (I'm ok with my appdomain rebooting upon change). I need to do this fully programmatically within my application, and by not needing to change anything on IIS or OS level.
I've looked at articles such as this one and this from MS and also many SO posts that come very close to my problem, like this here and this regarding the actual error I get. This is the closest I can get at the moment and I don't know how to get past the error:
System.Configuration.Configuration webConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MICC");
//At this point I can see that webConfig.FilePath is pointing to correct Web.config file
System.Configuration.ConfigurationElement webSocketElement = webConfig.GetSection("system.webServer/webSocket");
if (webSocketElement == null)
{
//Not sure how to initialize a new ConfigurationElement, if GetSection() returned null
}
else
{
//Attempting to change values, but following two lines gives me:
//[System.Configuration.ConfigurationProperty]' is inaccessible due to its protection level
webSocketElement["enabled"] = true;
webSocketElement["pingInterval"] = TimeSpan.Parse("00:00:99"); //Test value
}
webConfig.Save(); //Never getting this far...
I'm also very open to any other suggestions on how to work around this. Meanwhile I'll keep googling...
EDIT: Forgot to mention I'm on .NET 4.5