2

I am used to using soap services where you add a service reference and it creates strong types classes (return types) of each method..

Of course REST doesn't work like this

How do i consume a WCF REST Service?

Is there no way to use strongly typed classes for the return types?

I am using Visual Studio 2010 specifically

Any ideas or thoughts really appreciated

Thanks in advance

mark smith
  • 20,637
  • 47
  • 135
  • 187
  • NO, not really - how would you create strongly typed classes, when there's really no machine readable service definition (like the WSDL/XSD with SOAP).... that's one of the big disadvantages in my opinion of REST - you just get back a blob of angle-bracket soup, and you have to hope there's some useful documentation describing that somewhere.... – marc_s Aug 07 '10 at 17:23
  • @marc_s Actually services that purport to be RESTful and deliver a "blob of angle-bracket soup" have completely missed the point of REST's self-description constraint and the importance of precise media-types. – Darrel Miller Aug 07 '10 at 20:15
  • Darrel, i have it working now.. using ReadAsDataContract on the response object.. – mark smith Aug 07 '10 at 20:24

2 Answers2

1

See Is there a WCF Rest C# Client Generation Tool?. If you own both the service and the client, you can reuse the contracts and instantiate an client channel with WebChannelFactory.

Community
  • 1
  • 1
larsw
  • 3,790
  • 2
  • 25
  • 37
  • If you are going to use .net contracts and (de)serialize to .net types on both ends of the wire, why would you bother with the REST bindings? Wouldn't the wshttpbinding be a much better choice? – Darrel Miller Aug 07 '10 at 20:13
  • My whole point is to get away from SOAP ... I don't use half the stuff it provides so whats the point... I only wanted to be able to return my data into objects rather than going through XML and with the method ReadAsDataContract .. this is now sorted – mark smith Aug 07 '10 at 20:25
  • @mark Ok, so you are free to call me pedantic, but what you are trying to do is not REST. I'm not saying it is not a valid a approach, just be aware that when someone says "you can't do that it's not RESTful", you are free to ignore them :-) – Darrel Miller Aug 07 '10 at 20:37
  • Ok point taken. I of course wish to hear your comments. Thanks again. I will investigate further.. I understand the way i am doing things is probably not PURE REST but is supported and recommended via WCF (using REST)... that is why the method ReadAsDataContract exists... nothing special happening as far as i can see... Only that the methods takes the XMl from the REST Service and populates some .net classes... no magic really... Again thanks for your comments... much appreciated – mark smith Aug 07 '10 at 21:04
0
        // It is used to call the webservice url
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://careernet.localhost/rep-details/report_details/retrieves");
        request.Method = "POST";
        request.ContentType = "application/json";
        request.Accept = "application/json";

        try
        {
            WebResponse response = request.GetResponse();

            Stream responseStream = response.GetResponseStream();

           // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(responseStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // ...

I hope it will work for you.

Koshinae
  • 2,240
  • 2
  • 30
  • 40
Ranjeet SIngh
  • 673
  • 1
  • 9
  • 23