Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize wcf client with particular Url Address?

Tags:

c#

.net

wcf

I remember with ASMX there was an easy solution:

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

How can achieve the same with WCF?

like image 553
dexter Avatar asked Feb 24 '23 22:02

dexter


1 Answers

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();
}
like image 169
Darin Dimitrov Avatar answered Mar 07 '23 00:03

Darin Dimitrov