The request to Azure blob storage’s slightly different for list requests. Modifica- tions to Azure blob storage helper methods to support listing blobs and contain- ers shows the updates to the helper methods to list blobs and containers.
private HttpRequestMessage CreateRequest(HttpMethod verb,
string container, string blob, long? contentLen = default(long?)) {
string path; Uri uri;
if (blob != null) Gets blob content {
path = $"{container}/{blob}"; uri = new Uri(BlobEndpoint + path); }
else if (container != null) Lists blobs in a container {
path = container;
uri = new Uri($"{BlobEndpoint}{path}?restype=container&comp=list"); }
else Lists containers {
path = "";
uri = new Uri($"{BlobEndpoint}?comp=list"); }
var rfcDate = DateTime.UtcNow.ToString("R"); var request = new HttpRequestMessage(verb, uri); if (blob != null)
Don’t write this header for list requests
request.Headers.Add("x-ms-blob-type", "BlockBlob"); request.Headers.Add("x-ms-date", rfcDate);
request.Headers.Add("x-ms-version", ServiceVersion);
var authHeader = GetAuthHeader(verb.ToString().ToUpper(), path, rfcDate, contentLen, blob == null, container == null);
request.Headers.Add("Authorization", authHeader); return request;
}
private string GetAuthHeader(string verb, string path, string rfcDate, long? contentLen, bool listBlob, bool listContainer)
{
var devStorage = BlobEndpoint.StartsWith("http://127.0.0.1:10000") ? $"/{AccountName}" : "";
var signme = $"{verb}\n\n\n" + $"{contentLen}\n" + "\n\n\n\n\n\n\n\n" +
Leave blob type out of auth header (listBlob ? "" : "x-ms-blob-type:BlockBlob\n") + $"x-ms-date:{rfcDate}\n" + $"x-ms-version:{ServiceVersion}\n" + $"/{AccountName}{devStorage}/{path}"; if (listContainer) Add querystring parameters to auth header when listing
signme += "\ncomp:list";
Listing 7.29 Modifications to Azure blob storage helper methods to support listing blobs and containers
else if (listBlob)
signme += "\ncomp:list\nrestype:container"; string signature;
using (var sha = new
HMACSHA256(System.Convert.FromBase64String(AccountKey))) {
var data = Encoding.UTF8.GetBytes(signme);
signature = System.Convert.ToBase64String(sha.ComputeHash(data)); }
return $"SharedKey {AccountName}:{signature}"; }
7.7
Deleting a blob
To round out the functionality of the Markdown service, we’ll add the ability to delete a blob from a container. A request with a DELETE verb has a similar structure as a GET request. The only real consideration’s what status code to return. Azure blob storage returns a 202 (Accepted) status code when issuing a delete blob command. This is because the blob immediately becomes unavailable but isn’t deleted until a garbage collection happens. This is inline with RFC 2616 of the HTTP specification.
A successful response SHOULD be 200 (OK) if the response includes an entity describing the status, 202 (Accepted) if the action hasn’t yet been enacted, or 204 (No Content) if the action has been enacted but the response doesn’t include an entity.
— RFC 2616 (https://tools.ietf.org/html/rfc2616#section-9.7)
For the Markdown service, the blob’s deleted. We won’t return the value of the blob in the response, and a 204 (No Content) seems more appropriate. DeleteBlob operation to delete a blob from container shows how to write the delete operation.
[HttpDelete]
public async Task<IActionResult> DeleteBlob(string container, string blob) {
var request = CreateRequest(HttpMethod.Delete,
Don’t forget to use the right verb here
container, blob);
using (var client = new HttpClient()) {
var response = await client.SendAsync(request); if (response.StatusCode==HttpStatusCode.Accepted)
If Azure returns 202, we’ll return 204 return NoContent();
else
return Content(await response.Content.ReadAsStringAsync()); }
}
83 Summary
7.8
Summary
In this chapter, we learned how to write a microservice and communicate with other HTTP services as a client. The key concepts covered were:
Use HttpClient to make requests
ASP.NET Core routes messages based on the HttpGet, HttpPost, etc. attributes ASP.NET Core automatically populates method parameters and allows access to
the raw stream from the request
Microservices control their own data store
Some important techniques to remember from this chapter:
A library called MarkdownLite’s available for quick and easy conversion of Markdown to HTML
Async programming leaves threads unblocked, which improves the perfor- mance of your application
Curl’s a powerful and simple tool for quickly testing your services
Much of modern programming involves writing and communicating with HTTP ser- vices. ASP.NET Core makes writing HTTP REST services quick and intuitive by using a convention-based approach. Methods like Content, Created, Accepted, etc. match the HTTP specifications. Routing requests to the right methods are handled via the Http* attributes and accessing parameters from the URI or querystring doesn’t require manual parsing.
Making HTTP requests from .NET Core code’s straightforward. The HttpCli- ent class offers useful helper methods. In this chapter, we used HttpClient to com- municate with Azure storage. For .NET Framework developers used to having the Azure SDK, contacting the HTTP services directly can seem daunting. But once you understand how to authenticate, it’s easy.
Now that we've an idea of how to build services, we need to find out how to add logging to those services. This way, we can see what’s going on with our services in pro- duction. In the next chapter, we’ll look at the logging mechanisms built into the .NET Standard.
.NET Core is what it sounds like. It's a subset of the .NET framework with libraries and runtimes that drasti- cally reduce its footprint, so you can write and run .NET applications more efficiently. In addition to Win- dows, .NET Core includes runtimes for Mac and Linux, making it a high-productivity cross-platform option for web, cloud, and server-based applications. It's open source and backed by Microsoft, so supported operat- ing systems, CPUs, and application scenarios will con- tinue to grow over time.
.NET Core in Action shows .NET developers how to build professional software applications with .NET Core. You'll start by getting the big picture of how to build .NET Core applications and use the tools. Then you'll learn unit testing, debugging, and logging. You'll also dis- cover simple data access and networking. The last part of the book goes into more advanced topics, like performance profiling, localization, and signing assemblies, that you need to know so you can release your library or application to the world. By the end of this book, you'll be able to convert existing .NET code to work on multiple plat- forms or start new projects with knowledge of the tools and capabilities of .NET Core.
What's inside:
Debugging .NET Core applications
Using PerfView to investigate performance issues Enabling localization in a library
Creating unit tests with XUnit
Converting existing .NET projects to Core Working with relational data stores Interacting with web services Tools for writing .NET Core apps All examples are in C#