30

What are my options to consume a RESTful service using the .Net framework? When is WCF(using the WebChannelFactory) more preferable to HttpClient?

user989046
  • 571
  • 2
  • 7
  • 11

7 Answers7

16

Microsoft`s newest HTTP library is here https://www.nuget.org/packages/Microsoft.Net.Http and I have a blog post showing how to use it here.

You would never want to use WebChannelFactory against a RESTful service. The coupling generated by WebChannelFactory defeats the point of REST.

Darrel Miller
  • 129,370
  • 30
  • 183
  • 235
14

Check out restsharp. I haven't used it, but am looking into it for consuming our own REST services.

Rex Morgan
  • 2,890
  • 2
  • 19
  • 32
3

The hammock project makes it very easy to consume RESTful services, you can use it to easily create the required http requests you need:

https://github.com/danielcrenna/hammock

Bassam Mehanni
  • 14,186
  • 2
  • 30
  • 41
2

There are few different ways of consuming REST services in .NET:

I've wrote a blog post that demonstrates first three options.

As of consuming through WCF or HttpClient I think it makes sense to read this SO question to understand the potential of REST services. When you consume a REST service via WCF you cannot use all that power.

Community
  • 1
  • 1
Andriy Buday
  • 1,889
  • 1
  • 17
  • 40
2

I think WCF is preferable whenever you want the abstraction it provides.

WCF provides an abstraction over the specific messaging and communication protocols being employed. Even only considering a RESTful scenario, you can more easily adapt to different message formats (XML, JSON, HTML).

WCF also provides configuration mechanisms, extensibility points, and instrumentation.

Ethan Cabiac
  • 4,843
  • 18
  • 36
  • For anyone reading this now: WCF is essentially dead. Should you feel like looking into it, know that's it's a complete departure from REST, and should only be used if you really know you need it. – TheMonarch Jul 13 '17 at 16:15
  • @TheMonarch you seem to be under the impression that WCF is tied to a specific set of protocols. Judging by your comment you most likely associate WCF with various SOA technologies like XML and SOAP. WCF is in fact a communication framework designed to be extended for different protocols. It is true that most of the out-of-the-box extensions are related to SOA (a consequence of the time when WCF was released) but extensions can be written/exist for REST (HTTP / JSON), protocol buffers, gRPC, or even a custom protocol. – Ethan Cabiac Jul 17 '17 at 20:20
0

This is one technique of calling or consuming rest webservice in asp.net c#

var client  = new RestClient("url"); 
var request = new RestRequest(Method.POST);

request.AddHeader("content-type", "application/json");
request.AddParameter("application/x-www-form-urlencoded",
    "type=password& user_id=test@gmail.com",ParameterType.RequestBody);

IRestResponse response = client.Execute(request);
ΩmegaMan
  • 22,885
  • 8
  • 76
  • 94
0

I just released a REST client here today. You can download the Git repo to see the samples. https://bitbucket.org/MelbourneDeveloper/restclient-.net

  • Open Source. (MIT License)
  • Markup language agnostic. (Supports JSON, SOAP and other markup languages)
  • Use strong types with REST.
  • Supports Android, iOS, Windows 10, Windows 10 Phone, Silverlight, .NET, .NET Core.
  • Incredibly simple.
  • Async friendly (uses async, await keywords).

When is WCF(using the WebChannelFactory) more preferable to HttpClient?

That is a very loaded question. WCF is a very large collection of technologies that allow you to communicate with a number of different protocols, authentication methods, and so on. It is very configurable, but REST is simple and supported by nearly all technologies available. If you write a REST service, chances are that nearly any app could consume it. Really, the question is about who your target audience is.

Christian Findlay
  • 4,791
  • 1
  • 33
  • 74