使用 ConfigurationManager 加载 System.ServiceModel 配置部分 [英] Loading System.ServiceModel configuration section using ConfigurationManager

查看:38
本文介绍了使用 ConfigurationManager 加载 System.ServiceModel 配置部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 C# .NET 3.5 和 WCF,我试图在客户端应用程序中写出一些 WCF 配置(客户端连接到的服务器的名称).

Using C# .NET 3.5 and WCF, I'm trying to write out some of the WCF configuration in a client application (the name of the server the client is connecting to).

显而易见的方法是使用ConfigurationManager来加载配置节并写出我需要的数据.

The obvious way is to use ConfigurationManager to load the configuration section and write out the data I need.

var serviceModelSection = ConfigurationManager.GetSection("system.serviceModel");

似乎总是返回 null.

Appears to always return null.

var serviceModelSection = ConfigurationManager.GetSection("appSettings");

完美运行.

配置部分存在于 App.config 中,但由于某种原因 ConfigurationManager 拒绝加载 system.ServiceModel 部分.

The configuration section is present in the App.config but for some reason ConfigurationManager refuses to load the system.ServiceModel section.

我想避免手动加载 xxx.exe.config 文件和使用 XPath,但如果我不得不求助于那个,我会的.只是看起来有点像黑客.

I want to avoid manually loading the xxx.exe.config file and using XPath but if I have to resort to that I will. Just seems like a bit of a hack.

有什么建议吗?

推荐答案

元素用于配置部分,而不是部分.您需要使用 System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup() 获取整个组.

The <system.serviceModel> element is for a configuration section group, not a section. You'll need to use System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup() to get the whole group.

这篇关于使用 ConfigurationManager 加载 System.ServiceModel 配置部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