In the (relatively) distant past, MSTest was often used by organizations because it was provided by Microsoft “in the box” with Visual Studio/.NET. Because of this, some organizations trusted MSTest over open source testing frameworks such as NUnit. This was at a time when the .NET open source ecosystem was not as advanced as it is today and before Microsoft began open sourcing some of their own products.

Nowadays MSTest is cross-platform and open source and is known as MSTest V2, and as the documentation states: “is a fully supported, open source and cross-platform implementation of the MSTest test framework with which to write tests targeting .NET Framework, .NET Core and ASP.NET Core on Windows, Linux, and Mac.”.

MSTest V2 provides typical assert functionality such as asserting on the values of: strings, numbers, collections, thrown exceptions, etc. Also like other testing frameworks, MSTest V2 allows the customization of the test execution lifecycle such as the running of additional setup code before each test executes. The framework also allows the creation of data driven tests (a single test method executing multiple times with different input test data) and the ability to extend the framework with custom asserts and custom test attributes.

One problem when dealing with developer “secrets” in development is accidentally checking them into source control. These secrets could be connection strings to dev resources, user IDs, product keys, etc.

To help prevent this from accidentally happening, the secrets can be stored outside of the project tree/source control repository. This means that when the code is checked in, there will be no secrets in the repository.

Each developer will have their secrets stored outside of the project code. When the app is run, these secrets can be retrieved at runtime from outside the project structure.

One way to accomplish this in ASP.NET Core projects is to make use of the Microsoft.Extensions.SecretManager.Tools NuGet package to allow use of the command line tool. (also if you are targeting .NET Core 1.x , install the Microsoft.Extensions.Configuration.UserSecrets NuGet package).

Setting Up User Secrets

After creating a new ASP.NET Core project, add a tools reference to the NuGet package to the project, this will add the following item in the project file:

Build the project and then right click the project and you will see a new item called “Manage User Secrets” as the following screenshot shows:

Clicking menu item will open a secrets.json file and also add an element named UserSecretsId to the project file. The content of this element is a GUID, the GUID is arbitrary but should be unique for each and every project.

<UserSecretsId>c83d8f04-8dba-4be4-8635-b5364f54e444</UserSecretsId>

User secrets will be stored in the secrets.json file which will be in %APPDATA%\Microsoft\UserSecrets\<user_secrets_id>\secrets.json on Windows or ~/.microsoft/usersecrets/<user_secrets_id>/secrets.json on Linux and macOS. Notice these paths contain the user_secrets_id that matches the GUID in the project file. In this way each project has a separate set of user secrets.

The secrets.json file contains key value pairs.

Managing User Secrets

User secrets can be added by editing the json file or by using the command line (from the project directory).

To list user secrets type: dotnet user-secrets list At the moment his will return “No secrets configured for this application.”

To set (add) a secret: dotnet user-secrets set "Id" "42"

The secrets.json file now contains the following:

{
"Id": "42"
}

Other dotnet user-secrets commands include:

clear - Deletes all the application secrets

list - Lists all the application secrets

remove - Removes the specified user secret

set - Sets the user secret to the specified value

Accessing User Secrets in Code

To retrieve users secrets, in the startup class, access the item by key, for example:

One thing to bear in mind is that secrets are not encrypted in the secrets.json file, as the documentation states: “The Secret Manager tool doesn't encrypt the stored secrets and shouldn't be treated as a trusted store. It's for development purposes only. The keys and values are stored in a JSON configuration file in the user profile directory.” & “You can store and protect Azure test and production secrets with the Azure Key Vault configuration provider.”

There’s a lot more information in the documentation and if you plan to use this tool you should read through it.

Just because serverless allows us to quickly deploy value, it doesn’t mean that testing is now obsolete. (click to Tweet)

