# Sunday, 25 November 2018

FearTrumpInTheWhiteHouseI have read a lot of biographies in my life and many of those focused on the lives of U.S. presidents; but it's rare for me to read a book about a sitting president. I made an exception with Fear: Trump in the White House - partly because it was written by Pulitzer Prize winner Bob Woodward, whose books I've always admired; and partly because Donald Trump's presidency - on which this focuses - is different from any other presidency.

So less than two years into his term, I am reading about Trump's ascent to power.

Woodward describes some of the positives of the Trump presidency. For example, he spends a lot of time discussing a decisive and successful strike on a Syrian air base in response to Syrian President Assad's use of chemical weapons on children, in violation of international law.

But Trump supporters will probably not remember the positive coverage, because there are so many unfavourable descriptions of the President. After the first third of Fear, Woodward's descriptions of Mr. Trump are largely unflattering.

The title of the book comes from a Donald Trump quote: "Real power is fear.", a sentiment he has expressed multiple times.

Entering the White House, Donald Trump had very little understanding of the most basic principles of economics or policy or politics or governing.

For example, he repeatedly suggested that printing more money was a viable solution to the deficit. 

Worse, Trump had no interest in correcting his misconceptions. After trying unsuccessfully to educate Trump on the fact that the U.S. had years ago moved from a manufacturing-based economy to a service based economy, a frustrated advisor asked the president "Why do you have these views?" to which Mr. Trump replied, "I just always have."

He displayed similar ignorance and stubbornness in other matters.

He repeatedly insisted to his advisors that the U.S. should not spend money defending other countries and resisted the argument that doing so was an investment in American security. For example, forward-positioned American troops in South Korea reduce the alert time of a potential North Korean nuclear launch from 15 minutes down to 7 seconds. His generals and economic advisors praised this as a good investment. Despite this, Trump would raise the issue every few months, arguing that the U.S. was wasting money in South Korea.

He repeatedly insisted on huge increases in tariffs, despite advice from his economic advisors that doing so would damage the economy.

Donald Trump takes pride in his decisive action, but he often does so without seeking advice or ignoring expert advice.

He pushed hard for withdrawing from Paris Climate Accord with little consultation about the legality and impact.

He declared via Twitter that transgenders would not be allowed in the military - a major policy decision that broke a campaign promise and defied existing laws. He justified it by grossly overestimating the cost and impact transgender soldiers had on the military. The military refused to enforce this ban and it ultimately failed after an expensive court battle.

In choosing his advisors and staff, Donald Trump values personal loyalty to Donald Trump over experience, intelligence, or other qualifications.

Once hired, he ruled his people by intimidation and bullying, often publicly insulting his staff. He took delight in setting one staff member against another. The result is an abundance of infighting within the White House, which made it difficult for everyone to act in a unified manner.

The advisors in the administration do everything in their power to mitigate Trump's worse impulses. Since reasoning often fails, they often deliberately delayed executing on a directive or they stole papers from the president's desk to prevent him from signing an order or even thinking about it.

This worked because the president's attention span is short, and he has no list of things to accomplish - neither on paper nor in his head. When a paper was removed from his desk, he often did not miss it.

In one of the lowest points in the Trump presidency, the president refused to condemn Nazis and Klansmen marching in Charlottesville, VA, chanting racists slogans, such as "Jews will not replace us". Trump's initial response placed blame "on both sides". His advisors finally convinced him to deliver a speech a few days later, explicitly condemning the white supremacist groups; but Trump almost immediately regretted doing so, complaining that it made him look weak. A few days later, he reverted to his equivalency argument stating there were "very fine people on both sides". His handling of the incident drew praise from Ku Klux Klan leader David Duke and almost universal criticism from non-racists in both political parties.

Fear largely paints a picture of a petty, impetuous, ill-tempered, easily distracted, stubborn, president with little ability to listen or learn

