3. Building a software solution – Automate building the environment

Welcome back, I am continuing my journey of building a complete software solution. To remind you, I am building a sports league management solution. Granted, I’m not running any sports leagues, so, any domain knowledge from that side would be excellent, if anyone has any input. What I do have experience with is being a parent and fighting with websites just to figure out when games and practices are. Or, the chain emails that they forwarded around that have 10 different dates and times in them. There has to be a better and easier way to at least coordinate practices and games. Perhaps in the future, I’ll get into stat tracking and analytics.

After the last post, I realized that I wanted to properly version and have an effective CI/CD solution for the infrastructure. So, I deleted all of the After the last post, I realized that I wanted to have correct version control and an effective CI/CD solution for the infrastructure. So, I deleted all of the resources created. We are going to automate and build the environment using Azure DevOps and Azure Resource Manager (ARM) Templates. To start, we go to Azure DevOps and create a new build pipeline.

DevOps will now ask you where the code is being stored, I choose GitHub and select the appropriate repository:

When it asks you to “Configure your pipeline choose “Starter pipeline”. You will then be presented a YAML file that loos like this:

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

- master

  vmImage: 'ubuntu-latest'

- script: echo Hello, world!
  displayName: 'Run a one-line script'

- script: |
    echo Add other tasks to build, test, and deploy your project.
    echo See https://aka.ms/yaml
  displayName: 'Run a multi-line script'

The first thing I want to do is change that trigger statement. I am currently working on building my dev environment. I want this to trigger on commits to my develop branch. I am using the GitFlow branching scheme if you want to use that. What this means is that I’ll have a develop branch that I use for my dev environment. I’ll also create a build pipeline that triggers on my master branch for PROD. To push to master, I’ll do a GitFlow release. I follow this branching mechanism because it also helps me by allowing me to use GitVersion for semantic versioning automatically. I am going to be building that into my pipeline as well. I make use of GitVersion in my pipeline, so it is necessary to add it from the Visual Studio Marketplace.

You can edit the YAML file in the browser, or after you save it, pull it locally from your repository to edit it. If you choose to edit it in the browser, there is an assistant that can help you add items to your YAML from the menu of items available:

Ok, so, after changing the trigger to the develop branch, I then go and delete everything after steps: and then look for the “GitVersion Task” in the Tasks menu. The task asks you for some information, and there is a lot that can be done with GitVersion, just not for this project. I mostly just want it to get the semantic version details from my git repo. So I accept the defaults and add it to my project. My pipeline now looks like this:

- master

  vmImage: 'ubuntu-latest'

- task: GitVersion@5
    runtime: 'core'

Now that I have GitVersion installed, I want my build version the same as the version of the code in the repository. I found a fantastic article where the author accomplished this. Check it out at https://www.neovolve.com/2019/05/03/gitversion-and-github-with-azure-devops-build-yaml/. They added a script task like this:

- script: echo %Action%%BuildVersion%
  displayName: 'Set build version'
    Action: '##vso[build.updatebuildnumber]'
    BuildVersion: $(GitVersion.SemVer)

In their article, they were doing NuGet versioning, and I knew from the Visual Studio Marketplace page that by adding the GitVersion to my project, I would have access to the $(GitVersion.SemVer) variable. So I changed their example, which sets the build version to my semantic versioning provided by GitFlow.

And then select New variable:

As the screen shows, DevOps can store encrypted secrets so that you should NEVER put a username and password, or certificate, or anything that should be secure in your source code. Later, when we add a SQL Server and DB to our project variables will store secrets. Now I’ll add my variables:

I add the following variables:

SubscriptionIDThis is the GUID of my subscription

Now, the only way I could figure out how to create a resource group was to use a Powershell script. Since I want this to run inside of my Azure subscription I chose the Azure CLI item from the task list:

I then chose my subscription from the drop-down, I chose Powershell Core (because everything I do on my Mac is using Powershell Core). Next, you can save the script in your repo and select it, but I chose to put my script inline. Here is the entire step in the pipeline with the script:

