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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Tuhin PaulPosted Mar 20, 2023, 12:45 AM
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.
Tuhin PaulPosted Mar 20, 2023, 12:44 AM
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 PaulPosted Mar 20, 2023, 12:44 AM
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.
Rajeesh MenothPosted Mar 17, 2023, 4:39 AM
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
Dhanapal ChandranPosted Mar 13, 2023, 5:33 PM
Use http client