The president's pre-conceived notions (often based on ignorance), his poor listening skills, and his frequent refusal to consider opposing viewpoints often made life frustrating for his advisors, many of whom left shortly after taking the position.

Not surprisingly, Donald Trump himself declared the book "fake", even without reading it. And Trump supporters often close their mind to anything critical of their hero, labeling any criticism - fair or otherwise as "fake news". But several things reinforce the credibility of this book. One is Woodward's reputation: He has won two Pulitzer Prizes and has written critical of public figures in both major parties. The other is that the picture Woodward paints of the president in private is consistent with the image Trump projects through Twitter and his rallies. He has never shied away from personal attacks or name-calling; he frequently overstates his own abilities in speeches, claiming to be the best in the world at multiple skills; and his closest associates have publicly attacked on another.

Woodward conducted hundreds of hours of interviews for this book. He does not identify many of his sources, but it's not difficult to guess some of them, particularly when he reports on a private conversation between Trump and one other person. Woodward requested an interview with the president, but never received a reply.

The author writes in the straightforward style of a professional journalist. Woodward seldom asserts his own opinion. Instead, he quotes the opinions of others in the administration.

Woodward closes the book with a disagreement between Trump and his lawyer about whether the President should testify as part of Special Prosecutor Robert Mueller's investigation into alleged collusion between the Trump campaign and Russia. Trump insists he would be an excellent witness. His lawyer tries to tactfully discourage Trump from testifying (he even threatens to resign as his lawyer) because he knows that Trump is a habitual liar and will almost certainly commit perjury during any sworn testimony.

As I write this review, my hope is that Donald Trump will notice it and label me an enemy, as he does with so many who disagree with him.

TrumpTweet

Sunday, 25 November 2018 07:02:00 (GMT Standard Time, UTC+00:00)
# Saturday, 24 November 2018

Padington StationI was in a period of transition last week when the account rep asked if I could travel to London Monday to meet with a customer. A re-org had been announced a few days earlier and I had not yet met my new manager. Following numerous e-mails, meetings, and calls, it was decided I should make the trip. So, I found myself on Thursday afternoon purchasing an international ticket to leave for England Saturday afternoon.

Many who know me are surprised I had never visited the UK, given the amount of travel - both domestic and international - that I've done the past few years. But I was excited for my first visit.

I arrived at London's Heathrow Airport early Sunday morning, expecting to spend the day exploring the city. Unfortunately, a sleepless transatlantic flight meant that I spent much of the day in bed. I did manage to connect with a couple expats living in London, so I had lunch with James, who moved here from Ohio a few months ago; and dinner with Tobiasz, who moved to England from Poland a couple years ago.

On Monday, I also managed to connect with Andy, whom I had known for a few years from our participation in the IT Camp in Transylvania. He joined me for lunch, where I experienced  the delight that is English meat pie.

Waterhouse SqureIt was good to see friendly faces in a foreign country. But I came here to work, so I met with the customer Monday, where we successfully brainstormed potential projects on which we could work together. It was good that I came, and I went to dinner with a couple other Microsoft folks afterward to experience the delight that is fish and chips and English beer in an English pub.

St. James PalaceI had Tuesday to myself, so I opened the day with a guided walking trip around London. The tour was well worth my time and money. Sights included Buckingham Palace, St. James Palace, St. James Garden, Covent Market, and Trafalgar Square. Robin, our tour guide did an excellent job of combining humor and history. The rain began to fall as we began the tour and steadily increased in intensity until we ended 3 hours later in a downpour at Westminster Abbey. A hot meal in nearby pub took the chill off nicely.

National GalleryWith a full belly, I decided to visit the National Gallery, where I saw works by Van Gogh, da Vinci, Bruegel, Cezanne, and others. Like most museums in London, the National Gallery offers free admission, with a suggested (but not required) donation. I could easily spend an entire day at the Gallery, but a couple hours sufficed on this day.

