Programmatically Invoke WCF REST Service Without Reference to Contract
This is useful as a basis:
How to programmatically connect a client to a WCF service?
However, I'd like my client to do the same thing REST-style without know开发者_运维知识库ledge of any service contract.
Seeing how this is done easily in Javascript / jQuery, it seems odd that C# presents no options.
In C# all you need is a standard HttpWebRequest or WebClient like this:
var request = HttpWebRequest.Create("http://localhost:28330/books");
var response = request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
Console.WriteLine(reader.ReadToEnd());
or
var client = new WebClient();
Console.WriteLine(client.DownloadString("http://localhost:28330/books"));
Of course you still need to do something with the XML or JSON (or whatever data format is returned) but that is no different in JavaScript with jQuery.
Seeing how this is done easily in Javascript / jQuery, it seems odd that C# presents no options.
That is only partially true. It does - you can use HttpWebRequest
to do a call. Old REST StarterKit (only technology preview) and a new Web-API (only CTP) offers better support in HttpClient
class.
精彩评论