- task: AzureCLI@2
  displayName: 'Create Resource Group if one does not exist'
    azureSubscription: 'My Subscription Name'
    scriptType: 'pscore'
    scriptLocation: 'inlineScript'
    inlineScript: |
      az login --identity
      $GroupExists = $(az group exists --name $env:ResourceGroupName --subscription $env:SubscriptionID)
      if ($GroupExists -eq "False") {
          az group create --location $env:ResourceGroupLocation --name $env:ResourceGroupName --subscription $env:SubscriptionID

Ok, let’s break this down. The Powershell script I wrote is in the inlineScript: section. The first line is az login --identity There are several options here. You can pass in a username and password, or a certificate. If you want to see all of your options look here. I chose identity because I have authenticated my DevOps environment to my Azure Tenant. It has the necessary roles to create my items. Next, I get the result of the Azure CLI command az group exists --name $env:ResourceGroupName --subscription $env:SubscriptionID This command will return the boolean string value of either “True” or “False.” You also see that I have added some of the environment variables to the Azure CLI command. The exists command will tell me if the Resource Group exists. If it does not, I then run the az group create to create the resource group. If you want to know more about what Azure CLI options for resource groups, check out the documentation.

So, now, our build pipeline creates the resource group if it is needed. I am not going to create all of the resources in this pipeline completely. I plan to put the actual template deployment in the release pipeline. The reason for this is that if I want to roll back my environment, I want to have that available as an option. So, instead, I am going to publish all of the items in my repo as a pipeline artifact using the Publish Pipeline Artifacts step:

I named my artifact Templates-Develop with the step looking like this:

- task: PublishPipelineArtifact@1
    targetPath: '$(Pipeline.Workspace)'
    artifact: 'Templates-Develop'
    publishLocation: 'pipeline'

The Publish Pipeline Artifacts takes my entire repository and saves the files for the release pipeline. In my next post, I’ll go over how to build an ARM Template and release it.

2. Building a software solution – Where will it run?

As I was deciding to build this, I was torn on where to begin. Should I build the front-end? Maybe I should model out the data entities, and build the API? After reading Clean Code by Uncle Bob, I quickly realized that my days of building a solution by starting at the database are over. I might feel more comfortable in the 3rd normal form, and be able to query anything when the data normalized, but that is not where my application will start.

Furthermore, clean architecture shows that items like the database are “accessory” items. I’ll eventually be making use of Entity Framework Core, but by using a code-first approach, I won’t have to worry myself much with building the database and tables ahead of time. I may have to build tables and views for the “Query” side of my CQRS design, but that will come later. The first thing I should build is the environment where my application will run.

My initial plan for my app

This application will run in Azure, so the first thing I did was create a resource group (using PowerShell and Azure CLI):

az login
az group create --location eastus --name LeagueTrackr_DEV --subscription SUBSCRIPTION_GUID

The az login command will pop open a web browser and look ask you to sign in to your Azure account. When that is successful, you can close the browser window, your terminal/PowerShell will then be authenticated. The next command creates a resource group. In Azure, all resources need a resource group and a subscription. I am going to assume you have a subscription already, that is usually set up when you create your Azure account.

az appservice plan create --name LeagueTrackrAppPlan_DEV --resource-group LeagueTrackr_DEV --is-linux --location eastus --sku B1 --subscription SUBSCRIPTION_GUID

This command creates what is called an App Service Plan. An App Service Plan determines what kind of “server” will be used to run my code. I put the server in quotes because I am making use of Azure’s Platform as a Service (PAAS) offering. I am not creating a VM with Linux or Windows, even though there will be one somewhere running my code. I am also telling my “server” to be on the basic 1 tier and be a Linux server. I chose the basic 1 tier because it is the cheapest just above the free tier. I would have preferred to use the free tier, but you cannot have two free app plans in one resource group.

az webapp create --name LeagueTrackr_DEV --plan LeagueTrackrAppPlan_DEV --resource-group League-Trackr_DEV --runtime "node|LTS"

Now I will do both of these steps again to create my API. Both the API and Web App could run in the same App Service plan, but in the event that I ever need to scale one and not affect the other, it would be difficult. For that purpose, I am going to split them out now.

Next, I will create the Azure SQL Server and the Azure SQL Database for my project:

az sql server create --admin-password PASSWORD --admin-user ADMINUSER --name leaguetrackrsql-dev --resource-group League-Trackr_DEV --location eastus --subscription SUBSCRIPTION_GUID
az sql db create --name LeagueTrackrDB_DEV --resource-group --server leaguetrackrsql-dev --edition Basic --subscription SUBSCRIPTION_GUID

Once this is complete, a dev environment will exist. However, the problem with this is that I don’t need to keep a dev environment around forever. If I am not actively developing, I’ll want to delete this environment to save me some money. I do want to be able to recreate this environment quickly if needed. I also want to be able to script any changes and push those to production when it is created. To do this, I’ll make use of Azure Resource Manager (ARM) templates and deploy them using Azure DevOps. I’ll look at this in my next post.

1. Building a software solution – Intro

Recently I was challenged to upgrade my level of knowledge regarding web-based solutions. My experience has been in building business solutions making use of many technologies like SSRS, SSIS, ASP.NET Web API’s, SQL Server, Power BI, Microsoft Dynamics, and more. I’ve built portions that all exist in a full web application, and I’ve even deployed solutions to Azure. Still, I have don’t have much experience putting them all together in a single solution or product. I am accomplishing this, and as I do it, I plan to document my work here on this blog.

The first challenge I ran into was that I needed something to build. I know that if I don’t care about the project, it’ll just go by the wayside, and I’ll never finish it. Also, I didn’t want to take anything on that was going to be over my head. I have kids that play in sports (Baseball/Softball, Basketball, Soccer, and more). Thus far in my parental career, I have yet to interact with a sports league website that just simply allowed parents to keep track of practices and games, and know what is going on with their child’s sporting event. I’m sure there is a decent solution out there, I’ve just never seen it. So, I’m going to build one. A sports league solution should be a reasonably straightforward (famous last words) solution. I need to learn React, so my solution will be a React front end, with a .NET Core 3 Web API back-end.

Furthermore, I was given the challenge to build the solution using the CQRS pattern. I’ve never explicitly implemented that pattern before, but I will be figuring out how to during this project. The back-end database will be an Azure SQL instance. All components will be deployed to Azure, with a CI/CD pipeline built using Azure DevOps.

Here are the links to what I am going to be building:

I know I don’t have the repositories yet but there will also be one for the Web API, and the database. When I create those, I will come back and update the links.

As I am writing this, I am also open to suggestions, as long as they aren’t in the realm of Use x framework instead of y, or deploy to a cloud provider instead of b. If a suggestion is provided, I ask that a good logical reason be provided. This is a learning journey

I hate Microsoft… (this is such an annoying phrase)

Hello everyone.  I want to write an article that discusses an issue that drives me insane.  Recently I heard a developer claim “I hate Microsoft…” and proceeded on a tirade of this reason or that reason why they are frustrated.  In the world of technology, Microsoft has been around long enough that we should realize they aren’t going anywhere any time soon.  Whenever I hear a tech person proclaim that they hate Microsoft, in my ears, it sounds like a mechanic saying: “I hate metric tools.”  It is a pointless thing to hate.  When I hear that someone is frustrated with Microsoft, what it usually comes down to, is that they are not aware of what the right setting is they are looking for.  The product is doing exactly what it is supposed to, and the developer just doesn’t know what setting to change.  In the past several years, since Satya took over, Microsoft has taken a 180-degree turn and started building some fantastic tools.  They say it is hard for a rudder to turn a large ship, but Satya has done it.  In fact, Microsoft seems to have the attitude that I would want to see out of any tech company: “Check out what we have, but if you don’t like it, we’ll support you using what you want.”  The next time you hear someone proclaim they don’t like Microsoft, just remind them that it doesn’t make sense to not like your tool, you just need to learn how to use your tool.

I just want to clarify, I am not paid by Microsoft, nor am I saying that they are the best tool for everything.  However, they are a prevalent force in the tech industry and that means if you want to be good at Tech, you need to know how to use Microsoft products.

Azure.Mobile.Server.EntityData ModelState is invalid… why?

So, I have been building an ASP.NET MVC 5 web application making use of the Azure.Mobile.Server SDK.  So I created my model objects, and everything is excellent.  I used the scaffolding to generate my Controllers and Views.  From there I spent some time customizing my views, which I hate to do.  I’m not a very good front-end developer, and I have all the respect in the world for those who are skilled at making things look pretty.  So I went to test the various CRUD operations and starting at Create, I failed.  I set a breakpoint and found out that my model state was invalid.  The default code does something like this:


When I was clicking the submit button on my form, the check of ModelState.IsValid was coming back false.  This is because in my form, I was only adding data to “ModelProperty.”  The rest of the bindings (Id, Version, CreatedAt, UpdatedAt, Deleted) are all part of the Microsoft.Azure.Mobile.Server.EntityData object type which I inherited in my model.  I use this data type because I have an accompanying mobile app and the Mobile SDK does a great job of offline syncing my data.  So… I had to remove those bindings from the [Bind(Include = “…”)] statement.  Once I removed those, my ModelState was valid, and life continued.

The next tech bust

The internet is a wonderful source of information.  Videos and volumes of text exist to provide information for just about any imaginable topic.  In 1990 the World Wide Web was born.  In the past 28 years, we have managed to fill hard drives with data from almost all recorded parts of human history.  Why did people put forth the effort to compile all of this information?  It started as an academic endeavor, but it quickly became a method of making money.  Fast forward to present day, and everyone with a computer and a blog can start making money.  How is it so easy to start making money?  It is because money is being earned on false pretense.

Continue reading → The next tech bust

Finding the last date a date stamped item shows up in a table (SQL)

So, I was writing a query to retrieve data from a Dynamics GP table.  There was a question about when certain codes were last used.  So, my thought process took me down the path of saying I could write a query that pulls all of the distinct codes from the table like so:


However, there is also a date column for when the code is used.  This is because it is a transaction table.  So, if I add in the date stamp, I will get tons of rows returned as a result of the query.  I needed some way to only get the last date stamp that each code was used.  For that I used the following code:

FROM QueryTable

So how does this work?  According to the Microsoft Docs:

You can use the OVER clause with functions to compute aggregated values such as moving averages, cumulative aggregates, running totals, or a top N per group results.

I guess this is a good time as ever to say that I am using SQL Server, and this applied to SQL Server 2008 and beyond.  So, this essentially makes a group of each code, and then uses the aggregate function MAX to find the maximum date used per group of code.  Now I can say when the last time a specific code was used.

Greatest Common Denominator/Divisor/Factor (Geek Style – Euclidean Algorithm)

I recall learning about performing fractional math when I was young.  To be completely honest, I never actually learned an efficient way of determining greatest common denominator.  I brute forced it most of the time, meaning I just kept trying numbers until it seemed like I got the right denominator.  My method was very similar to what you see here at Kahn Academy.  As part of an algorithms course, I needed to write a function to determine the Greatest Common Denominator.  Any time you start talking about writing code

Any time you start talking about writing code, and you consider performing a function that “brute forces” an answer, your code will not run very fast.  Sometimes, you have no choice but to brute force a solution.  Sometimes there is a better way.  In this case, brute force is not the best way.  Here is how a brute force method would look:

Continue reading → Greatest Common Denominator/Divisor/Factor (Geek Style – Euclidean Algorithm)

Shuffle a list C# (Fisher-Yates shuffle)

So, recently I was writing some code, and I was presented with a problem.  I had a List<CustomObject> in C#.  I needed to jumble that list up and still use those items.  Keeping in mind that I am not one of these prodigies that went to MIT, and I do not memorize algorithms for fun, I set about like most good programmers to Stack Overflow.  I came across this post which provided me this code: Continue reading → Shuffle a list C# (Fisher-Yates shuffle)

Offline Visual Studio 2017 Install

It is very easy to forget that many people have development environments that do not have internet access.  In this day and age, it’s easy to install frameworks and IDE’s from the web.  Recently I wanted to start using Visual Studio 2017 for a project.  My environment is completely isolated from the internet, and getting access is not feasible.  So how was I going to get Visual Studio 2017 from the internet to a computer with no internet access?  In the past this was easy, just download an iso and either burn it to a disc or just copy and mount the iso.  With Visual Studio 2017 Professional edition’s total download size is 27.1 GB. Continue reading → Offline Visual Studio 2017 Install