I walked around the city about more, passing through Leicester Square and Chinatown before heading back to my hotel.

My new manager is based in the UK, so he drove to London Tuesday evening and I had a chance to meet him face-to-face for the first time.

After we parted, I decided I needed to experience one more English beer in one more English pub before checking in for my final night and my flight home. I chose the White Hart, which bills itself as "The Oldest licensed premises in London", where I sat in a corner, sipped a lager, and read a couple chapters of "Brideshead Revisited".

It was a whirlwind trip, executed in a short time with a minimum of planning, but it was well worth the effort. I hope to return soon.

Saturday, 24 November 2018 08:26:00 (GMT Standard Time, UTC+00:00)
# Friday, 23 November 2018

Azure Functions provide a simple way to deploy code in a scalable, cost-effective way.

By default, Azure Functions are stateless, which makes it difficult to create complex workflows with basic Azure functions - particularly long-running workflows, such as those that require human interaction.

A Durable Azure Function maintains state for a long time, without having to stay in memory, making it ideal for orchestrations. Stateful information is stored in an Azure Storage Account when the the process terminates. This saves you money, because the default pricing model for Azure functions only charges you while the function is running.

A Durable Function is not triggered in the same way as other Azure Functions (via HTTP, queue, database changes, timer, etc.) Rather, it is called from a "starter" function, which can be triggered in the usual way.

Rather than placing all logic within a single Durable Function, it usually makes more sense to split tasks into individual Activity Functions and have the Durable Function manage these. The most simple Durable Function would simply call multiple activities in sequence. A diagram of this is shown in Fig. 1.

DF01-DurableFunctionFlow
Fig. 1

You can create a Function App for an Azure Durable function in Visual Studio in the same way you create any function - by selecting File | New Project from the menu and selecting "Azure Functions" from the Project Templates dialog, as shown in Fig. 2.

DF02-NewFunctionProject
Fig. 2

Select "Azure Functions v2" from the top dropdown and HttpTrigger" from the list of templates, as shown in Fig. 3; then, click the [OK] button to create the solution and project.

DF03-FunctionTemplate
Fig. 3

The new project contains a function named "Function1". Right-click this function in the Solution Explorer and rename it to "StarterFunction", as shown in Fig. 4.

DF04-RenameFunction
Fig. 4

Open StarterFunction.cs and change the first line of the class from

[FunctionName("Function1")]

to

[FunctionName("StarterFunction")]

Now, you can add a Durable Function to the project. Right-click the project in the Solution Explorer and select Add | New Azure Function from the context menu, as shown in Fig. 5.

DF05-AddNewAzureFunction
Fig. 5

Name the new function "DurableFunction1", as shown in Fig. 6.

DF06-AddDurableFunction
Fig. 6

At the next dialog, select "Durable Function Orchestration" from the list of triggers and click the [OK] button to create the function, as shown in Fig. 7.

DF07-DurableFunctionsOrchestration
Fig. 7

This Durable Function will manage 3 functions, calling each one sequentially. To the project, add 3 new functions named "Function1", "Function2", and "Function3". It does not matter which trigger you choose, because we are going to overwrite the trigger. Paste the code below into each function:

    public static class Function1 
    { 
        [FunctionName("Function1")] 
        public static async Task<string> Run( 
            [ActivityTrigger] string msg, 
            ILogger log) 
        { 
            log.LogWarning("This is Function 1");

            await Task.Delay(10000); 
            msg += "Function1 done; "; 
            return msg; 
        } 
    }
  

Listing 1

    public static class Function2 
    { 
        [FunctionName("Function2")] 
        public static async Task<string> Run( 
             [ActivityTrigger] string msg, 
            ILogger log) 
        { 
            log.LogWarning("This is Function 2");

            await Task.Delay(10000); 
            msg += "Function2 done; "; 
            return msg; 
        } 
    }
  

