# Tuesday, November 20, 2018

In previous articles, I showed how to create Azure Function Apps and Azure Functions directly in the Azure Portal. You can also create Function Apps and Functions in Visual Studio and then deploy them to Azure. I prefer to do this, because it makes it easier to get my code into source control.

Before working with and creating Azure artifacts in Visual Studio, you must install the Azure tools. To install these tools, launch Visual Studio installer and check "Azure Development, as shown in Fig. 1.

Fig. 1

Once the Azure tools are installed, launch Visual Studio and select File | New | Project  from the menu, as shown in Fig. 2.

Fig. 2

In the "New Project" dialog, expand Visual C# | Cloud in the left tree and select "Azure Functions" from the list of templates; then enter a project name and location, as shown in Fig. 3.

Fig. 3

The next dialog (Fig. 4) presents a list of options for your Azure Function.

Fig. 4

In the top dropdown, select "Azure Functions v2".

Select "Http Trigger" to create a function that will be triggered by an HTTP GET or POST to a web service URL.

At the "Storage Account" dropdown, select "Storage Emulator". This works well for running and testing your function locally. You can change this to an Azure Storage Account when you deploy the Function to Azure.

At the "Access rights" dropdown, select "Function".

Click the [OK] button to create an Azure Function App with a single Azure Function.

A function is generated with the following code:

public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
    ILogger log)
    log.LogInformation("C# HTTP trigger function processed a request.");

    string name = req.Query["name"];

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    name = name ?? data?.name;

    return name != null
        ? (ActionResult)new OkObjectResult($"Hello, {name}")
        : new BadRequestObjectResult("Please pass a name on the query string or in the request body");

Listing 1

The method is decorated with the "FunctionName" attribute, which provides the name of the function.


Notice that the first parameter is decorated with

[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]

This tells the system that the Function is triggered by an HTTP request and that it will request either a GET or POST verb.

We also pass in an ILogger, so that we can output debugging information.

Let's walk through the code in this function

Log some information, so we can confirm the function was properly triggered.

log.LogInformation("C# HTTP trigger function processed a request.");

If a "name" parameter is passed in the querystring, capture the value of this parameter.

string name = req.Query["name"];

If this is a POST request, there may be information sent in the request body. Retrieve this information and convert it to a JSON object:

string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); 
dynamic data = JsonConvert.DeserializeObject(requestBody);

If the "name" parameter was passed in the querystring, use that; if not, look for it in the JSON object from the request body.

name = name ?? data?.name;

If a "name" parameter was found, return an HTTP Response Code 200 (OK) with a body containing the text "Hello, " followed by the value of the name.

If no "name" parameter was passed, return an HTTP Response Code 400 (Bad Request) with a message into the body indicating a name is required.

return name != null 
    ? (ActionResult)new OkObjectResult($"Hello, {name}") 
    : new BadRequestObjectResult("Please pass a name on the query string or in the request body");

Publish App

One quick way to publish a Function App to Azure is directly from Visual Studio. To do this, right-click the project in the Solution Explorer and select "Publish" from the context menu, as shown in Fig. 5.

Fig. 5

The "Pick a publish target" dialog displays, as shown in Fig. 6.

Fig. 6

Check the "Run from ZIP" checkbox.

Select either the "Create New" or "Select Existing" radio button, depending whether you wish to deploy to an existing or a newly-created Azure Function; then click the [Publish] button.

The follow-up dialog if you select "Create New" is shown in Fig. 7a and for "Select existing" in Fig. 7b.

Click the [OK] or [Create] button at the bottom of the follow-up dialog to deploy the Function.

This article showed how to create an Azure Function App in Visual Studio, making it easier to test locally and integrate your code with source control.

Tuesday, November 20, 2018 9:41:00 AM (GMT Standard Time, UTC+00:00)
# Monday, November 19, 2018

Episode 538

Jeff Fritz on Live Streaming Coding

Jeff Fritz uses twitch.tv to live stream while he codes with others. He talks about how, why, and when he does it.

