简体   繁体   中英

Silverlight and Duplex WCF Service

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

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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM