app.config に保存されているクライアント WCF エンドポイント アドレスをオーバーライドして、"localhost" を指すものから実稼働 URL を指すものに変更できるようにしたい [アプリ内から設定できる構成に応じて (オブジェクト 'appConfig' に含まれる)以下に示すコードで) - これは WinForms プロジェクトです。]
この分野の他の質問を読んで、Form_Load イベントから呼び出す次のコード (InitEndpoint を呼び出す InitAllEndpoints) にたどり着きました。アプリケーションでこれらを試してみましたが、「ep」変数の値にカーソルを合わせると、EndPoint アドレスが変更されているように見えます。それでも、コードの後に serviceModelSectionGroup.Client.Endpoints を再度ループすると、実際には変更されていないことがわかります。(EndPoint アドレスは不変であることを今読んだので、Uri ではなく新しい EndPoint アドレス オブジェクトでアドレスを上書きすることを期待しているので、私のコードはとにかく間違っているように見えますか?)
クライアント 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);
}
}