S. Valmont
S. Valmont

Reputation: 1031

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 knowledge of any service contract.

Seeing how this is done easily in Javascript / jQuery, it seems odd that C# presents no options.

Upvotes: 2

Views: 1811

Answers (2)

Maurice
Maurice

Reputation: 27632

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.

Upvotes: 3

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364369

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.

Upvotes: 0

Related Questions