4

I remember with ASMX there was an easy solution:

 MyAsmxServiceClient serviceClient = 
     new MyAsmxServiceClient("http://myServiceLocation/myService.asmx");

How can achieve the same with WCF?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
dexter
  • 7,063
  • 9
  • 54
  • 71

2 Answers2

4

On the same lines, binding = binding type you are using

BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://localhost:8888/MyService");        
MyServiceClient sv= new MyServiceClient(binding, address)
Kumar
  • 997
  • 5
  • 8
  • How do I initialize the binding object? – dexter Mar 02 '11 at 22:15
  • Updated above assuming you are using BasicHttpBinding. Hope it helps. – Kumar Mar 02 '11 at 22:18
  • No that did not work unfortunately, the exception was something along the lines with the client is in the faulty state. – dexter Mar 02 '11 at 22:32
  • refer this, which I came across while searching more on this- http://stackoverflow.com/questions/1193124/how-to-consume-wcf-web-service-through-url-at-run-time – Kumar Mar 02 '11 at 22:35
4

That's usually done in the app.config/web.config:

<system.serviceModel>
    <client>
        <endpoint
            address="http://myServiceLocation/myService.asmx"
            binding="basicHttpBinding"
            contract="IMyServiceContract" />
    </client>
</system.serviceModel>

or you could also do it programatically if you prefer.

Normally when you generate the client side proxy using the svcutil.exe it will also create a sample output.config file containing all you need to setup the configuration.


UPDATE:

You could also provide names to your endpoints:

<system.serviceModel>
    <client>
        <endpoint
            name="foo"
            address="http://foo.com/myService.asmx"
            binding="basicHttpBinding"
            contract="IMyServiceContract" />
        <endpoint
            name="bar"
            address="http://bar.com/myService.asmx"
            binding="basicHttpBinding"
            contract="IMyServiceContract" />
    </client>
</system.serviceModel>

and then:

using (var client = new MyClientProxy("foo"))
{
    var result = client.SomeMethod();
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Ok this looks good by what I really need to do is this: if(x) service = new Service("http://x") else service = new Service("http://y"), so is there a way to define 2 endpoints for the same contract and retrieve them by name or something? – dexter Mar 02 '11 at 22:16
  • 1
    @The Chad Who Loved Me, yes you can. I have updated my answer to include an example. – Darin Dimitrov Mar 02 '11 at 22:19