views:

3517

answers:

4

Hi,

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.
+2  A: 

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 MyService**Client** - 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.

marc_s
Hi Marc,Thanks for you answer. I am doing the same and able to access . But my requirement is to provide the same interface (as we get when we right click to add the service reference) to client in which he can see the urls and service reference will be updated accordingly with url selected.
Ashish Ashu
How can I acheieve this at run time. How to construct the serviceclient at runtime with the URL selected.
Ashish Ashu
OK, so you want to let your client add service references at runtime? What is he going to do with them? I mean - you can certainly create a proxy at runtime - but how will he or you be calling methods?
marc_s
One thing you can do e.g. for testing is use some tool like SoapUI (www.soapui.org) - this allows you to dynamically discover, connect to, and execute SOAP calls - but that's really not your app anymore - just a debug/test front-end for your services. Is that what you're looking for?
marc_s
Can I intialize the Serviceclient. I see it has 5 constructors taking endpoingBindingaddress and remoteaddress. can you please tell me what are they and how it can be intialized..
Ashish Ashu
As for the "what are they" (binding and endpoint addresses), I wuold suggest you read the "WCF Essentials" article by Juwal Lowy - he does a much better job explaining those things than I could - http://www.code-magazine.com/Article.aspx?quickid=0605051
marc_s
+3  A: 

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.

Stefan Egli
A: 

You can also make use of the WebClient class to call the WCF service without needing a service proxy. Effectively you can send and receive Strings and Binary data and also simulate POSTs.

I use it extensively for reusable components where the developer may not ever create the required proxy methods. A good comparison of ways to do POST is available here.

Diago
A: 

Hi Ashish,

i also have the same task to complrte .can u please give me the sample how did u completed this one

I wanted to develop a wcf service and i wanted to invoke this wcf service in client application by generating the proxy at

runtime and use this proxy to incoke the methods of the WCF service. I have an idea how to develop wcf proxy by using either

SVCUtil.exe or Addservice Option in Visual studio. But My client wants to generate this proxy at runtime only as i have the

service endpoint available to my client application .As I know the service Endpoint in my client applicatio( Like

http://localhost:8080/ HelloWcfservice/Service.svc) ,now i have to use this service url and needs to generate the proxy

class for this service and use this proxy to invoke the methods.

I got some basic info on this..is like this

As i have this Service URL ,I will get the Metadata of that service at runtime. and then from this Metadata i will generate a

proxy and invoke the Methods of the service. But i don't have any samle how i can do this .Can someone please help me with

this how i can complete this task

Thanks

samantha435