Question

I have a web application that exposes web services using WCF and wsHttpBindings. It is possible to have the application on different machines and different urls. This would mean the WCF service location would be different for each.

I am building a Windows Service that will reference each application and perform a task. Each task needs to call a service on the web application. I understand that the bindings are all setup in the app.config, but is there a simpler way to call the service dynamically, or how would I structure the app.config?

<webApplication WebServiceUrl="http://location1.com/LunarChartRestService.svc" />
<webApplication WebServiceUrl="http://location2.com/LunarChartRestService.svc"/>
Was it helpful?

Solution

Your client's config file could 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 pick whichever location you need. There's nothing stopping you from creating multiple client proxies, either! So you can connect to multiple server endpoints using multiple client proxies, no problem.

Alternatively, you can of course also create an instance of "WsHttpBinding" and "EndpointAddress" in code, and set the necessary properties (if any), and then call the constructor for the client proxy with this ready made objects, thus overriding the whole app.config circus and creating whatever you feel is needed:

EndpointAddress epa = 
    new EndpointAddress(new Uri("http://location1.com/LunarChartRestService.svc"));
WSHttpBinding binding = new WSHttpBinding();

Marc

OTHER TIPS

From your description, it sounds as if all servers are exposing the same service contract. If so, you could very well just declare multiple endpoints in your web.config and choose one at runtime based on the endpoint name.

Of course, it may be that you prefer not to deal with that part of the WCF configuration and would rather just have a simpler list of URLs and be done with it. That's perfectly possible as well; you just need to do a little bit more work on the code side to instantiate the client side proxies / channel objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top