【发布时间】:2012-05-06 08:23:18
【问题描述】:
我想覆盖存储在 app.config 中的客户端 WCF 端点地址,以便我可以将它们从指向“localhost”更改为指向生产 URL [取决于可以从应用程序中设置的配置(包含在一个对象中) 'appConfig' 在下面显示的代码中) - 这是一个 WinForms 项目。]
通过阅读该领域的其他问题,我已经了解了我从 Form_Load 事件中调用的以下代码片段(调用 InitEndpoint 的 InitAllEndpoints)。我在我的应用程序中尝试了这些,如果我将鼠标悬停在“ep”变量中的值上,它们似乎会更改端点地址。然而,如果我在我的代码之后再次循环通过 serviceModelSectionGroup.Client.Endpoints 我发现它们实际上没有改变。 (我现在读到 EndPoint 地址是不可变的 - 所以我的代码无论如何看起来都是错误的,因为我希望用新的 EndPoint 地址对象覆盖地址 - 而不是 Uri?)
如何以编程方式覆盖客户端 app.config WCF 端点地址?
private void InitAllEndpoints()
{
ServiceModelSectionGroup serviceModelSectionGroup =
ServiceModelSectionGroup.GetSectionGroup(
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None));
if (serviceModelSectionGroup != null)
{
foreach (ChannelEndpointElement ep in serviceModelSectionGroup.Client.Endpoints)
{
InitEndpoint(ep,
appConfig.ExternalComms_scheme,
appConfig.ExternalComms_host,
appConfig.ExternalComms_port);
}
}
}
private void InitEndpoint(ChannelEndpointElement endPoint, string scheme, String host, String port)
{
string portPartOfUri = String.Empty;
if (!String.IsNullOrWhiteSpace(port))
{
portPartOfUri = ":" + port;
}
string wcfBaseUri = string.Format("{0}://{1}{2}", scheme, host, portPartOfUri);
endPoint.Address = new Uri(wcfBaseUri + endPoint.Address.LocalPath);
}
注意:我的代理位于单独的项目/DLL 中。
例如
public class JournalProxy : ClientBase<IJournal>, IJournal
{
public string StoreJournal(JournalInformation journalToStore)
{
return Channel.StoreJournal(journalToStore);
}
}
【问题讨论】:
-
这可能就是你要找的...stackoverflow.com/questions/5151077/…
标签: c# winforms wcf web-services