Get data from REST API using .NET core - c#

I am trying to get data from REST API using HttpClient, but I have an issue.
Using the same service but from Console Application, everything works fine.
From Controller everything works fine, but when GetAsync(url) method from HttpHandler is calling, it looks like something works in the background but nothing happen..
This is my service:
public class UserService : IUsersService
{
private const string url = "https://jsonplaceholder.typicode.com/users";
private IHttpHandler httpHandler;
public UserService(IHttpHandler httpHandler)
{
this.httpHandler = httpHandler;
}
public List<User> GetAllUsers()
{
HttpResponseMessage response = httpHandler.Get(url);
if (response.IsSuccessStatusCode)
{
return response.Content.ReadAsAsync<List<User>>().Result;
}
//Nice to add Logging system that we cannot connect into following URL
return new List<User>();
}
public User GetUserById(int userId)
{
HttpResponseMessage response = httpHandler.Get(
string.Concat(url,"?id=",userId));
if (response.IsSuccessStatusCode)
{
return response.Content.ReadAsAsync<List<User>>().Result.FirstOrDefault();
}
//Nice to add Logging system that we cannot connect into following URL
return null;
}
}
This is my Controller (using WEB API controller, httpClient is not getting data from REST API)
public class UsersController : ApiController
{
IUsersService userService;
public UsersController(IUsersService userService)
{
this.userService = userService;
}
public List<User> GetUsers()
{
return userService.GetAllUsers();
}
public User GetUser(int userId)
{
return userService.GetUserById(userId);
}
}
And this is my HttpHandler which is currently using HttpClient:
public class HttpHandler : IHttpHandler
{
private HttpClient client = new HttpClient();
public HttpResponseMessage Get(string url)
{
return GetAsync(url).Result;
}
public HttpResponseMessage Post(string url, HttpContent content)
{
return PostAsync(url, content).Result;
}
public async Task<HttpResponseMessage> GetAsync(string url)
{
return await client.GetAsync(url);
}
public async Task<HttpResponseMessage> PostAsync(string url, HttpContent content)
{
return await client.PostAsync(url, content);
}
}
This is my console Application which is working well and shows correct result:
class Program
{
static void Main(string[] args)
{
HttpHandler handler = new HttpHandler();
UserService service = new UserService(handler);
var users = service.GetAllUsers();
Console.WriteLine(users[0].Email);
Console.ReadKey();
}
}
I don't really know, what could be a problem.

During digging network, I found the solution to my problem https://stackoverflow.com/a/10369275/5002910
In the HttpHandler class in the GetAsync method I have to return
return await client.GetAsync(url).ConfigureAwait(continueOnCapturedContext:false);

Related

Error while returning HttpResponseMessage in.NET core

I have a simple API gateway controller which returns an IActionResult. The issue is I am not able to read the body of the response.
If I comment out the using block in ExecuteResultAsync it seems to work fine but there is not content/body.
Not sure how to get this working with the httpbody being returned. RouteRequest returning HttpResponseMessage is not an option as it puts the response from the microservice as the body of the response from the Gateway.
So I need to use the HttpResponseMessageResult middleware, which works as expected for headers but not for the body.
public async Task<IActionResult> RouteRequest()
{
// Calls a method which send a request and gets a response and constructs a HttpResponseMessage
_contextAccessor.HttpContext.Response.RegisterForDispose(response);
return new HttpResponseMessageResult(response);
}
public class HttpResponseMessageResult : IActionResult
{
private readonly HttpResponseMessage _responseMessage;
public HttpResponseMessageResult(HttpResponseMessage responseMessage)
{
_responseMessage = responseMessage;
}
public async Task ExecuteResultAsync(ActionContext context)
{
context.HttpContext.Response.StatusCode = (int)_responseMessage.StatusCode;
var responseMessageHeadersArray = _responseMessage.Headers.ToArray();
for (int i = 0; i < responseMessageHeadersArray.Length; i++)
{
var header = responseMessageHeadersArray[i];
context.HttpContext.Response.Headers.TryAdd(header.Key, new StringValues(header.Value.ToArray()));
}
using (var stream = await _responseMessage.Content.ReadAsStreamAsync())
{
await stream.CopyToAsync(context.HttpContext.Response.Body);
await context.HttpContext.Response.Body.FlushAsync();
}
}
}
Try this out, based on this good answer to a similar question, I used the ObjectResult class instead of manually manipulating the streams. When I run it with response from one of our API's (JSON), I get the same amount of data in the body of objectResult when it calls ExecuteAsync as were in the initial response.
public class HttpResponseMessageResult : IActionResult
{
private readonly HttpResponseMessage _responseMessage;
public HttpResponseMessageResult(HttpResponseMessage responseMessage)
{
_responseMessage = responseMessage;
}
public async Task ExecuteResultAsync(ActionContext context)
{
var objectResult = new ObjectResult(await _responseMessage.Content.ReadAsStreamAsync())
{StatusCode = (int)_responseMessage.StatusCode};
foreach (KeyValuePair<string, IEnumerable<string>> h in _responseMessage.Headers)
{
context.HttpContext.Response.Headers.TryAdd(h.Key, string.Join("", h.Value));
}
await objectResult.ExecuteResultAsync(context);
}
}

