为什么 UdpClient 实例化会导致 ConfigurationException?

发布于 2024-12-10 19:33:03 字数 523 浏览 1 评论 0原文

我最近在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷→糖丶微凉 2024-12-17 19:33:03

它在创建 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文