WCF 客户端配置:如何检查端点是否在配置文件中,如果没有则回退到代码?
希望创建一个通过 WCF 将序列化消息对象发送回服务器的客户端。
为了让最终开发人员(不同部门)变得容易,最好他们不需要知道如何编辑配置文件来设置客户端端点数据。
也就是说,端点也没有嵌入/硬编码到客户端中,这也是很棒的。
在我看来,混合场景是最容易推出的解决方案:
如果(在配置中描述)使用配置文件,否则回退到硬编码端点。
我发现的是:
- 如果没有找到配置文件定义,
new Client();
就会失败。 new Client(binding,endpoint);
因此有效:
Client client;
try {
client = new Client();
}catch {
//Guess not defined in config file...
//fall back to hard coded solution:
client(binding, endpoint)
}
但是有没有办法检查(除了 try/catch)以查看配置文件是否声明了端点?
如果在配置文件中定义但配置不正确,上面的操作不会失败吗? 区分这两种情况会很好吗?
Looking to make a Client that sends serialized Message objects back to a server via WCF.
To make things easy for the end-developer (different departments) would be best that they didn't need to know how to edit their config file to set up the client end point data.
That said, would also be brilliant that the endpoint wasn't embedded/hard-coded into the Client either.
A mix scenario would appear to me to be the easiest solution to roll out:
IF (described in config) USE config file ELSE fallback to hard-coded endpoint.
What I've found out is:
new Client();
fails if no config file definition found.new Client(binding,endpoint);
works
therefore:
Client client;
try {
client = new Client();
}catch {
//Guess not defined in config file...
//fall back to hard coded solution:
client(binding, endpoint)
}
But is there any way to check (other than try/catch) to see if config file has an endpoint declared?
Would the above not fail as well if defined in config file, but not configured right? Would be good to distinguish between the two conditions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想提出 AlexDrenea 解决方案的改进版本,该解决方案仅对配置部分使用特殊类型。
I would like to propose improved version of AlexDrenea solution, that uses only special types for configuration sections.
以下是读取配置文件并将数据加载到易于管理的对象中的方法:
“cs”对象将公开一个名为“endpoints”的集合,该集合允许您访问在配置文件中找到的所有属性。
确保您也处理“if”的“else”分支并将它们视为失败情况(配置无效)。
here is the way to read the configuration file and load the data into an easy to manage object:
The "cs" object will expose a collection named "endpoints" that allows you to access all the properties that you find in the config file.
Make sure you also treat the "else" branches of the "if"s and treat them as fail cases (configuration is invalid).