Listing 2

    public static class Function3 
    { 
        [FunctionName("Function3")] 
        public static async Task<string> Run( 
            [ActivityTrigger] string msg, 
            ILogger log) 
        { 
            log.LogWarning("This is Function 3");

            await Task.Delay(10000); 
            msg += "Function3 done; "; 
            return msg; 
        } 
    }
  

Listing 3

As you can see, each function essentially does the same thing: log a brief message; wait 10 seconds; then, return a string consisting of the string passed in with a bit more appended to the end.

Notice also that the "msg" parameter in each function is decorated with the [ActivityTrigger] attribute, which is what makes each of these an Activity Function.

The Task.Delay() simulates a long-running activity. Imagine an activity that requires human input, such as a manager navigating to a web page and filling out a form. It might take days or weeks for this to happen. We certainly would not the application to continue running during this time: This would be an inefficient use of resources and it would be expensive. Durable functions handle this by storing state information in Azure storage; then retrieving that state when the function needs to resume.

Return to the DurableFunction1 class and replace the code with the following:

    public static class DurableFunction1 
    { 
        [FunctionName("DurableFunction1")] 
        public static async Task<IActionResult> Run( 
            [OrchestrationTrigger] DurableOrchestrationContext ctx, 
            ILogger log) 
        { 
            var msg = "Durable Function: "; 
             msg = await ctx.CallActivityAsync<string>("Function1", msg); 
            msg = await ctx.CallActivityAsync<string>("Function2", msg); 
            msg = await ctx.CallActivityAsync<string>("Function3", msg);

            // Use LogWarning, so it shows up in Yellow, making it easier to spot 
            log.LogWarning(msg);

            return new OkObjectResult(msg); 
        } 
    }
  

Listing 4

You will probably have to add the following to the top of the file in order for it to compile:

using Microsoft.AspNetCore.Mvc;

In Listing 4, we see that the Durable Function calls the 3 Activity functions in order. It passes to each Activity Function the output of the previous function. At then end of the orchestration, we expect to see a concatenation of messages from each of the 3 Activity Functions.

Notice also the parameter of type DurableOrchestrationContext, which is decorated with the [OrchestrationTrigger] attribute. This identifies this as a Durable Orchestration Function.

Finally, return to the StarterFunction class and replace the code with the following:

    public static class StarterFunction
    {
        [FunctionName("StarterFunction")]
        public static async Task<HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
            HttpRequestMessage req,
            [OrchestrationClient] DurableOrchestrationClient starter,
            ILogger log)
        {
            log.LogInformation("About to start orchestration");

            var orchestrationId = await starter.StartNewAsync("DurableFunction1", log);
            return starter.CreateCheckStatusResponse(req, orchestrationId);
        }
    }
  

Listing 5

To see this in action, compile and run the project. A console will display similar to the one in Fig. 8.

DF08-RunFunction
Fig. 8.

