Compiling .NET projects in Sublime Text
The subject of this post is the compilation of .NET projects and solutions in Sublime Text using the Build Systems feature.
Sublime Text supports custom Build Systems.
By adding MSBuild as a new Build System, it is possible to build Visual Studio projects and solutions without the Visual Studio IDE installed.
Adding the Source Code
The following steps require a folder with MSBuild project files. In order to be pragmatic, the custom Build System is going to compile the source code of AutoFixture.
Go to File menu, click Open Folder... and select the root folder of the project to be compiled.
Adding the new Build System
Go to Tools menu, Build System, and click New Build System...
Paste the following code to a file:
<pre><code class='json'>{
"cmd": ["C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\MSBuild.exe", "AutoFixture.sln"],
"working_dir": "${project_path:${folder:${file_path}}}\\Src"
}
Save the file as AutoFixture.sublime-build.
Go to Tools menu, Build System, and select AutoFixture.
The above steps are per MBSuild project file. Repeat the above steps by creating a Build System per MSBuild project file.
Running the Build
Go to Tools menu, and select Build. Alternatively, use the Ctrl+B command.

In the above screenshot, on line 38 a semicolon has been intentionally removed for the demo. After running a build, the compiler detects the missing semicolon.
What about the Tests?
Currently, the easiest way to run the tests is by using a command-line interface version of the test runner.
On my machine, attempting to add a new Build System for the tests resulted in high memory usage. This has been tested while Sublime Text 3 was in beta (Build 3010).
Commit to Revision Control with Care
We should always commit to revision control with care.
Often, developers provide commit messages that are incomplete, lowercase, with orthographic permutations. Even worse, snapshots can contain code that doesn’t even compile.
In a signed document the rules and regulations can be altered and then another document is going to be created and signed, but the original document will always remain signed.
As signatures in documents can’t be erased so do recorded snapshots, at least it is not always an easy task.
After a snapshot has been recorded it acts like a signature for the developer – should we ever put our signature roughly?
ReSharper - The Good Parts (Static Code Analysis)
What I really like about ReSharper is the Static code analysis, and the quick-fixes. What I really don’t like is pretty much everything else.
Keeping only the Static code analysis
Go to ReSharper menu and clik Options…
In the Environment > General tab uncheck:
- Show tips on startup
- Loop selection around ends of a list
- Show managed memory usage in status bar

In the Environment > Keyboard & Menus tab select:
- Hide overridden Visual Studio menu items (from Menus & Toolbars)
- None (from Keyboard Shortcuts)

In the Environment > Editor tab uncheck:
- Highlight current line
- Auto-format on semicolon brace
- Auto-format on closing brace
- Use CamelHumps
From Braces and Parentheses uncheck:
- Highlight matching delimiters when care is ..
- Auto-insert pair brackets, parentheses and quotes
- Auto-insert closing brace

In the Environment > IntelliSense > General tab select:
- Limited ReSharper IntelliSense and uncheck C#

Uncheck everything in the IntelliSense > Completion Behavior tab.

In the Environment > IntelliSense > Completion Appearance tab select:
- Visual Studio IntelliSense font
- Uncheck everything else

In the Environment > IntelliSense > Parameter Info tab select:
- Arrow keys
- Uncheck everything else

Enable Visual Studio IntelliSense
Since we customized ReSharper to use Visual Studio IntelliSense, we have to manually enable it from Visual Studio options.
In Visual Studio, go to Tools menu and click Options…
In the Text Editor > C# tab select:
- Auto list members
- Parameter information

Suspending ReSharper
There are some projects where I don’t even want to have ReSharper’s Static code analysis and the quick-fixes.
In Visual Studio, go to Tools menu and click Options…
In the ReSharper tab click Suspend.

