In my existing .net core project ,i need to call API external server for particular object to fetch in table
Loading
In my existing .net core project ,i need to call API external server for particular object to fetch in table
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Dhanapal ChandranRank #795 · 2.9K reputationPosted Mar 13, 2023, 1:33 PM EDT
Use http client
Rajeesh MenothRank #118 · 18.4K reputationPosted Mar 17, 2023, 12:39 AM EDT
Hi,
As dhanapal mentioned you can go with HTTP client calls but in .Net core, you should use "IHttpClientFactory" otherwise socket exception will create on the server side.
Reference:
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-7.0
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 19, 2023, 8:44 PM EDT
Add the HttpClient package to your project using the NuGet Package Manager. You can do this by right-clicking on your project in Solution Explorer, selecting "Manage NuGet Packages," searching for System.Net.Http, and clicking "Install". Create a class that will represent the response data from the external server. This class should have properties that match the fields you want to retrieve from the external server.
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 19, 2023, 8:44 PM EDT
Use the HttpClient class to make a request to the external server API. You can do this by creating an instance of the HttpClient class and calling its GetAsync method with the URL of the API endpoint you want to retrieve data from. The GetAsync method returns an HttpResponseMessage object, which you can then use to extract the response data. Use the response data to populate the objects in your Entity Framework DbContext. You can create a new instance of your response data class and set its properties based on the data returned by the external server API. Then you can add this new object to your DbContext and call its SaveChanges method to persist the changes to the database.
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 19, 2023, 8:45 PM EDT
using System; using System.Net.Http; using System.Threading.Tasks; namespace MyApp { public class ExternalData { public int Id { get; set; } public string Name { get; set; } public int Value { get; set; } } public class MyDbContext : DbContext { public DbSet ExternalData { get; set; } // ... } public class MyService { private readonly HttpClient _httpClient; private readonly MyDbContext _dbContext; public MyService(HttpClient httpClient, MyDbContext dbContext) { _httpClient = httpClient; _dbContext = dbContext; } public async Task GetExternalData() { var response = await _httpClient.GetAsync("https://api.example.com/external-data"); if (response.IsSuccessStatusCode) { var externalData = await response.Content.ReadAsAsync(); _dbContext.ExternalData.Add(externalData); await _dbContext.SaveChangesAsync(); } else { throw new Exception("Failed to retrieve external data"); } } } }we have a MyDbContext class that represents our Entity Framework context with a DbSet property for the objects we want to populate from the external server API.