Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to consume WCF web service through URL at run time?

I want to access all the methods exposed in the service through the URL. if suppose the URL will be :

http://localhost/MyService/MyService.svc

How can I access methods:

  1. if suppose I have a ServiceReference
  2. and what should I do if don't have the Service Reference.
like image 477
Ashish Ashu Avatar asked Jul 28 '09 09:07

Ashish Ashu


2 Answers

In order to use a WCF service, you will need to create a WCF client proxy.

In Visual Studio, you would right-click on the project and pick the "Add Service Reference" from the context menu. Type in the URL you want to connect to, and if that service is running, you should get a client proxy file generated for you.

This file will typically contain a class called MyServiceClient - you can instantiate that class, and you should see all the available methods on that client class at your disposal.

If you don't want to add a service reference in Visual Studio, you can achieve the same result by executing the svcutil.exe command line tool - this will also generate all the necessary files for your client proxy class for you.

Marc

UPDATE:
if you want to initialize a client proxy at runtime, you can definitely do that - you'll need to decide which binding to use (transport protocol), and which address to connect to, and then you can do:

BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://localhost:8888/MyService");

MyServiceClient serviceClient = new MyServiceClient(binding, address);

But even in this case, you need to have imported and created the proxy client first, by using the "Add Service Reference" or svcutil.exe tools.

like image 176
marc_s Avatar answered Sep 21 '22 21:09

marc_s


To answer how to do it without having a service reference. Have a look here (option #a):

Writing your first WCF client

You still need some reference (namely a reference to an assembly containing the contract / interface) but you do not make a service reference.

EDIT: Though the above is possible I would not recommend it. Performance is not exactly great when you have to generate the proxies like this. I usually use svcutil.exe and create an assembly containing my clients and create a reference to that assembly. This way you have more options for controlling what the proxies look like.

like image 33
Stefan Egli Avatar answered Sep 21 '22 21:09

Stefan Egli