Skip to main content

Posts

Showing posts from August, 2019

Visual Studio 2019 - The mystery of ‘Run selected code locally’

When I right clicked on some code in Visual Studio 2019, I noticed a new ‘Run selected code locally’ menu item at the top. Clicking on it doesn’t do anything(at least on my pc). Who can tell me what this does?

XUnit - Testing event handlers

Testing event handlers in XUnit can be done through the Assert.Raises method. This method expect 3 parameters: An action of EventHandler<T> to attach a handler An action of EventHandler<T> to detach the handler An action containing the code that should trigger the event This was not immediately clear to me so here is a (simple) example: And here is the interface of the IUnitOfWork that is tested above:

EF Core - Structure your mapping configurations

I typically use the Fluent API to configure my Entity Framework (Core) model. I think it gives me the most control without polluting my domain model with EF Core specific attributes. You can do this directly inside the OnModelCreating method inside your DbContext but this can get out-of-control quite fast if you have a lot of objects to be mapped. A better alternative is to use the IEntityTypeConfiguration<> interface together with the ApplyConfiguration method:

GraphQL - All errors are equal, but some errors are more equal than others.

The GraphQL specification gives you one way to handle errors, but typically you have in your application different type of errors. (e.g. validation errors, technical errors, …) Not all these errors should be handled in your application in the same way. Sasha Solomon shared some interesting ideas on how they did error handling this at Medium:

Azure DevOps - Pull requests - Build expired

I made a pull request in Azure Devops that had some policies. The code was reviewed and the build succeeded but  I didn’t had time to complete the Pull request. Later when I came back I noticed that the Build was expired and  therefore I couldn’t complete the Pull request anymore. Build Expiration is a part of the build branch policies . It makes sure that updates to your protected branch don't break open pull requests. You can choose between: Always require a new build Require a new build if older than ... hours Don't require a new build If the build expired, you can retrigger it by clicking on the … next to the Build expired message and choosing Queue build:

XUnit–Roslyn analyzers

I recently started using XUnit . I’m still discovering what is possible using this unit test framework. XUnit has built-in support for parameterized unit tests through the [Theory] and [InlineData] attributes. Nothing special there and similar to what other unit testing frameworks have to offer. But what makes this really nice in XUnit is that when you install the XUnit NuGet package , you get some Roslyn analyzers installed as well. These analyzers will validate your parameterized unit tests and return errors when the [InlineData] parameters match don’t match method's parameters:

XUnit–Writing test output

I recently started using XUnit . I’m still discovering what is possible using this unit test framework. One of the things I had to discover was how to write log messages during my tests.  I could fall back to using Trace.WriteLine or Console.WriteLine but that is not the best solution. XUnit allows you to capture test output using a special interface called ITestOutputHelper . In order to use this, just add a constructor argument for this interface and store it inside a variable inside your test class: Now if you call the ITestOutputHelper.WriteLine method, the output is captured and available inside your test output:

XUnit–Test lifecycle

I recently started using XUnit . I’m still discovering what is possible using this unit test framework. Before I was using NUnit, although I really liked it I always forgot what exact attribute I needed to control the lifecycle of my tests. I had to look up in the documentation if I needed a SetupFixture , a OneTimeSetup or a Setup attribute. In XUnit there is a lot less magic going on and you can fallback to standard .NET idioms like the usage of the constructor and the IDisposable.Dispose() method. With these 2 you can already get quite far, but if necessary you can also use Class Fixtures : shared object instance across tests in a single class Collection Fixtures : shared object instances across multiple test classes

.NET Conf 2019 is coming!

Mark September 23 until 25 in your agenda’s as .NET Conf 2019 is coming. This year you certainly don’t want to miss out the event as .NET Core 3.0 is launched at .NET Conf 2019. Can’t wait…

Stack-trace-formatter - Pretty print .NET stack traces

We all got this problem before. We got a stack trace copied from somewhere but due to a lack of formatting it’s almost impossible to understand what’s going on. Especially if you want to copy/paste a stack trace inside a blogpost it becomes kinda ugly. The guys from Elmah created a free online stack trace formatter , a small tool that allows you to pretty print any .NET stack trace and either copy or download the nicely formatted stack trace. Nice!

Azure DevOps–Reindexing

The search experience in Azure DevOps(Server) is powered by ElasticSearch. If for any reason your search didn’t seem to work, a lot of Powershell scripts are available to help you with maintaining, updating and monitoring the health of your ElasticSearch instance. Remark: Be sure to take the correct scripts for your version of Azure DevOps. One of the scripts I use the most is the TriggerCollectionIndexing.ps1 script. This script allows you to reindex everything(wiki, workitem, code) at the collection level. Remark: You have to run this script on a server that has the SQL Server Client Tools installed. Let’s try it out: PS N:\Azure_DevOps_Server_2019> .\TriggerCollectionIndexing.ps1 cmdlet TriggerCollectionIndexing.ps1 at command pipeline position 1 Supply values for the following parameters: (Type !? for Help.) SQLServerInstance: mytfssqlinstance CollectionDatabaseName: tfs_defaultcollection ConfigurationDatabaseName: tfs_configuration Co

Azure DevOps - Agent Auto-provisioning

When you create a new Agent Pool in Azure DevOps, you get the following options that can be selected: Auto-provision corresponding agent pools in all projects Allow all pipelines to use this pool   The second option is kind-of self explaining but what does the first option do? What’s important to understand is that an agent pool exists at multiple levels, you have one at the organization level and you have one at the project level. The project level pool is not really a new pool but rather an import of the organization level agent pool into your team project. Your Azure pipelines will always target the project level pool. By selecting the ‘Auto-provision corresponding agent pools in all projects’ checkbox(which is checked by default) the organization agent pool is imported in all your team projects and is accessible there immediately. If you didn’t set this checkbox, you have to import the organization agent pool yourself. Therefore go to Project Settings > Agent

Cloud Design Patterns

This is the poster that every Cloud Architect should hang above his bed: Cloud Design Patterns: Prescriptive Architecture Guidance for Cloud Applications

Microsoft Orleans–Integrate with your favorite IoC container

As mentioned in a previous post , it is not that hard to start using dependency injection with Orleans. But what if you don’t want to use the built-in IoC container? To switch the IoC container instance you can call the UseServiceProviderFactory() method exposed by the SiloHostBuilder. Here is an example that uses StructureMap:

Microsoft Orleans–Dependency Injection

Most .NET(Core) applications today use any kind of inversion of control. This is not different for a Microsoft Orleans application. It leverages the ASP.NET Core built-in IoC container to allow you to inject dependencies in your grains: This sounds really easy and in fact it is. The only thing you need to be aware of is that the Orleans application is NOT re-using the (ASP) .NET Core IoC container but uses it’s own instance. This is something I didn’t know and caused me a lot of headaches before I figured out the reason. Microsoft Orleans is using similar concepts as ASP.NET Core but is not using the same builder. There are plans to integrate it directly when .NET Core 3.0 is released but at the moment of writing, you have to use a specific OrleansBuilder: Important to notice in the code above is that the ConfigureServices() method you call here exposes a different IServicesCollection instance as the one used by ASP.NET Core.