Moq Quick Reference for Rhino Mocks Users

Behold! A quick reference guide for Rhino Mocks (AAA syntax) users making the switch to Moq.

Rhino MocksMoq
MockRepository.GenerateMock<T>new Mock<T>
MockRepository.GeneratePartialMock<T>new Mock<T> { CallBase = True }
Arg<T>.Is.Equal(foo)It.Is<T>(x => x == foo)
.AssertWasNotCalled(…).Verify(…, Times.Never)

In addition to the syntax translations listed in the table above, a key difference is that you use the instantiated mock object via the mock’s Object property. For example:

var mock = new Mock<SomeClass>();

Parse Emails With Power Automate

Last week, I wrote about how to use Power Automate to add automation to everyday tasks like emails. One of the examples I gave was to notify myself when vacation requests arrive so I can approve them as quickly as possible. Emails like these frequently have links like “click here to view the request” that take you to the item needing attention. It sure would be nice if we could extract that link and do something useful with it, right?

Regular expressions are a great way to parse text like this to extract useful details. Unfortunately, Power Automate doesn’t seem to have native support for regular expressions, but there are connectors from a third-party provider Plumsail. I took it for a test run today, and it worked pretty well. So, let’s take a look at the steps.

In order to use the Plumsail connectors, you’ll need a Plumsail account & API key. You can register for an account here, and create an API key here. Keep your API key handy, and head over to Power Automate.

In my workflow, I added the Plumsail Regular Expression Match connector as a new action after my “When a new email arrives” trigger action. If it’s the first time using a Plumsail connector, it will prompt you for your API key and a connection name.

After you’ve specified your connection name & API key, the connector UI changes to what you’d expect for a regular expression: Pattern & Text. The output of this control is a collection of matches, and the dynamic content will adjust based on the capture groups (parentheses) specified in your pattern.

In the screenshot above, my regular expression will search the email body for the “please click here” hyperlink and capture the URL. I can then dump this URL into the message to myself in Slack. Works great!

Build a Linux .NET Core Dev Machine in Under an Hour

I have a complicated relationship with Linux. Every few years, I get nostalgic about my Solaris days back in college. So I build a machine, struggle through getting all the hardware & whatnot working, and invest tons of time trying to get the Windows apps I know & love running on it. Then I give up and go back to Windows.

And that’s about where I’m at now, in the nostalgia/fascination stage with fantasies of leaving my familiar Microsoft pastures. At work, we’re moving more & more toward containerization and Kubernetes with the goal of reducing cost by hosting services on Linux. Maybe its relevance to work will give the effort more legs this time?

So I embarked on the journey (again) and have been pleased with how much the experience has matured. Doing a clean install of Ubuntu on my old laptop was completely painless. I didn’t need to do any special setup or troubleshooting to get hardware running. In fact, it was 100% as easy as installing Windows, which I was forced to do a few weeks earlier presumably due to some botched updates that had me blue-screening every few minutes.

Keeping the momentum going, I jumped right into installing .NET Core and setting up Visual Studio Code. The whole thing–from beginning to end; from formatting and installing the OS to running my first console app–took about an hour.

It was easy enough that just about anybody capable of doing a clean OS install should be able to manage it. In fact, let’s go through all the steps, right now! I’ll walk you through what’s needed from installing the OS, .NET Core, and VS Code all the way through to creating and running a new project.

Getting the operating system up & running is obviously step 1. I followed the instructions on the Ubuntu website:

  1. Create a bootable USB drive
  2. Boot off the device
  3. Follow install directions

Alternatively, you could use a virtual machine. Since I’m retracing my own steps as I write this, I’m using VirtualBox which is freely available to everybody–so no excuses about not having a spare computer laying around! If you’re going this route, download VirtualBox, fire it up, and create a new virtual machine. For reference, I picked Linux/Ubuntu (64-bit) and used all defaults*. You still need to download the Ubuntu ISO, and select it as the boot media the first time you start the VM.

*My VM kept hanging during install until I increased memory from the default 1024 MB to 2048 MB.

Alright, now we’ve got our Linux machine running, and it’s time to install .NET Core. Follow Microsoft’s instructions to install the .NET Core SDK, or open a terminal and run the following commands:

wget -q -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt-get update
sudo apt-get install apt-transport-https
sudo apt-get install dotnet-sdk-3.1

We’re almost there now. Install Visual Studio Code along with the C# extensions by running a couple more commands:

sudo snap install --classic code
code --install-extension ms-vscode.csharp

That’s it! Who’s ready to run some code? Use the dotnet CLI to create a new console app, open the code in VS Code, and run it using F5 or Debug > Start Debugging. (If it’s your first run with the C# extensions installed, you may need to wait while it downloads dependencies.) VS Code will ask you which environment to use; pick .NET Core to add a launch.json file which tells VS Code how to run your app.

dotnet new console -o HelloConsole
cd HelloConsole
code .

And there you have it. Reproducing the setup on a VM, I was able to download the Ubuntu ISO, create a clean install, install .NET Core SDK & runtime along with VS Code, and create & run a new C# console app in under an hour. Not bad!

@mentions in Slack From Microsoft Power Automate

In my last post, I demonstrated how to create a basic workflow that posts to Slack when email is received that matches specified criteria. Pretty cool, right? Sometimes you want to make sure that somebody gets a notification about these important messages that you’re posting, though. Slack has a built-in way to do this with its mentioning system.

Unfortunately, mentioning a user or group from Power Automate isn’t as straightforward as you might expect. When you specify message text like @somegroup it just shows up in Slack as that text.

The reason it doesn’t work is because the text isn’t parsed to use special formatting required by Slack for things like mentioning. The good news is that the workflow widget will do this work for you if you enable Parse Mode=full in the advanced options.

The bummer about using Parse Mode=full is that you can’t include other object entities e.g. an email object’s subject. Enabling full parsing gives us a great hint at the solution if we look at the run results…

So if we know the “special formatting” values, we can use those in messages? Yep!

It’s not a perfect solution because there doesn’t seem to be a good, built-in way to retrieve the special formatting for a Slack user or group required to @mention them from Power Automate, but it does give you a couple ways to accomplish the goal of mentioning them in messages you post. You can enable full parsing in advanced options to mention from a static text message, or determine the special formatting for a user or group yourself to use in a dynamic message constructed from other entities in your workflow!

Everyday Automation With Microsoft Power Automate

One of my favorite tools these days is Microsoft Power Automate (formerly Microsoft Flows). Power Automate lets you visually construct workflows for all kinds of things, and I’ve found it to be an incredibly powerful way to automate tasks. Here are some examples:

  • Post in Slack when an email is sent to a shared mailbox
  • Post status tweets from a SaaS provider’s Twitter account in Slack
  • Send an SMS text message when a critical process fails

The thing I love most about Power Automate is that you can make sophisticated workflows very quickly, and they don’t require hosting or deployment. You just create, save, and it’s live.

Many of my workflows start by me realizing that I didn’t notice a certain category of emails quickly enough or that I need to give my team better awareness of an event. For example, I wanted to ensure that expense reports get approved right away so that employees get reimbursed, so I created a workflow to send a direct message in Slack whenever I get an approval request email from the expense system. Another common example has been to notify my team when applications report failures through various means.

Power Automate boasts a “low code” experience, but most of the workflows I’ve created have required coding skills and creative troubleshooting to figure out. That said, you can still do useful things with pretty much zero code as long as you’re not trying to get too fancy. In this post, we’ll build a simple workflow that posts a notification to Slack when an email comes in.

We begin by creating a new automated workflow. Automated workflows always start with a trigger, so I’m going to pick “When a new email arrives.” Another option that’s been useful to me is “When an email arrives in a shared mailbox,” but as you see in the screenshot below, there are many options to explore–and many that have nothing to do with email, as well.

If you’re automating something for an unmonitored mailbox, you may not want additional criteria, but I’m usually looking for something like an email from a specific sender with a certain subject. So, the next step in our workflow will be a Condition so we can specify which criteria we care about.

The last step in our workflow will be to define what happens when the condition is met or not met. We want our sample workflow to send a private message in Slack. To accomplish this, I’ll add the “Post message to Slack” step and enter my Slack username for the channel and some text for the message. Instead of hard-coded message text, you could also include attributes from the email message entity, similar to those used in the condition step.

The final step is simply to save it. That’s it. When an email arrives, the workflow checks it against the conditions you’ve defined, and performs the corresponding Yes/No actions–in this case send ourselves a message in Slack.

Clean-up Your Inbox With Retention Policies

I get a lot of email, and I can’t always keep up with it. My first line of defense is rules. Rules help me filter out a lot of noise and tag messages in meaningful ways, but I find that if I move things out of my inbox, they slip into the out-of-sight, out-of-mind void that is everywhere-except-my-inbox.

I get a lot of notifications from Azure DevOps for things like pull request reviews. These are things that I want to know about, but it’s not something I’m realistically going to get back to if I put it off for a few days. I wanted to create a rule that was something like “delete messages that match [whatever criteria] that are more than [x] days old,” but I couldn’t find a way to do it other than by moving the messages away from my inbox to a folder with a custom archiving policy (i.e. into the void where I won’t see them) or by doing some custom development.

But then I found them: retention policies.

Retention policies control how long messages messages are kept, and they’re perfect for these sorts of highly-relevant-but-only-for-a-short-while messages. You apply the policy to a message, which can be done quite easily with inbox rules, and Office 365 takes care of the rest.

Sounds awesome; sign me up, right? It took me an embarrassingly long time to figure out how to add the policies to my account and use them. I’m talking 2 or 3 sessions where I gave up, defeated. Fear not, though, friends! For I have figured out the path forward and present it to you here in just four easy steps.

1-3. Add Policies to Your Account

This was the part that escaped me for so long, and the issue is that I was trying to do it from Outlook instead of through the Outlook Web App.

  1. Log into O365 > Outlook
  2. Settings > View all Outlook settings > Retention Policies
  3. Add new policy > (add some policies)

Alternatively, you can jump directly to the following URL:

4. Make a Rule

Now that you’ve added some retention policies, you can apply them to messages with rules. As mentioned earlier in the post, I want to delete messages from Azure DevOps after a few days, so I created a rule that applies the “1 Week Delete” policy to them.

When these messages come in, they can sit comfortably in my inbox and compete for my attention. If I happen to get a behind–which happens frequently enough for me to have pursued this several times and then write a blog post about it–these stale messages will conveniently self-destruct and go away all by themselves. Wonderful!

Blogging Makes You A Better Developer

Throughout my career, I’ve found that I’d figure things out, and several months or years later, I’d bump into someone else that was trying to do something similar, and I’d think or say: “Yea, I can’t remember the specifics, but I definitely remember dealing with that.” That was the original motivation for starting my blog: to build a personal catalog of things I’ve learned that I could refer back to should the things become relevant again.

It started as a bit of a hobby but eventually became part of my weekly professional life. I’d research my assignments, and I’d track the journey. Once I finished, I’d organize my list of steps into an article and publish it. The more I wrote, the more views my blog would see, and it became exciting to watch traffic grow and track statistics.

I was reading an article about the Feynman Technique and realized that this is similar to how I approached writing.

  • Start with a concept: a problem I’m trying to solve for an assignment
  • Teach it to a toddler: figure it out/make it work
  • Identify gaps: are there things I don’t understand about the solution or aspects that I am not able to articulate?
  • Review & simplify: write an article that’s concise and easy to understand

I was having a lot of fun transforming my work into articles and watching daily traffic grow, but what I didn’t realize was that this activity was actually giving me tremendous professional growth in a number of ways.

Writing Skills

Writing is an extremely underrated skill for software developers. Between email, team chats, direct messages, and documentation, I’d venture to say I spend more time writing than I do coding. In the digital age, I’m often represented by the things I type much more than the things I say or do.

The more important part of writing is how it affects people around you, though. Good writing is essential to sharing and communicating your ideas. It allows you to explain concepts to your bosses, teammates, and clients/customers. Maybe it allows a colleague to understand your perspective and snap to your approach, or maybe it helps them explain back to you where you’re wrong. It might enable you to implement a cool new feature or prevent the team from wasting time on something that’s not needed. Good things happen when you articulate thoughts effectively.

Technical Expertise

This is the big advantage that I didn’t realize until more recently. I thought I was learning things and just documenting them by writing articles, but the exercise of sharing it with the world forced me to take extra steps. I needed to fact-check claims I was making and double-check assertions. This correlates to the “identify gaps” step of the Feynman Technique. The act of publishing things I thought I knew forced me to make sure I actually knew them as best I could.

Similarly, knowing things a little better and having “practiced” explaining it makes you an excellent resource for your team. You know more things, you know them well, and you know how to explain them.

Shareable/Referenceable Content

My original goal! It’s awesome when somebody needs something, and you can just give them a link instead of explaining. It’s also fun when a co-worker’s researching something and they find you, which has happened a couple times. Sometimes you even turn up as an answer to your own questions!

Personal Brand

This is particularly good advice for people early in their career. If I get a resume from somebody with a blog that’s been active for several years, I get excited. This is someone that values learning and communication. This is someone that wants to share ideas and help others. This is someone that I think can provide a lift to the team.

Of course, if I look at the blog and it’s poorly written or has a lot of mistakes, it might work against you. This is where it’s important to follow the process. Start with your idea. Get it written. Fill the gaps. Simplify. With diligence and time, you’ll have lots of great content that represents you well.

Two Ways to Send Emails to Slack

I get a lot of emails where I want to discuss with my team in Slack, and I was really excited when the Send to Slack Outlook add-in was created. It’s really nice to click a button, specify a channel, and have the email show up.

Unfortunately, I noticed yesterday that things like screenshots & images contained in an email message may not show up when the add-in is used. However, just today I happened upon another setting in Slack preferences that allow you to generate a unique email address. When you send emails to this address, they come as a direct message from Slackbot–and images that didn’t show up in emails sent with the Outlook add-in did show up when I sent them to this email address.

The setting is found in Preferences > Messages & Media > Bring Emails into Slack.

Database-First Entity Framework in .NET Core

.NET Core and Entity Framework make connecting to an existing database really easy. This post will demonstrate how to generate models for an existing database using .NET Core Entity Framework. Note that you must have the .NET Core SDK installed.

First, let’s create a new console application. Open a new terminal and run the following commands:

/> dotnet new console -o MyConsoleApp
/> cd MyConsoleApp

If your app is using ASP.NET Core 2.1+, you’ll get all the Entity Framework packages you need. However, our console app is using .NET Core (not ASP.NET Core), so we need to install a couple more packages. Run the following commands:

/> dotnet add package Microsoft.EntityFrameworkCore.Design
/> dotnet add package Microsoft.EntityFrameworkCore.SqlServer

Now we’re ready to create our models. Again, we’ll do this by running a command in the terminal. Run the following command to generate models for all tables in your database along with a DbContext.

/> dotnet ef dbcontext scaffold "<connection string>" Microsoft.EntityFrameworkCore.SqlServer -o Models

Need help obtaining the connection string? If you’re using SQL Azure, you can browse to the database in the Azure Portal, and click the Connection Strings link.

With your DbContext and models created, you should now be able to write code to access tables. Add the following lines to your console app’s Program.cs:

using System;
using System.Linq;
using MyConsoleApp.Models;

namespace MyConsoleApp
    class Program
        static void Main(string[] args)
            Console.WriteLine("Hello World!");

            using (var context = new YourDatabaseNameContext())
                var foo = context.SomeTable.First();

One-Step React App with .NET Core Web API Backend

Last week, I wrote a short article demonstrating how to create a React app using Create React App and hook it up to a .NET Core Web API created by dotnet new webapi. Dotnet new also has a react template, though, which allows you to create a new React app with .NET Core web API backend in a single step.

/> dotnet new react -o my-app
/> cd my-app
/> dotnet run

Note! The front-end client and backend API still run as separate servers, so you must still specify the proxy in the React app. Perform the following steps to do this:

  1. Open package.json in the /ClientApp folder
  2. Add "proxy":"https://localhost:5001"
  3. Save & restart your app
  4. Verify API communication by viewing the Fetch Data page

There are some differences between the two approaches. Create React App creates a better-looking but more-barebones site. Dotnet new starts you with a more complex React site with basic navigation and multiple pages.