开发者

How to initialize wcf client with particular Url Address?

I remember with ASMX there was an easy solution:

 MyAsmxServiceClient serviceClient = 
     new MyAsmxService开发者_高级运维Client("http://myServiceLocation/myService.asmx");

How can achieve the same with WCF?


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();
}


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