Empty ASP.NET Web API Project Template
Usually, every time you create a new Web API project you:
- Delete almost all the generated code.
- Uninstall NuGet packages that you don’t need (yet, or at all).
- Remove unused assembly references.
- Manually edit the
Web.configfile(s) to remove the elements that point to the assemblies which have been removed.
Solution
Use the Empty ASP.NET Web API Project Template. The Visual Studio extension can be downloaded from here.
It will add a new project template Empty Web API which includes the following:
Files
- Properties\AssemblyInfo.cs
- favicon.ico
- Global.asax
- Web.config
Assemblies
- System
- System.Core
- System.Configuration
- System.Net.Http
- System.Web
- System.Web.Abstractions
- System.Web.ApplicationServices
- System.Web.Mvc
- System.Web.Routing
- System.Xml
NuGet packages
- Microsoft.AspNet.WebApi.Client
- Microsoft.AspNet.WebApi.Core
- Microsoft.AspNet.WebApi.WebHost
Microsoft.AspNet.Providers.Core- Microsoft.Net.Http
- Newtonsoft.Json
Remarks
The Newtonsoft.Json package is included because it is lazy loaded inside the System.Net.Http.Formatting.JsonMediaTypeFormatter type.
If you completely remove the Newtonsoft.Json assembly a System.IO.FileNotFoundException exception is thrown with message “Could not load file or assembly ‘Newtonsoft.Json, ..’ or one of its dependencies. The system cannot find the file specified.”
A favicon.ico file is included because the browser requests it so it’s better not to respond with a 404 Not Found.
Exercising the SUT asynchronously
In unit testing, there are times were the SUT has to be exercised asynchronously.
How can we wait for the exercise to complete execution?
- An instance of the SUT can be created on the main thread.
- The main (waiting) thread spins in user mode while starting the exercise of the SUT asynchronously.
- Once the result has been received, the execution continues on the main thread.
- The assertion takes place.
The SpinWait synchronization type contains a method named SpinUntil which works perfect for the described scenario.
<pre><code class='csharp'>// Fixture setup
var sut = new ObjectLocalStorage(); sut.Set(@object, expected); object result = null;
// Exercise system new Task(() => result = sut.Get(@object)).Start(); SpinWait.SpinUntil(() => result != null);
// Verify outcome Assert.Equal(expected, result); // Teardown
- Once the
Taskhas been created it is immediately scheduled for execution by calling theStartmethod. - As long as the unit test runs fast, the waiting thread spins in user mode, which is a good thing.
There is also a SpinUntil overload accepting a TimeSpan timeout.
Truly Constrained Non-Deterministic Numbers in AutoFixture
Numbers in AutoFixture are currently created using a strictly monotonically increasing sequence.
<pre><code class='csharp'>var fixture = new Fixture();
var i = fixture.CreateAnonymous<int>(); // Prints –> 1 var l = fixture.CreateAnonymous<long>(); // Prints –> 2 var f = fixture.CreateAnonymous<float>(); // Prints –> 3.0
Starting with version 2.13.0, by applying a specific customization numbers can also be created using a constrained non-deterministic sequence. The new customization is called RandomNumericSequenceCustomization.
<pre><code class='csharp'>var fixture = new Fixture()
.Customize(new RandomNumericSequenceCustomization());
var i = fixture.CreateAnonymous<int>(); // Prints –> 122 var l = fixture.CreateAnonymous<long>(); // Prints –> 38 var f = fixture.CreateAnonymous<float>(); // Prints –> 147.0
Once the customization has been applied to a Fixture instance subsequent requests for numeric types will yield random non-repeatable numbers in the range of [1, 255]. When requesting more than 255 numbers the range is automatically changed to [256, 32767] and so on.
The default ranges are [1, 255], [256, 32767], and [32768, 2147483647].
Supplying a custom range
To supply a custom range, customize an instance of the Fixture class with an instance of the RandomNumericSequenceGenerator and pass to its constructor a sequence of integer numbers (e.g. -100, 100, 255).
The sequence must be two positive or negative numbers optionally followed by a series of greater numbers.
<pre><code class='csharp'>var fixture = new Fixture();
fixture.Customizations.Add(
new RandomNumericSequenceGenerator(-100, 100, 255));
var i = fixture.CreateAnonymous<int>(); // Prints –> -95 var l = fixture.CreateAnonymous<long>(); // Prints –> 47 var f = fixture.CreateAnonymous<float>(); // Prints –> -82.0
After applying the customization, numbers are now created in the range of [-100, 100]. However, when requesting more numbers than the range size the range is automatically changed to [101, 255].