What are my options to consume a RESTful service using the .Net framework? When is WCF(using the WebChannelFactory) more preferable to HttpClient?
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.
Check out restsharp. I haven't used it, but am looking into it for consuming our own REST services.
The hammock project makes it very easy to consume RESTful services, you can use it to easily create the required http requests you need:
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.
There are few different ways of consuming REST services in .NET:
- Plain .NET HTTP request
- WCF mechanisms
- HttpClient (recommended, nuget package)
- Other libraries (RestSharp, Hammock, etc.)
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.
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);
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.
© 2022 - 2024 — McMap. All rights reserved.