Define wsHttpBinding at runtime using WCF
I have a web application that provides web services using WCF and wsHttpBindings. It is possible to have the application on different machines and different URLs. This would mean that the location of the WCF service would be different for each.
I am creating a windows service that will reference each application and perform a task. Each task must call a service in the web application. I understand that the bindings are all configured in app.config, but is there an easier way to call the service dynamically or how would I structure my app.config?
<webApplication WebServiceUrl="http://location1.com/LunarChartRestService.svc" />
<webApplication WebServiceUrl="http://location2.com/LunarChartRestService.svc"/>
a source to share
Your client config file might look something like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint name="Endpoint1"
address="http://location1.com/LunarChartRestService.svc"
binding="wsHttpBinding"
contract="(whatever-your-contract-is)" />
<endpoint name="Endpoint2"
address="http://location2.com/LunarChartRestService.svc"
binding="wsHttpBinding"
contract="(whatever-your-contract-is)" />
<endpoint name="Endpoint3"
address="http://location3.com/LunarChartRestService.svc"
binding="wsHttpBinding"
contract="(whatever-your-contract-is)" />
</client>
</system.serviceModel>
</configuration>
Then in code you can create such an endpoint (client proxy) based on its name and thus you can choose any location you need. Nothing prevents you from creating multiple client proxies! This way, you can connect to multiple server endpoints using multiple client proxies without issue.
Alternatively, you can also instantiate "WsHttpBinding" and "EndpointAddress" in code and set the required properties (if any) and then call the constructor for the client proxy with these ready-made objects, thus overriding the whole app.config circus and creating everything. what you see fit:
EndpointAddress epa =
new EndpointAddress(new Uri("http://location1.com/LunarChartRestService.svc"));
WSHttpBinding binding = new WSHttpBinding();
Mark
a source to share
From your description, it sounds like all servers are subject to the same service contract. If so, you could very well just declare multiple endpoints in your web.config and select one at runtime based on the endpoint name.
Of course, perhaps you prefer not to deal with that part of the WCF configuration, but rather just have a simpler list of urls and do with it. This is perfectly possible; you just need to do a little bit of code work to instantiate client side / channel objects.
a source to share