title: Runtime access to web.config system.webServer/webSocket section
body: <p>OK here it goes, posting my first ever SO question after hours of googling. Be gentle please :)</p>
  
  <p><strong>Question:</strong> 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#?</p>
  
  <p><strong>Background &amp; what I've tried:</strong>
  I have an ASP.NET Web Application written in C# which utilizes Microsoft SignalR for server&lt;->client function calls. SignalR uses Websocket protocol when possible and falls back to other transport methods when it's not available.</p>
  
  <p>I have this entry in my web.config, within system.webServer section:</p>
  
  <pre><code>&lt;webSocket enabled="true" pingInterval="00:00:10" /&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>So, I added code in my Global.asax to detect OS and IIS version, to know if WebSocket is supported or not. Next <strong>I want to dynamically add or remove that WebSocket configuration line on runtime</strong> (I'm ok with my appdomain rebooting upon change).
  <strong>I need to do this fully programmatically within my application, and by not needing to change anything on IIS or OS level</strong>.</p>
  
  <p>I've looked at articles such as <a href="https://blogs.msdn.microsoft.com/saurabh_singh/2007/11/24/accessing-iis-7-0-features-programmatically-from-configuration-files-c/" rel="nofollow noreferrer">this one</a> and <a href="https://www.iis.net/configreference/system.webserver/httpprotocol" rel="nofollow noreferrer">this from MS</a> and also many SO posts that come very close to my problem, like <a href="https://stackoverflow.com/questions/32626916/programmatically-edit-the-sslflags-in-the-web-config-on-an-iis-site">this here</a> and <a href="https://stackoverflow.com/questions/8587614/configurationproperty-is-inaccessible-due-to-its-protection-level">this regarding the actual error I get</a>. This is the closest I can get at the moment and I don't know how to get past the error:</p>
  
  <pre><code>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...
  </code></pre>
  
  <p>I'm also very open to any other suggestions on how to work around this.
  Meanwhile I'll keep googling...</p>
  
  <p><strong>EDIT:</strong> Forgot to mention I'm on .NET 4.5</p>