2

I have added a WCF service reference to Silverlight application and here's what the binding from web.config that I have looks like

<bindings>
  <wsDualHttpBinding>
    <binding name="wsDualHttpBinding">
      <security mode="None" />
    </binding>
  </wsDualHttpBinding>
  <pollingDuplexHttpBinding>
    <binding name="multipleMessagesPerPollPollingDuplexHttpBinding"
      duplexMode="MultipleMessagesPerPoll" />
  </pollingDuplexHttpBinding>
</bindings>

And I have this snippet to create a service client instance

var serviceClient = new DuplexCallerIdServiceClient(
         new PollingDuplexHttpBinding(PollingDuplexMode.MultipleMessagesPerPoll),
      new EndpointAddress("http://localhost:51445/Service/MyService.svc"));

My concern is that why do I have to provide an absolute url in code. I have a winforms application that uses the same service and I can just do new DuplexCallerIdServiceClient() to create a service client instance which seems ideal. Is there any way I can work around it. I cannot change the binding settings.

Thanks

Professor Chaos
  • 8,850
  • 8
  • 38
  • 54
  • 1
    The reason why you can call the service client constructor without parameters is that the address is specified in the app.config. If you don't like that the url is absolute, you can use a relative url like '../MyService.svc' – vortexwolf Oct 21 '11 at 16:33

1 Answers1

0

You do not have to hardcode the service URL. Replace the hard coded string that either is passed in as an argument or makes a function call (or gets some object's property) to populate the constructor with a valid service URL.

Here's one way among many:

var serviceClient = new DuplexCallerIdServiceClient(
     new PollingDuplexHttpBinding(PollingDuplexMode.MultipleMessagesPerPoll),
  new EndpointAddress(Info.Instance.ServiceURL));

Where Info is a singleton object, Instance gets the singleton's instance and ServiceUrl is a string property that comes from... wherever. Database, config file, hard coded to start etc...

P.S. Careful with the Singleton pattern, but as config info entities they can be very useful.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189