You can trigger the StarterFunction by issuing an HTTP GET to the URL displayed in the console (in this case http://localhost:7071/api/StarterFunction). Open a browser, enter this URL into the address bar, and press [ENTER].

Watch the console. You should see the log statements in each of the functions display in turn. Finally, we will see the final value of the msg variable after being passed to all 3 Activity functions. The output should look something like fig. 9.

DF09-FunctionComplete
Fig. 9

This illustrates the concepts of a Durable Orchestration Function. You can view the source code in the SequentialDurableFunctionDemo project at my Azure-Function-Demos GitHub repository.

Friday, 23 November 2018 09:23:00 (GMT Standard Time, UTC+00:00)
# Thursday, 22 November 2018

GCast 23:

Azure Logic Apps

Learn how to create a Logic App to deploy a workflow in the cloud.

Thursday, 22 November 2018 09:12:00 (GMT Standard Time, UTC+00:00)
# Wednesday, 21 November 2018

Azure Functions allow you to declaratively add bindings to external resources by decorating a C# function with binding attributes.

This means you need to write less code and the code you do write will focus more on your business logic than on updating resources.

In this article, I will show you how to add CosmosDB bindings to an Azure function in order to read from and write to a CosmosDB database.

Create an Configure CosmosDB database and collection

See this article to learn how to create a new CosmosDB instance.

Next create a Database and Collection within your CosmosDB. This article describes how to create a CosmosDB Database and Collection; or you can quickly create a Database named "ToDoList" and a Collection named "Items" from the "Quick Start" tab of the CosmosDB database you created, as shown in Fig. 1.

CD01-QuickStart
Fig. 1

As you work with data in this database, you can view the documents on the "Data Explorer" tab, as shown in Fig. 2.

CD02-DataExplorer
Fig. 2

You will need the Connection String of your CosmosDB. You can find two connection strings on the "Keys" tab, as shown in Fig. 3. Copy either one and save it for later.

CD03-Keys
Fig. 3

Visual Studio project

Create a function in Visual Studio 2017. If you base it on the "Azure Functions" template (Fig. 4), it will have many of the necessary references.

CD04-NewAzureFunctionApp
Fig. 4

Open the local.settings.json file and add a key for "CosmosDBConnection", as shown in Fig. 5. Set its value to the connection string you copied from the "Keys" blade above.

CD05-localsettingsjson
Fig. 5

Delete the existing Function1.cs file from the project and add a new function by right-clicking the project in the Solution Explorer and selecting Add | New Function from the context menu, as shown in Fig. 6. Give the function a meaningful name.

CD06-AddFunction
Fig. 6

Repeat this for any function you wish to add.

Create a model of the expected data

CosmosDB is a schemaless document database, meaning that the database engine does not enforce the type of data it accepts. This is distinct from something like SQL Server, which requires you to define in advance the name, data type, and rules of each column you expect to store.

If you want to validate data, you must do so in your application. One way to do this is to create a Model class that matches the expected incoming data.

In my demo, I expect only to store data that looks like the following:

{
"id" : "001",
"description" : "Write blog post",
"isComplete" : false
}
  

So I created the ToDoItem class shown in Listing 1

public class ToDoItem 
 { 
    [JsonProperty("id")] 
    public string Id { get; set; }

    [JsonProperty("description")] 
    public string Description { get; set; }

    [JsonProperty("isComplete")] 
    public bool IsComplete { get; set; } 
 }
  

Listing 1

Insert a document

The code below generates a function to insert a new document into a database. The function is triggered when you send an HTTP POST request to the function's URL (in this case, "api/InserToDoItem). The document will have the value of the JSON

[FunctionName("InsertItem")] 
public static HttpResponseMessage Run( 
    [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]HttpRequestMessage req, 
    [CosmosDB( 
        databaseName: "ToDoList", 
        collectionName: "Items", 
        ConnectionStringSetting = "CosmosDBConnection")] 
    out ToDoItem document, 
    ILogger log) 
{ 
    var content = req.Content; 
    string jsonContent = content.ReadAsStringAsync().Result; 
    document = JsonConvert.DeserializeObject<ToDoItem>(jsonContent);

    log.LogInformation($"C# Queue trigger function inserted one row");

    return new HttpResponseMessage(HttpStatusCode.Created); 
}
  

Let's walk through the function.

[FunctionName("InsertItem")]

The name of the function is InsertItem

public static HttpResponseMessage Run(

The Run method executes when the function is triggered. It returns an HTTP Response Message

[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]HttpRequestMessage req,

The first parameter is the incoming HTTP Request. It is decorated with HttpTrigger, indicating this is an HTTP trigger. Within this decorator's parameters, we indicate that the function can be called anonymously, that it can only be called with an HTTP POST (not GET or PUT or any other verb); and that we are not changing the default routing.

[CosmosDB(
      databaseName: "ToDoList",
      collectionName: "Items",
      ConnectionStringSetting = "CosmosDBConnection")]
     out ToDoItem document,        

The second parameter is an output parameter of type ToDoItem. We will populate this with the data in the Request body, so we type it as a ToDoItem. This parameter is decorated with the CosmosDB attribute, indicating that we will automatically insert this document into the CosmosDB. The databaseName, collectionName, and ConnectionStringSetting tell the function exactly where to store the document. The ConnectionStringSetting argument must match the name  we added     for the connection string in the local.settings.json file, as described above.

ILogger log)

The logger allows us to log information at points in the function, which can be helpful when troubleshooting and debugging.

var content = req.Content;
string jsonContent = content.ReadAsStringAsync().Result;
document = JsonConvert.DeserializeObject<ToDoItem>(jsonContent);

The 3 lines above retrieve the body in the HTTP POST request and convert it to a .NET object of type ToDoItem, which validates that it is the correct format.

log.LogInformation($"C# Queue trigger function inserted one row");

This line is not necessary, but may help us to understand what part of the function executed when we are troubleshooting.

return new HttpResponseMessage(HttpStatusCode.Created);

When the document is successfully inserted, we return an HTTP 201 (Created) status to indicate success.

Retrieve all documents

The following function retrieves all the documents in a container.

    public static class GetItems
    {
        [FunctionName("GetItems")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
            [CosmosDB(
                databaseName: "ToDoList",
                collectionName: "Items",
                ConnectionStringSetting = "CosmosDBConnection",
                SqlQuery = "select * from Items")
            ]IEnumerable<ToDoItem> toDoItems,
            ILogger log)
        {
            log.LogInformation($"Function triggered");

            if (toDoItems == null)
            {
                log.LogInformation($"No Todo items found");
            }
            else
            {
                var ltodoitems = (List<ToDoItem>)toDoItems;
                if (ltodoitems.Count == 0)
                {
                    log.LogInformation($"No Todo items found");
                }
                else
                {
                    log.LogInformation($"{ltodoitems.Count} Todo items found");
                }
            }

            return new OkObjectResult(toDoItems);
        }
    }
  

Breaking down this function:

[FunctionName("GetItems")]        

The name of the function is “GetItems”.

public static async Task<IActionResult> Run(

The Run method executes when the function is triggered. This method is asynchronous and will eventually return an ActionResult.

[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]HttpRequestMessage req,

The first parameter is the incoming HTTP Request. It is decorated with HttpTrigger, indicating this is an HTTP trigger. Within this decorator's parameters, we indicate that the function can be called anonymously, that it can only be called with an HTTP GET; and that we are not changing the default routing.

[CosmosDB(
databaseName: "ToDoList",
collectionName: "Items",
ConnectionStringSetting = "CosmosDBConnection",
SqlQuery = "select * from Items") ]IEnumerable<ToDoItem> toDoItems,

This parameter is what will be returned by the function (eventually, because it runs asynchronously). It is a list of objects of type ToDoItem. When serialized, this will be transformed into an array of JSON objects. This parameter is decorated with the CosmosDB attribute, indicating that we will automatically retrieve the list from the CosmosDB. The databaseName, collectionName, and ConnectionStringSetting tell the function exactly where to store the document. The SQlQuery tells what query to run to retrieve the data (in this case, return all the rows)

ILogger log)

The logger allows us to log information at points in the function, which can be helpful when troubleshooting and debugging.

log.LogInformation($"Function triggered");
if (toDoItems == null)
    {
         log.LogInformation($"No Todo items found");
    }
    else
    {
         var ltodoitems = (List<ToDoItem>)toDoItems;
         if (ltodoitems.Count == 0)
        {
            log.LogInformation($"No Todo items found");
        }
        else
        {
             log.LogInformation($"{ltodoitems.Count} Todo items found");
         }
    }

We did not need to write code to query the database. This happens automatically. The code above simply verifies that items were returned and transforms them into  List<ToDoItem> and stores this list in a local variable.

return new OkObjectResult(toDoItems);

We return a 200 (“OK”) HTTP response and the list of items.

Retrieve a single document by its ID

The following function retrieves a single document, given the ID.

    public static class GetItemById
    {
        [FunctionName("GetItemById")]
            public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "GetItem/{id}")] HttpRequestMessage req,
            [CosmosDB(
                databaseName: "ToDoList",
                collectionName: "Items",
                ConnectionStringSetting = "CosmosDBConnection",
                Id = "{id}")
            ]ToDoItem toDoItem,
            ILogger log)
        {
            log.LogInformation($"Function triggered");

            if (toDoItem == null)
            {
                log.LogInformation($"Item not found");
                return new NotFoundObjectResult("Id not found in collection");
            }
            else
            {
                log.LogInformation($"Found ToDo item {toDoItem.Description}");
                return new OkObjectResult(toDoItem);
            }

        }
    }
  

