exam questions

Exam 70-487 All Questions

View all questions & answers for the 70-487 exam

Exam 70-487 topic 1 question 73 discussion

Actual exam question from Microsoft's 70-487
Question #: 73
Topic #: 1
[All 70-487 Questions]

You deploy a RESTful ASP.NET Web API to manage order processing.
You are developing an Azure App Services Web App to consume the API and allow customers to order products. You use the HttpClient object to process order entries. The API throws SocketException errors when the Web App experiences a high volume of concurrent users.
You need to resolve the errors.
What should you do?

  • A. Implement a Using statement block when declaring the HttpClient object.
  • B. Increase the value of the Timeout property when declaring the HttpClient object.
  • C. Use the static modifier to declare the HttpClient object.
  • D. Create a new HttpClient instance for each API request and use asynchronous method calls.
Show Suggested Answer Hide Answer
Suggested Answer: C 🗳️
If the class that wraps the external resource is shareable and thread-safe, create a shared singleton instance or a pool of reusable instances of the class.
The following example uses a static HttpClient instance, thus sharing the connection across all requests. public class SingleHttpClientInstanceController : ApiController
{
private static readonly HttpClient httpClient;
static SingleHttpClientInstanceController()
{
httpClient = new HttpClient();
}
// This method uses the shared instance of HttpClient for every call to GetProductAsync. public async Task<Product> GetProductAsync(string id)
{
var hostName = HttpContext.Current.Request.Url.Host;
var result = await httpClient.GetStringAsync(string.Format("http://{0}:8080/api/...", hostName)); return new Product { Name = result };
}
}
References:
https://docs.microsoft.com/en-us/azure/architecture/antipatterns/improper-instantiation/

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
Sose
Highly Voted 5 years, 3 months ago
C is correct. This is because if the HttpClient is disposed, new sockets will be allocated for each request, exhausting the pool of sockets if a large number of requests are run in a short period of time (throwing the described exception). Reusing the same HttpClient instance will make sure that sockets are reused whenever possible.
upvoted 9 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...