How to Consume a Restful Service in .NET? [closed]
Asked Answered
C

7

30

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

Candida answered 16/1, 2012 at 17:14 Comment(0)
A
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.

Anthonyanthophore answered 16/1, 2012 at 18:30 Comment(5)
Your blog link is dead, can you fix it?Nomenclator
@Nomenclator Sorry about that.Anthonyanthophore
You'll want to see this package too: nuget.org/packages/Microsoft.Net.HttpUnicameral
Mentioned Nuget package is deprecated. It is better to add a link to this one: nuget.org/packages/Microsoft.Net.HttpImprest
@AndriyBuday Updated. Thanks.Anthonyanthophore
O
14

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

Ovid answered 16/1, 2012 at 17:22 Comment(2)
How did this work out? Is restsharp a good choice? It looks super convenient and easy to use.Ertha
Yes, I really like using it.Ovid
P
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

Precept answered 16/1, 2012 at 17:19 Comment(0)
S
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.

Silverweed answered 16/1, 2012 at 17:23 Comment(2)
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.Freedafreedman
@Freedafreedman 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.Silverweed
I
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.

Imprest answered 17/9, 2015 at 11:8 Comment(0)
S
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& [email protected]",ParameterType.RequestBody);

IRestResponse response = client.Execute(request);
Schiro answered 25/5, 2016 at 13:20 Comment(0)
T
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.

Twopenny answered 14/8, 2016 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.