Here are the details of this function:

[FunctionName("GetItemById")]        

The name of the function is “GetItemById”

public static async Task<IActionResult> Run(

The Run method executes when the function is triggered. This method is asynchronous and will eventually return an ActionResult.

[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]HttpRequestMessage req,

The first parameter is the incoming HTTP Request. It is decorated with HttpTrigger, indicating this is an HTTP trigger. Within this decorator's parameters, we indicate that the function can be called anonymously, that it can only be called with an HTTP GET; and that we are not changing the default routing.

[CosmosDB(
databaseName: "ToDoList",
collectionName: "Items",
ConnectionStringSetting = "CosmosDBConnection",
Id = "{id}")
]IEnumerable<ToDoItem> toDoItems,
 

This parameter is what will be returned by the function (eventually, because it runs asynchronously). It will be an object of type ToDoItem. This parameter is decorated with the CosmosDB attribute, indicating that we will automatically retrieve the list from the CosmosDB. The databaseName, collectionName, and ConnectionStringSetting tell the function exactly where to store the document. The id tells the function on which Id to filter the results.  

ILogger log)

The logger allows us to log information at points in the function, which can be helpful when troubleshooting and debugging.

log.LogInformation($"Function triggered");

Debugging information. Not necessary for the operation, but helpful when troubleshooting.  