Monday, November 19, 2018 7:16:00 AM (GMT Standard Time, UTC+00:00)
# Sunday, November 18, 2018

GANGConf (1)Sometimes you can go home again.

I was a member of the Great Lakes Area .NET User Group (a.k.a. GANG) for years and spent some time on the board, including 2 years as President. But I've had much less interaction with them since joining Microsoft and moving to Chicago in 2014.

So, I was excited when my friend Ondrej called to tell me that GANG was hosting a conference and I could speak there if I wanted. I wanted to be a part of this event, so I made the trek from back to Detroit.

The event was held at the Microsoft offices in downtown Detroit. About 70 people came to hear a presentation on both technical topics and soft skills.

GANGConf (2)Cassandra Faris opened the conference telling people how they can manage and  promote your personal brand.

J Tower was next with a presentation on how to use .NET Standard to share code among different types of applications and platforms.

I wrote a presentation about Azure Functions and delivered for the first time at this event.

Kevin Davis's presentation titled "Living your Best (Developer) Life" talked about how to choose and manage your career.

Aydin Akcasu had the best demos of the day, showing Bluetooth devices integrating  with the Chrome web browser.

Finally, Daniel Davis described the benefits of clean code and how to achieve it.

The event reminded me of a similar Saturday event I hosted to celebrate GANG'S 10-year anniversary back in 2011. This is the second year in a row, GANG has held GANGConf and president Ryan Albertson promised to do it again next year.
I hope to be there again for it.

GANGConf (3)

Sunday, November 18, 2018 8:33:00 AM (GMT Standard Time, UTC+00:00)
# Saturday, November 17, 2018

DoAndroidsDreamThe nuclear fallout from World War Terminus has killed most of earth's animals, left a cloud of radioactive dust across the planet, and encouraged the people of Earth to emigrate to colonies on other planets. Those left on earth need to find a way to survive in a polluted and chaotic world.

The biggest advance that science has brought is the creation of androids - creatures that look exactly like humans and are designed to serve humans on the off-world colonies. But androids lack empathy and sometimes they escape their servitude, kill their human masters, and hide among the humans of Earth.

Rick Deckard is a bounty hunter, tasked with tracking down renegade androids. The police have developed a test to identify androids based on their lack of empathy.

In one 2-day period, Deckard tracks down 6 killer androids and struggles with his own purpose in life.

Do Androids Dream of Electric Sheep by Philip K. Dick tells the story of these two days and Deckard's pursuit of the androids. But mostly it tells of how Deckard and the rest of Earth's people have lost its own humanity. They spend their days using machines to alter their moods and collecting animals as status symbols (or lifelike mechanical animals, if they cannot find the real ones). Deckard himself is troubled by the empathy he feels towards the androids he is hired to destroy.

The story's title refers to Deckard's pet electric sheep that he keeps in order to impress his wife and neighbors.

Dick does a masterful job painting a dystopian society. The post-nuclear-war world is filthy and gray and empty, and people struggle to maintain a sense of normalcy. The world outside is so bland that they use a "Mood Organ" - a mood altering machine to dial their emotions either up and down. They have latched onto a religion, which is based on a VR recreation of a martyr experiencing a stoning; The most popular TV show features Buster Friendly, a goofy host, who holds his audiences in near-religious control.

Dick focuses on Deckard's struggle to find meaning in life. He questions his job: destroying androids for whom he feels empathy. He does it for the money in hopes of making his wife happy with a live animal. But his wife spends her days distracted by the Mood Organ.

Society demonizes androids for their lack of empathy; but many humans lack this same quality: the radioactive fallout caused brain damage in some humans and no one cares about them; And Deckard's years of bounty hunting take their toll on his ability to empathize.

It's worth noting that Ridley Scott's excellent 1982 movie "Blade Runner" is loosely based on this novel, which certainly boosted the book's popularity. But the book is far more cerebral than the movie, exploring themes of religion and human nature and humanity. 

"Do Androids Dream of Electric Sheep" is recommended to any fan of science fiction.

