How to set up a WCF client using wsDualHttpBinding in code?

谁说我不能喝 提交于 2019-12-04 03:55:11

You can easily achieve what you want. See code below :

 Uri baseAddress = new Uri("http://localhost/CommService");
 WSDualHttpBinding wsd = new WSDualHttpBinding();
 EndpointAddress ea = new EndpointAddress(baseAddress, EndpointIdentity.CreateDnsIdentity("localhost"));
 client  = new CommServiceClient(new InstanceContext(this), wsd, ea);

Let me explain a bit :

  • first we create an instance of a WSDualHttpBinding with the default settings (those are the exact ones the generated app.config has). If you want to modify any of the settings, you can modify them trough the exposed properties.
  • then we create an EndPointAddress with th desired URL and identity. No need to link it with a binding because we will link all of them in the Service Client constructor.
  • lastly we create the Service Client. One of the contructor overloads allows us to specify a Binding and an Endpoint Address.
  • in general every element available in the app.config file has an associated Class in .NET code and every attribute or child element has an associated Property in the specified class.
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!