If we’re using Azure Functions as our serverless platform we can write our code (for example C#) and test it before deploying to Azure. In this case we’re talking about precompiled Azure Functions as opposed to earlier incarnations of Azure Functions that used .csx script files.

Working with precompiled functions means the code can be developed and tested on a local development machine. The code we write is familiar C# with some additional attributes to integrate the code with the Azure Functions runtime.

Because the code is just regular C#, we can use familiar testing tools such as MSTest, xUnit.net, or NUnit. Using these familiar testing frameworks it’s possible to write tests that operate at different levels of granularity.

One way to categorize these tests are into:

Unit tests to check core business logic/value

Integration tests to check function run methods are operating correctly

End-to-end workflow tests that check multiple functions working together

To enable effective automated testing it may be necessary to write functions in such a way as to make them testable, for example by allowing function run method dependencies to be automatically injected at runtime, whereas at test time mock versions can be supplied for example using a framework such as AzureFunctions.Autofac.

Reading additional blob content when an Azure Function is triggered can be accomplished by using an input blob binding by defining a parameter in the function run method and decorating it with the [Blob] attribute.

For example, suppose you have a number of blobs that need converting in some way. You could initiate a process whereby the list of blob files that need processing are added to a storage queue. Each queue message contains the name of the blob that needs processing. This would allow the conversion function to scale out to convert multiple blobs in parallel.

The following code demonstrates one approach to do this. The code is triggered from a queue message that contains text representing the input bob filename that needs reading, converting, and then outputting to an output blob container.

In the preceding code, there is a lot of blob access code (which could be refactored). This function could however be greatly simplified by the use of one of the built-in binding expression tokens. Binding expression tokens can be used in binding expressions and are specified inside a pair of curly braces {…}. The {queueTrigger} binding token will extract the content of the incoming queue message that triggered a function.

In the preceding code, the two [Blob] binding paths make use of the {queueTrigger} token. When the function is triggered, the queue message contains the name of the file to be processed. In the two [Blob] binding expressions, the {queueTrigger} token part will automatically be replaced with the text contents of the incoming message. For example if the message contained the text “File1.txt” then the two blob bindings would be set to names-in/File1.txt and names-out/File1.txt respectively. This means the input blob nameBlob string will automatically be read when the function is triggered,

When creating precompiled Azure Functions, bindings (such as a blob output bindings) can be declared in the function code, for example the following code defines a blob output binding:

[Blob("todo/{rand-guid}")]

This binding creates a new blob with a random (GUID) name. This style of binding is called declarative binding, the binding details are declared as part of the binding attribute.

In addition to declarative binding, Azure Functions also offers imperative binding. With this style of binding, the details of the binding can be chosen at runtime. These details could be derived from the incoming function trigger data or from an external place such as a configuration value or database item

To create imperative bindings, rather than using a specific binding attribute, a parameter of type IBinder is used. At runtime, a binding can be created (such as a blob binding, queue binding, etc.) using this IBinder. The Bind<T> method of the IBinder can be used with T representing an input/output type that is supported by the binding you intend to use.

The following code shows imperative binding in action. In this example blobs are created and the blob path is derived from the incoming JSON data, namely the category.

In additional to cloud-based offerings such as Microsoft’s Azure IoT Suite, it’s also possible to create representations of Internet of Things devices using the Actor Model and Akka.NET. For example, in addition to hosting an Akka.NET actor system in the cloud, you can also have them running on-premises; for example you may have an actor system monitoring an underground railway network for a city. In this case you may already have the infrastructure in place and decide that hosting in the cloud is unnecessary or risky, and deploy to servers geographically close to the underground with some disaster recovery/backup servers located elsewhere.

The Actor Model is a good fit for IoT scenarios due to the inherent concurrency controls, fault-tolerance, and performance/scalability. Akka.NET is a “toolkit and runtime for building highly concurrent, distributed, and fault tolerant event-driven applications on .NET & Mono”[1].

In the Actor Model, the smallest unit of computation is the actor. An actor can receive messages from other actors, perform computations, manage their state, and send messages to other actors.

For IoT scenarios, actors can model the system in a number of ways. For example, for every physical device in the real world there can be an actor instance to represent that device. This means you may have many actor instances, one for each physical device. When a sensor registers a new reading (temperature, proximity, pressure, etc.) this sensor can communicate with the actor responsible for it, the actor receives a message and can update its internal state to represent the latest reading. The actor representing the device can also respond to another type of message to access the last updated value. The device actor may also be responsible for sending messages to the physical device to update it’s configuration (for example) or this functionality may be broken down into its own actor definition depending on the exact requirements. The device actor may communicate with another actor that is purely responsible for handling the network interfacing required to communicate with devices. In this way if the “network actor” crashed and restarts the actors representing the device don’t crash and lose their state.

Groups of “device actors” can created with the supervision hierarchy that the Actor Model provides to protect groups of actors/devices from crashing other groups of actors/devices. These hierarchies can also provide device management semantics such as the registering of a new device that has just come online. Actors can also represent “abstract” concepts such as the creation of a dedicated actor to perform querying of groups of actors.

I grew up in humble surroundings, my family was for the most part “working class”, I moved around a bit as a kid, moved schools a few times, and lived in state/council housing. At one point as a child (due to some unfortunate circumstances) we lived for a short time in a “homeless” hostel – a transitional place whilst waiting for state housing to be allocated. In one area that I lived as a child I had a knife pulled on me outside a local shop, I learned then how quickly I could run! Today I live in a nice safe suburb, drive a decent car, and generally don’t have to worry too much about personal safety or not having a roof over my head. This is due to some kindnesses I’ve been shown along the way and also by investing. Investing in myself…

I recently completed reading Tony Robbins Money Master the Game, it is a good book for those new to investing - with a few chapters being somewhat US-centric. (Other books you may find interesting if you’re just starting out your investing journey include The Little Book of Common Sense Investing and A Random Walk Down Wall Street.) While Money Master the Game contains a lot of information about how to attempt to maximize your financial returns and ways to diversify your portfolio, in it Tony also talks about how you can add more value.

One way to improve your financial investments is by by investing in yourself.

One nice idea is that by investing in yourself you can add more value and if you can add more value you can earn more and if you can earn more you can invest more.

I had some help and kindnesses shown to me in my journey and like everyone I’ve also some challenges to deal with along the way. Even though I come from a somewhat humble background, and as a white heterosexual male I’ve never had to deal with prejudice, I am lucky that I have always loved to learn. I became fascinated by computers and programming from an early age and was lucky enough to borrow one for a time when I was younger. Eventually my interest and enthusiasm meant I was lucky enough to get my own machine.

Over the many years I continued to learn and was eventually privileged enough to be able to attend university to study computing. Even after starting my first job I continued to learn in my own time, in the evenings and at weekends, always interested in learning more.

As I look back now, at the time I was just following my natural curiosity, but looking back what I was really doing was investing in myself.

About 2 and a half years ago I stepped into a gym for the first time in my life. I look back now and smile, my first experience was not pleasant, I didn’t know what exercises to do, I tried bench pressing with an empty bar and wobbled all over the place, while the muscular guy next to be hoisted 50kg dumbbells to the sky. I went home feeling awful and a little stupid. Two days later I went back, and I kept going back. I devoured Arnold Schwarzenegger's Encyclopedia of Modern Bodybuilding and eventually paid for some personal training sessions to learn how to clean and press and bench press properly. Whilst I am not a shredded muscular bodybuilder, I did lose 14kgs over 2 years and add some amount of muscle mass and some strength. This is another example of investing in you, this time the physical you. Oftentimes, as developers we don’t always take the best care of ourselves, but I believe investing in the physical you carries over to the work/business you.

As the adage goes, "if you want better answers, ask better questions". One question I’m asking myself this year is: how can I continue to add more value than anyone else? As a software developer and “techie-minded”, in the past I would have thought of a question like this as being big-headed or management-speaky. But if you want to help others you need to help yourself and if you want to help yourself you need to offer value to others.

If you want better answers, ask better questions

It’s good to take a step back sometimes and ask ourselves some questions, especially as we get laser focused on the test we’re writing or the feature we’re working on or the sprint that we’re in, or the next project that might be coming along.

I’m grateful for the opportunities I’ve been given in life, I’m grateful for the challenges and failures and what I’ve learned from them, and I’m grateful for the gift of my lifelong love of learning.

Whilst somewhat dramatic, there is some truth to the phrase “if you’re not growing you’re dying” and if you want to grow you have to invest in you.

With over 15 years experience, Jason Roberts is a former 5-time Microsoft .NET MVP, freelance developer, writer, and Pluralsight course author. He has written multiple books and is an open source contributor. In addition to enterprise software development, he has also designed and developed both Windows Phone and Windows Store apps.