if (toDoItem == null)
{
    log.LogInformation($"Item not found");
   return new NotFoundObjectResult("Id not found in collection");
}
else
{
    log.LogInformation($"Found ToDo item {toDoItem.Description}");
   return new OkObjectResult(toDoItem);
}

We did not need to write code to query the database. This happens automatically. The code above simply checks if an item was returned matching the ID. If an item is found, we return a 200 (“OK”) HTTP response, along with the item. If no item is returned, we return a 404 (“Not Found) HTTP response.


Retrieve a set of documents using a query

The following function retrieves a set of document. A query tells the function how to filter, sort and otherwise retrieve the documents. In this example, we only want to return documents for which isComplete = true.

    public static class GetCompleteItems
    {
        [FunctionName("GetCompleteItems")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
            [CosmosDB(
                databaseName: "ToDoList",
                collectionName: "Items",
                ConnectionStringSetting = "CosmosDBConnection",
                SqlQuery = "select * from Items i where i.isComplete")
            ]IEnumerable<ToDoItem> toDoItems,
            ILogger log)
        {
            log.LogInformation($"Function triggered");

            if (toDoItems == null)
            {
                log.LogInformation($"No complete Todo items found");
            }
            else
            {
                var ltodoitems = (List<ToDoItem>)toDoItems;
                if (ltodoitems.Count == 0)
                {
                    log.LogInformation($"No complete Todo items found");
                }
                else
                {
                    log.LogInformation($"{ltodoitems.Count} Todo items found");
                }
            }

            return new OkObjectResult(toDoItems);
        }
    }
  

