none
Consume REST webservice in visual Studio in C# RRS feed

  • Question

  • I have tried adding a service reference and i can connect successfully to the root url. however, after that, when i try to use a method in the webservice, i get the following error:

    Could not find default endpoint element that references contract 'RentItService.IRentItService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

    How should i consume a REST webservice? I tried to add endpoint in web.config but the errors still persist. Could you guide me on how to add a rest webservice thanks

    Thursday, March 28, 2013 2:51 PM

Answers

  • Not required to add service reference.

    //Get method
    
    WebRequest req = WebRequest.Create(@"http://localhost:61447/Products.svc/");
     
    req.Method = "GET";
     
    HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
    if (resp.StatusCode == HttpStatusCode.OK)
    {
        using (Stream respStream = resp.GetResponseStream())
        {
            StreamReader reader = new StreamReader(respStream, Encoding.UTF8);
            Console.WriteLine(reader.ReadToEnd());
        }
    }
    else
    {
        Console.WriteLine(string.Format("Status Code: {0}, Status Description: {1}", resp.StatusCode, resp.StatusDescription));
    }
    Console.Read();

    Refer this article for POST and more information


    Apriori algorithm [association rule]

    Thursday, March 28, 2013 10:32 PM
  • Hi,

    The REST service is not providing standard WSDL metadata since restful service is not SOAP oriented. But the "Add Service Reference" is rely on the WSDL(metadata) provided by the service. You can try using WebRequest class as Sukumar suggested above or using WebChannelFactory to consume the WCF REST service.

    Find more information in below blog about why Add Service Reference does not work for Rest service.

    #Mixing Add Service Reference and WCF Web HTTP (a.k.a. REST) endpoint does not work

    http://blogs.msdn.com/b/carlosfigueira/archive/2012/03/26/mixing-add-service-reference-and-wcf-web-http-a-k-a-rest-endpoint-does-not-work.aspx

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 29, 2013 6:14 AM
    Moderator

All replies

  • Not required to add service reference.

    //Get method
    
    WebRequest req = WebRequest.Create(@"http://localhost:61447/Products.svc/");
     
    req.Method = "GET";
     
    HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
    if (resp.StatusCode == HttpStatusCode.OK)
    {
        using (Stream respStream = resp.GetResponseStream())
        {
            StreamReader reader = new StreamReader(respStream, Encoding.UTF8);
            Console.WriteLine(reader.ReadToEnd());
        }
    }
    else
    {
        Console.WriteLine(string.Format("Status Code: {0}, Status Description: {1}", resp.StatusCode, resp.StatusDescription));
    }
    Console.Read();

    Refer this article for POST and more information


    Apriori algorithm [association rule]

    Thursday, March 28, 2013 10:32 PM
  • Hi,

    The REST service is not providing standard WSDL metadata since restful service is not SOAP oriented. But the "Add Service Reference" is rely on the WSDL(metadata) provided by the service. You can try using WebRequest class as Sukumar suggested above or using WebChannelFactory to consume the WCF REST service.

    Find more information in below blog about why Add Service Reference does not work for Rest service.

    #Mixing Add Service Reference and WCF Web HTTP (a.k.a. REST) endpoint does not work

    http://blogs.msdn.com/b/carlosfigueira/archive/2012/03/26/mixing-add-service-reference-and-wcf-web-http-a-k-a-rest-endpoint-does-not-work.aspx

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 29, 2013 6:14 AM
    Moderator
  • I released this open source library today:

    https://bitbucket.org/MelbourneDeveloper/restclient-.net

    It's basically a wrapper for the code posted here, but also includes some useful patterns to help with serialization and deserialization etc.

    Sunday, August 14, 2016 8:53 AM