Calling Delete Method of API Controller Does Not Work

The Get and Post methods work fine, but when I try to call the Delete endpoint, it seems like it is never executed.
UserController.cs
[HttpDelete]
[MapToApiVersion("1.0")]
public async Task<IActionResult> Delete([FromForm] string userName)
{
return await RemoveUser(userName);
}
I am using the HttpClientto perform the request as follows:
using (Client = new HttpClient())
{
Client.BaseAddress = new Uri("https://localhost:44332/");
var result = await Client.DeleteAsync(new Uri($"/api/v{Version}/User" +"/xxx"));
return result.ToString();
}
I have created a console application to test the API:
Program.cs
public class Program
{
private static readonly HttpClient Client = new HttpClient { BaseAddress = new Uri("https://localhost:44332/") };
public static void Main(string[] args)
{
Task.Run(() => RunAsync(args));
Console.ReadLine();
}
private static async Task RunAsync(IReadOnlyList<string> args)
{
var result = await Client.DeleteAsync(new Uri($"/api/v1/user/gareth"));
Console.WriteLine(result.ToString());
}
}
When I call the same endpoint using Postman it works, what am I doing wrong?
You are trying to parse the username from the request body ([FromBody]), but you are not providing any payload to the HTTP client, instead you are specifying the parameter within the URL. Therefore, your API method should look something like this:
UserController.cs
[HttpDelete("{userName}")]
public async Task<IActionResult> Delete(string userName)
{
return await RemoveUser(userName);
}
The code below will issue a DELETE request against the UserController and pass john-doe as the userName parameter.
Program.cs
private static void Main(string[] args)
{
var httpClient = new HttpClient { BaseAddress = new Uri("https://localhost:44332") };
httpClient.DeleteAsync(new Uri("/api/v1/user/john-doe", UriKind.Relative)).Wait();
}

Can a static helper class be used for WebApi calls (from UI)?