We will now explore this function in more detail:   

[FunctionName("GetCompleteItems")]        

The name of the function is “GetCompleteItems”.

public static async Task<IActionResult> Run(

The Run method executes when the function is triggered. This method is asynchronous and will eventually return an ActionResult.

[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]HttpRequestMessage req,

The first parameter is the incoming HTTP Request. It is decorated with HttpTrigger, indicating this is an HTTP trigger. Within this decorator's parameters, we indicate that the function can be called anonymously, that it can only be called with an HTTP GET; and that we are not changing the default routing.

[CosmosDB(
databaseName: "ToDoList",
collectionName: "Items",
ConnectionStringSetting = "CosmosDBConnection",
SqlQuery = "select * from Items i where i.isComplete")
]IEnumerable<ToDoItem> toDoItems,

This parameter is what will be returned by the function (eventually, because it runs asynchronously). It is a list of objects of type ToDoItem. When serialized, this will be transformed into an array of JSON objects. This parameter is decorated with the CosmosDB attribute, indicating that we will automatically retrieve the list from the CosmosDB. The databaseName, collectionName, and ConnectionStringSetting tell the function exactly where to store the document. The SQlQuery tells what query to run to retrieve the data (in this case, return only rows with isComplete=true) It is important to note that I am using the JSON property (“isComplete”), rather than the .NET class property (“IsComplete”) in this query. Even though they differ only in their case, the query is case-sensitive.  

ILogger log)

The logger allows us to log information at points in the function, which can be helpful when troubleshooting and debugging.

log.LogInformation($"Function triggered");
if (toDoItems == null)
    {
         log.LogInformation($"No complete Todo items found");
    }
    else
    {
         var ltodoitems = (List<ToDoItem>)toDoItems;
         if (ltodoitems.Count == 0)
        {
            log.LogInformation($"No complete Todo items found");
        }
        else
        {
             log.LogInformation($"{ltodoitems.Count} Todo items found");
         }
    }

We did not need to write code to query the database. This happens automatically. The code above simply verifies that items were returned and transforms them into  List<ToDoItem> and stores this list in a local variable.

return new OkObjectResult(toDoItems);

We return a 200 (“OK”) HTTP response and the list of items.

Conclusion

Notice that in each of these functions, I did not need to write code to query or update the database. By decorating a parameter with the CosmosDb attribute, the function automatically took care of the database operations.

You can find this code in the CosmosDBBinding solution in my Azure Function demos on GitHub.

Wednesday, 21 November 2018 09:07:00 (GMT Standard Time, UTC+00:00)
# Tuesday, 20 November 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.

AF01-AzureDevTools
Fig. 1

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

AF02-FileNewProject
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.

AF03-AzureFunctionTemplate
Fig. 3

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

AF04-FunctionOptions
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:

[FunctionName("Function1")]
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.

[FunctionName("Function1")]
  

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.

AF05-RightClickPublish
Fig. 5

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

AF06-PickPublishTarget
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, 20 November 2018 09:41:00 (GMT Standard Time, UTC+00:00)
# Monday, 19 November 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, 19 November 2018 07:16:00 (GMT Standard Time, UTC+00:00)
# Sunday, 18 November 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, 18 November 2018 08:33:00 (GMT Standard Time, UTC+00:00)
# Saturday, 17 November 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, 17 November 2018 09:30:00 (GMT Standard Time, UTC+00:00)
# Friday, 16 November 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

TF01-Function
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.

TF02-RunButton
Fig. 2

TF03-TestDialog
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.

TF04-HttpMethod
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.

TF05-RequestBody
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.

TF06-QueryParameters
Fig. 6

TF07-AddParameter
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.

TF08-AddHeader
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.

TF09-RunButton
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.

TF10-Output
Fig. 10

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

TF11-LogOutput
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, 16 November 2018 19:06:00 (GMT Standard Time, UTC+00:00)