为什么 UdpClient 实例化会导致 ConfigurationException?
我最近在 App.confg 文件中添加了一个配置部分。尽管我意识到该部分无效,但我的应用程序实际上运行了,直到它到达这一行:
this.udpClient = new UdpClient();
此时它给出了以下异常:
System.Configuration.ConfigurationErrorsException
带有消息:
"Configuration system failed to initialize"
和内部异常消息(相同类型的异常):
"Unrecognized configuration section AppDefaults. (<filename goes here>)"
为什么实例化 UdpClient 访问您的应用程序配置,为什么它会抛出配置异常,而不是像方法所述那样抛出 Socket 异常(带有内部配置异常)?
I recently added a configuration section to my App.confg file. Although I realize the section was invalid, my application actually ran, up until it hit this line:
this.udpClient = new UdpClient();
at which point it gave the following exception:
System.Configuration.ConfigurationErrorsException
with the message:
"Configuration system failed to initialize"
and inner exception message (same type of exception):
"Unrecognized configuration section AppDefaults. (<filename goes here>)"
Why does instantiating a UdpClient access your app config, and why does it throw a configuration exception, instead of a Socket Exception (with inner configuration exception), like the method states it will?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它在创建 UdpClient 对象时尝试访问配置的原因是因为 UdpClient 和 TcpClient 类只是 Socket 类的包装器。 Socket 类有一个配置部分,可以在其中存储套接字设置并从配置文件中读取套接字设置。由于配置文件存在问题并且无效,因此当它尝试在配置文件中查找该部分时,您收到了配置异常。这是有道理的,因为问题在于在创建底层套接字之前读取配置文件。
有关套接字配置部分的 MSDN 信息的链接
The reason that it was trying to access the config when creating the UdpClient object is because the UdpClient and TcpClient classes are only wrappers around the Socket classes. The Socket class has a configuration section for it where socket settings can be stored and read from the config file. Since there was a problem with the config file and it was invalid you received the configuration exception when it was trying to look for that section in the config file. It makes sense because the problem was with reading the config file before it even gets to create the underlying Socket.
Link to MSDN info on Sockets Configuration Section