I have X controllers that use a API site (WebApi). I have created an ApiHelper class. Which I use in these controllers. Now my question is this. Can I make this ApiHelper a static class? I think I can because the httpClient is instanced. Or do I overlook something, and does it need to be an instanced ApiHelper. (the use of static still confuses me sometimes). Example code below.
public class HomeController : Controller
{
public async Task<string> VersionDemo()
{
var response = await ApiHelper.Call("/api/config/version");
var data = response.Content.ReadAsStringAsync();
var res = Newtonsoft.Json.JsonConvert.DeserializeObject<string>(data.Result);
return res;
}
}
public class ConfigController : Controller
{
private async Task<List<ConfigSetting>> GetGeneralConfigurationDemo()
{
var generalConfiguration = new List<ConfigSetting>();
var response = await ApiHelper.Call("api/configuration/GetGeneralConfiguration");
var data = response.Content.ReadAsStringAsync();
generalConfiguration = JsonConvert.DeserializeObject<List<ConfigSetting>>(data.Result);
return generalConfiguration;
}
}
public static class ApiHelper
{
public static async Task<HttpResponseMessage> Call(string url)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var baseAdress = System.Configuration.ConfigurationManager.AppSettings["ApiBaseAddress"];
string apiUrl = baseAdress + url;
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(apiUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(apiUrl);
return response;
}
}
}
Make base controller and hide http client as protected thing.
public abstract class BaseController : Controller
{
protected ApiHelper Api { get; set; }
}
Then derive your controllers from BaseController
public class ConfigController : BaseController {}
public class HomeController : BaseController {}
Note : try not to use static classes cause they make your heap littered. They are allocated in "high-frequency" heap, which is never garbage collected.
There would be no problem to leave your class static as the HttpClient stays on the method scope and thus each call to your static method will use a different HttpClient. It would not be safe if you used a static member (field or property) as it would be shared by all the callers and you would need to synchronize the access (for a multi thread usage).
After reading (httpClient your are doing it wrong , singleton pattern) and subsequently testing. I ended up using the following code. Main goal is one httpClient application wide and avoid socket exhaustion.
In my controllers where I'm in need of a httpClient I use the HttpClientSingleton.Instance see below.
And here is a BaseController you can inherit from in your controllers that are going to use your API.
public class BaseController : Controller
{
public readonly string ApiBaseAdress = System.Configuration.ConfigurationManager.AppSettings["ApiBaseAddress"];
public BaseController()
{
//Set as needed Servicepoint settings
//string SecurityProtocolTypeFromConfig = System.Configuration.ConfigurationManager.AppSettings["SecurityProtocolType"];
//SecurityProtocolType fromConfig;
//Enum.TryParse(SecurityProtocolTypeFromConfig, out fromConfig);
//ServicePointManager.SecurityProtocol = fromConfig;
//possible ServicePoint setting needed in some cases.
//ServicePointManager.Expect100Continue = false;
//ServicePointManager.MaxServicePointIdleTime = 2000;
//ServicePointManager.SetTcpKeepAlive(false, 1, 1);
}
}
And here is the HttpClientSingleton class:
public sealed class HttpClientSingleton
{
private static readonly Lazy<HttpClient> lazy = new Lazy<HttpClient>(() => new HttpClient());
public static HttpClient Instance { get { return lazy.Value; } }
private HttpClientSingleton()
{
}
}
So putting it together. Here is an example of getting some loginfo from the API.
public class MyLogController : BaseController
{
[HttpPost]
public async Task<JsonResult> log(string requestId)
{
var url = ApiBaseAdress + string.Format("/api/runs/log/{0}", requestId);
List<Log> logs = new List<Log>();
var response = await HttpClientSingleton.Instance.GetAsync(url);
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadAsStringAsync();
logs = JsonConvert.DeserializeObject<List<Log>>(result);
return Json(logs);
}
}
You can write a static helper class. If the name is ApiHelper, then add a Microsoft.AspNet.WebApi.Client reference. When your app is initialized, call the class's InitializeClient() method, and you can call the GetAsync() method if you need. The code is below:
public static class ApiHelper
{
public static HttpClient ApiClient { get; set; }
public static void InitializeClient()
{
ApiClient = new HttpClient();
ApiClient.DefaultRequestHeaders.Accept.Clear();
ApiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public static async Task<T> GetAsync<T>(string url)
{
using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(url))
{
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsAsync<T>();
return result;
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
}

Modify .NET ApiController "BadResult()" to print custom JSON

I am using the .NET pattern where my controller action has a signature as such
public async Task<IHttpActionResult> GetTimeSeries(string deviceId, string tag) { ... }
and inside the controller I want to send a custom error message adhering to the JSON API Spec
My goal is when encountering an error to use
return BadRequest(someJSONErrorObject);
or
return NotFound(someJSONErrorObject);
rather than throw an exception.
Currently if I do
return BadRequest(JsonConvert.SerializeObject(someJSONErrorObject));
the JSON I get back looks like
{
"Message": "{\"data\":null,\"errors\":[{\"detail\":\"The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.\"}],\"meta\":null}"
}
Create a custom IHttpActionResult
public class CustomResponseResult<T> : IHttpActionResult {
public CustomResponseResult(HttpStatusCode statusCode, T content, HttpRequestMessage request) {
Content = content;
Request = request;
StatusCode = statusCode;
}
public T Content { get; private set; }
public HttpRequestMessage Request { get; private set; }
public HttpStatusCode StatusCode { get; private set; }
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken) {
return Task.FromResult(Execute());
}
private HttpResponseMessage Execute() {
var response = Request.CreateResponse(StatusCode, Content);
response.RequestMessage = Request;
response.ReasonPhrase = StatusCode.ToString();
return response;
}
}
and then in order to maintain usability create extension methods
public static class ApiControllerExtensions {
public static IHttpActionResult BadRequest<T>(this ApiController controller, T content) {
return controller.CustomResponse(HttpStatusCode.BadRequest, content);
}
public static IHttpActionResult NotFound<T>(this ApiController controller, T content) {
return controller.CustomResponse(HttpStatusCode.NotFound, content);
}
public static IHttpActionResult CustomResponse<T>(this ApiController controller, HttpStatusCode statusCode, T content) {
var request = controller.Request;
var result = new CustomResponseResult<T>(statusCode, content, request);
return result;
}
}
that allowed the calls wanted
return this.BadRequest(someJSONErrorObject);
or
return this.NotFound(someJSONErrorObject);
Create custom error class where you can set the http status and error message. Create a custom exception class to throw your custome error messag. Create a Global Exception filter which will send you custom exception in json format.

How to get content through IHttpActionResult

I have a simple method which returns a IHttpActionResult:
public IHttpActionResult invokeGetAction(HttpRequestMessage _request, String
_forResource)
{
return new GetResourceActionResult(_request, _forResource);
}
The implementation of GetResourceActionResult looks as follows:
public class GetResourceActionResult : IHttpActionResult
{
private readonly HttpRequestMessage _request;
private readonly string _location;
public GetResourceActionResult(HttpRequestMessage request, string location)
{
_request = request;
_location = location;
}
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var response = _request.CreateResponse(HttpStatusCode.OK);
response.Headers.Location = new Uri(_location);
return Task.FromResult(response);
}
}
I want to call the invokeGetAction() inside a System.Web.Http.ApiControllers' Get() method to just foward the present request to another API like
[Authorize]
[Route("")]
public IHttpActionResult Get()
{
return _someService.invokeGetAction(Request, "http://mockingsvc.../api/songs");
}
And I can see the HttpStatusCode.OK so I assume I'm not failing right now. But I have no idea where and how to get the content data delivered by the invokeGetAction() - somewhere in ExecuteAsync()?
Ok yes, I forgot something... ;-) My code is now
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
return Task.FromResult(Execute(_resourceUri));
}
public HttpResponseMessage Execute(String resourceUri)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(resourceUri);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return client.GetAsync("api/songs").Result;
}
}
and it works like a charm.

Categories