升级到.NET Framework 4.7.2后System.Net配置节无法识别问题咨询
解决方案
问题原因
升级到.NET Framework 4.7.2版本后,框架不再默认隐式识别system.net配置节,因此原配置中直接写的<system.net>节点会触发无法识别配置节的异常:
System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section system.net.
解决方法
方法1:在app.config中显式注册system.net配置节
在<configuration>根节点下的第一个位置添加<configSections>节点注册配置节,原system.net配置内容无需修改,完整示例如下:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <!-- 新增的配置节注册 --> <configSections> <section name="system.net" type="System.Net.Configuration.NetSectionGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> </configSections> <appSettings> <!--AuthenticationConfiguration is reading off of app.config. Once it's updated, remove these lines--> <add key="validIssuers" value="https://sts.windows.net/xxxxx/" /> <add key="tenant" value="microsoft.onmicrosoft.com" /> <add key="validAudiences" value="xxxx,xxxxx" /> <add key="enableValidateAudience" value="true" /> <add key="aadInstance" value="https://login.microsoftonline.com/{0}" /> <!-- CORS Settings--> <add key="corsAllowedDomains" value="*" /> <add key="corsAllowedHeaders" value="*" /> <add key="corsAllowedVerbs" value="*" /> </appSettings> <system.net> <connectionManagement> <add address = "*" maxconnection = "100" /> </connectionManagement> </system.net> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" /> </startup> <runtime> <gcServer enabled="true" /> </runtime> </configuration>
注意:修改完成后需要把
<startup>节点里的sku属性同步改为对应4.7.2版本,避免版本兼容问题。
方法2:代码动态设置maxconnection参数
无需修改配置文件,直接在应用程序的启动入口(如控制台项目Main方法、Web项目Global.asax的Application_Start方法)中添加如下代码即可,优先级高于配置文件设置:
// 需在所有HTTP请求发起前执行 System.Net.ServicePointManager.DefaultConnectionLimit = 100;
注意事项
- 若使用代码配置,可直接删除原配置文件中的
<system.net>节点 - 代码设置必须在应用发起任何外部HTTP请求前执行,否则已经初始化的ServicePoint不会应用新的连接数限制
内容的提问来源于stack exchange,提问作者Karthik Kandasamy
相关产品推荐
相关产品推荐

