Silverlight and Duplex WCF Service

434 views Asked by At

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

1

There are 1 answers

0
Paul Sasik On

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.