Saturday, November 17, 2018 9:30:00 AM (GMT Standard Time, UTC+00:00)
# Friday, November 16, 2018

In a previous article, I showed you how to create a new Azure Function with an HTTP trigger.

After you create an Azure Function, it is useful to be able to test it right in the Azure Portal.

To test an Azure function, log into the Azure Portal, open the Function App, and select your Function, as shown in Fig. 1

Fig. 1

Click the [Run] button (Fig. 2) above the Function to open a Log output window and a Testing dialog, as shown in in Fig. 3.

Fig. 2

Fig. 3

In the Test dialog on the right, you can change the HTTP verb by selecting either "POST" or "GET" in the "HTTP method" dropdown, as shown in Fig. 4.

Fig. 4

If you select the "POST" HTTP method, the "Request body" section (Fig. 5) is enabled and you can modify the data you want to send in the HTTP Body of your request.

Fig. 5

You can add querystring parameters to your request by clicking the "+ Add parameter" link under "Query" (Fig. 6) and entering a name and value of the parameter, as shown in Fig. 7.

Fig. 6

Fig. 7

Repeat this for as many querystring parameters as you need.

Similarly, you can add name/value pairs to the HTTP header of your  request by clicking the "+ Add header" link and entering the name and value of each header, as shown in Fig. 8.

Fig. 8

When everything is configured the way you want, click the [Run] button at the bottom (Fig.9) to call the web service and trigger your function.

Fig. 9

The "Output" section (Fig. 10) will display the HTTP response, as well as any text returned in the body of the response. Any response between 200 and 299 is good; any response of 400 and above indicates an error.

Fig. 10

If you function outputs log information, you will see this in the Log output window, as shown in Fig. 11.

Fig. 11

In this article, I showed how to test a function from within the Azure portal. You should create more sophisticated automated test as part of your build/deploy process, but this serves as a good, simple way to make sure your function is behaving as expected after you create it.

Friday, November 16, 2018 7:06:00 PM (GMT Standard Time, UTC+00:00)
# Thursday, November 15, 2018

GCast 22:

Creating an Azure Function Proxy

Learn how to create a proxy URL using Azure Functions

Thursday, November 15, 2018 9:49:00 AM (GMT Standard Time, UTC+00:00)
# Wednesday, November 14, 2018

In the last article, I showed how to create an Azure Function App. A Function App is not useful by itself: it is just a container for functions, which perform the real work.

Once you have created an Azure Function App, you will want to add one or more Functions to it.

Navigate to the Azure Portal, log in, and open your Function app, as shown in Fig. 1.

Fig. 1

Click either the [+] icon next to the "Functions" section on the left (Fig. 2) or the [New function] button at the bottom (Fig. 3)

Fig. 2

Fig. 3

NOTE: If this Function App already contains at least one function, the [New function] button does not display.

The "CHOOSE A DEVELOPMENT ENVIRONMENT" page of the "Azure Functions for .NET - getting started" dialog displays, as shown in Fig. 4

Fig. 4

Select the [In-portal] tile and click the [Continue] button to advance to the "CREATE A FUNCTION" page, as shown in Fig. 5

Fig. 5

Two triggers are listed: "Webhook+API", which will cause your function to execute after a web service URL is hit; and "Timer", which allows you to schedule your function to run at regular intervals. You can see more triggers by clicking the "More templates…" tile; but, for this demo, select the [Webhook+API] tile and click the [Create] button. After a few seconds, a function is created with an HTTP trigger and some sample code, as shown in Fig. 6.

Fig. 6

This sample function accepts a "name" parameter (either in the querystring or in the Body of a POST request) and returns an HTTP 200 (OK) response with the string "Hello, ", followed by the value of the name parameter. If no "name" parameter is supplied,  it returns a 400 (Bad Request) response with an error message.

You can now modify and save this code as you like.

In the next article, I will show you how to test this function within the portal.

Wednesday, November 14, 2018 9:59:00 AM (GMT Standard Time, UTC+00:00)
# Tuesday, November 13, 2018

An Azure Function allows you to deploy scalable code to the cloud without worrying about the server or other infrastructure issues.

