11 wcf wcf-client wcf-configuration
希望使客户端通过WCF将序列化的Message对象发送回服务器.
为了让最终开发人员(不同部门)更容易,他们不需要知道如何编辑他们的配置文件来设置客户端端点数据.
也就是说,端点也没有嵌入/硬编码到客户端也很棒.
在我看来,混合方案是推出的最简单的解决方案:
IF(在config中描述)USE配置文件ELSE回退到硬编码端点.
我发现的是:
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)
}
Run Code Online (Sandbox Code Playgroud)
但有没有办法检查(除了try/catch)以查看配置文件是否有一个端点声明?
如果在配置文件中定义,但上述配置是否也会失败,但配置不正确?区分这两个条件会好吗?
kla*_*har 11
我想提出改进版的AlexDrenea解决方案,它只使用特殊类型的配置部分.
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ServiceModelSectionGroup serviceModelGroup = ServiceModelSectionGroup.GetSectionGroup(configuration);
if (serviceModelGroup != null)
{
ClientSection clientSection = serviceModelGroup.Client;
//make all your tests about the correcteness of the endpoints here
}
Run Code Online (Sandbox Code Playgroud)
这是读取配置文件并将数据加载到易于管理的对象的方法:
Configuration c = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSectionGroup csg = c.GetSectionGroup("system.serviceModel");
if (csg != null)
{
ConfigurationSection css = csg.Sections["client"];
if (css != null && css is ClientSection)
{
ClientSection cs = (ClientSection)csg.Sections["client"];
//make all your tests about the correcteness of the endpoints here
}
}
Run Code Online (Sandbox Code Playgroud)
"cs"对象将公开名为"端点"的集合,该集合允许您访问在配置文件中找到的所有属性.
确保您还处理"if"的"else"分支并将其视为失败案例(配置无效).
| 归档时间: |
|
| 查看次数: |
5037 次 |
| 最近记录: |