15

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.
Ashish Ashu
  • 14,169
  • 37
  • 86
  • 117

5 Answers5

21

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 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 Jul 28 '09 at 11:29
  • How can I acheieve this at run time. How to construct the serviceclient at runtime with the URL selected. – Ashish Ashu Jul 28 '09 at 11:30
  • 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 Jul 28 '09 at 11:35
  • 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 Jul 28 '09 at 11:36
  • 1
    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 Jul 28 '09 at 11:41
  • 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 Jul 28 '09 at 20:25
  • @marc_s : If I need to create proxyObject for an ASP.NET web application, what scope the proxyObj ? should it be member variable for every page or global to all pages ? Please help. – Agent007 May 10 '13 at 13:14
8

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
  • 17,398
  • 3
  • 54
  • 75
2

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.

BinaryMisfit
  • 29,219
  • 2
  • 37
  • 44
1

You call it with a /functionname, eg:

http://localhost/MyService/MyService.svc/GetVersionNumber

enter image description here

Edit:

How do you configure your method in the WCF service so you can call it directly from the browser?

I have an Interface:

[ServiceContract]
public interface IWebServiceImpl
{
    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "GetVersionNumber")]
    string GetVersionNumber();

And a class to implement the GetVersionNumber method in the Interface:

public class WebServiceImpl
{
    public string GetVersionNumber()
    {
            return "1.0.0.0"; //In real life this isn't hard-coded
    }
}

Finally here is the Web.config configuration:

<system.serviceModel>        
    <diagnostics>
      <messageLogging logEntireMessage="true" logMalformedMessages="false" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="false" maxMessagesToLog="3000" maxSizeOfMessageToLog="2000"/>
    </diagnostics>        
    <bindings>
      <webHttpBinding>
        <binding name="webBinding">
          <security mode="Transport"/>
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="ServiceBehaviour" name="YOURWebServiceNameSpace.WebServiceImpl">            
        <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="YOURWebServiceNameSpace.IWebServiceImpl"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • I have tried your approach without luck. How do you configure your method in the wfc service so you can call it directly from the browser? – IceCode Nov 13 '18 at 10:06
  • @Örvar see edit, if that doesn't help post a new question. – Jeremy Thompson Nov 14 '18 at 00:46
  • Thanks alot for your answer, appreciate it. We have however abandoned the idea here at work using our existing wcf service. Going to setup a rest api, makes more sense for our further requirements – IceCode Nov 15 '18 at 18:24
0

You can just provide the wsdl of your service: http://localhost/MyService/MyService.svc?wsdl.

From wsdl you can generate proxy classes and use them on the client.

sandyiit
  • 1,597
  • 3
  • 17
  • 23