Azure Functions are contained within a Function App, so you need to create a Function App first.  To create a Function App, navigate to the Azure Portal, sign in and click the [Create a resource] button, as shown in Fig. 1.

Fig. 1

From the menu, select Compute | Function App, as shown in Fig. 2.

Fig. 2

The "Create Function App" blade displays as shown in Fig. 3

Fig. 3

At the "App Name" field, enter a unique name for your Function App.

At the "Subscription" field, select the Azure subscription with which to associate this Function App. Most people will have only one subscription.

At the "Resource Group" field, select "Create new" and enter the name of a Resource Group to create or select "Use existing" and select an existing resource group in which to store your Function App. A Resource Group is an organizational grouping of related assets in Azure.

At the "OS" radio button, select the operating system (Windows or Linux) on which you wish to host your Function App.

At the Hosting plan, select either "Consumption Plan" or "App Service Plan". With the Consumption Plan, you only pay for the time that your functions are running. Since most functions do not run 24 hours a day / 7 days a week, this can be a real cost savings. With the App Service Plan, you pay as long as your functions are available. This is appropriate if you expect clients to be constantly calling your functions.

At the "Location" field, enter a region in which you want your Functions to run. In order to minimize latency, you should select a region close to any resources with which the Functions will interact.

At the "Runtime Stack" dropdown, select one of the platforms. Select ".NET" if you plan to write your code in C# or F#. Select "JavaScript" if you plan to create a node function. Select "Java" if you plan to write your code in Java. As of this writing, Java is in Preview, so performance is not guaranteed.

If you selected "Consumption Plan" hosting plan, you will be prompted for a storage account. Function definitions will be stored in this account. Select an existing storage account or create a new one. I prefer to use a storage account for all my Function Apps in a given Resource Group.

For extra monitoring, turn on Application Insights and select the same region in which your Function App is located. If this region is not available, select a nearby region.

Click the [Create] button to create your Function App.

After your Function App is created, you will want to add a Function to it. I will show how to do this in the next article.

Tuesday, November 13, 2018 9:54:00 AM (GMT Standard Time, UTC+00:00)
# Monday, November 12, 2018

Episode 537

Robert Greene on DevOps

Robert Greene defines DevOps, discusses its advantages, and describes how to accomplish it with Microsoft tooling.

Monday, November 12, 2018 9:29:00 AM (GMT Standard Time, UTC+00:00)
# Sunday, November 11, 2018

FrankensteinFrankenstein is the story of Victor Frankenstein, a scientist who discovers a way to re-animate dead tissue and uses this knowledge to build a giant, grotesque creature. Victor is repulsed by his creation and rejects it, which angers the creature and inspires him to seek revenge on his creator by destroying those closest to him.

The story of Frankenstein is familiar to almost everyone - primarily through the 1931 movie and the works that it inspired. But the original novel by Mary Wollstonecraft Shelley, published in 1818 offers much that later interpretations do not.

The novel differs from most later interpretations of the story by how much is given to the creature displays complex feelings and motivations. Unlike Karloff's mute, shuffling monstrosity, Shelley's monster was self-educated, literate, and articulate. He feels the betrayal of his maker and the pain of rejection by the world. He takes out this pain by destroying everyone that Victor loves.

In this classic horror novel, Shelley explores the dangers of playing God, the power of loneliness, and the fear of losing all that we love. She even brings in the responsibilities that fathers have for their children - a topic at least as relevant today as it was 200 years ago. Victor Frankenstein is the ultimate deadbeat dad. he runs from his responsibilities, abandoning his offspring when needed most.

There are two evil creatures in this book. Victor shirks his responsibilities and abandons the creature he created, primarily because of its physical deformities. The monster dreamed of love and acceptance; but turned to evil and violence and revenge when he was rejected by his creator and by the world.

Mary Shelley was a pioneer in the science fiction and the horror genres and the longevity of "Frankenstein" is a testament to this standing.

Sunday, November 11, 2018 9:29:00 AM (GMT Standard Time, UTC+00:00)