Taeguk.co.uk Dave Shaw's Development Blog https://taeguk.co.uk/ Tue, 24 Jul 2018 13:29:11 +0000 Tue, 24 Jul 2018 13:29:11 +0000 Jekyll v3.7.3 You might not be seeing exceptions from SQL Server <p>This post describes a problem I noticed whereby I wasn’t seeing errors from my SQL code appearing in my C#/.NET code.</p> <p>I was recently debugging a problem with a stored procedure that was crashing. I figured what caused the stored procedure to crash and replicated the crash in SQL Management Studio, but calling it from the application code on my development environment didn’t throw an exception. What was even stranger was that the bug report <em>was</em> from an exception thrown in the C# code, I had the stack trace to prove it.</p> <p>After a bit of digging through the code, I noticed a difference between my environment and production that meant I wasn’t reading all the results from the <code class="highlighter-rouge">SqlDataReader</code>.</p> <p>The C# was something like this:</p> <div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">var</span> <span class="n">reader</span> <span class="p">=</span> <span class="n">command</span><span class="p">.</span><span class="nf">ExecuteReader</span><span class="p">();</span> <span class="k">if</span> <span class="p">(</span><span class="n">someSetting</span><span class="p">)</span> <span class="c1">//Some boolean I didn't have set locally.</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">reader</span><span class="p">.</span><span class="nf">Read</span><span class="p">())</span> <span class="p">{</span> <span class="c1">//reading results stuff.</span> <span class="p">}</span> <span class="n">reader</span><span class="p">.</span><span class="nf">NextResult</span><span class="p">();</span> <span class="p">}</span> </code></pre></div></div> <p>Changing <code class="highlighter-rouge">someSetting</code> to <code class="highlighter-rouge">true</code> in my development environment resulted in the exception been thrown.</p> <h2 id="whats-going-on">What’s going on?</h2> <p>The stored procedure that was crashing looked something like this:</p> <div class="language-sql highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">create</span> <span class="k">procedure</span> <span class="n">ThrowSecond</span> <span class="k">as</span> <span class="c1">--Selecting something, anything</span> <span class="k">select</span> <span class="n">name</span> <span class="k">from</span> <span class="n">sys</span><span class="p">.</span><span class="n">databases</span> <span class="n">raiserror</span> <span class="p">(</span><span class="n">N</span><span class="s1">'Oops'</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span> <span class="c1">--This was a delete violating a FK, but I've kept it simple for this example.</span> </code></pre></div></div> <p>It turns out that if SQL raises an error in a result set other than the first <strong>and</strong> you don’t try and read that result set, you won’t get an exception thrown in your .NET code.</p> <p>I’ll say that again, there are circumstances where SQL Server raises an error, and you will not see it thrown in your .NET Code.</p> <h2 id="beware-transactions">Beware transactions</h2> <p>The worst part of this… if you are using transactions in your application code, e.g. using <code class="highlighter-rouge">TransactionScope</code>, you will not get an exception raised, meaning nothing will stop it calling <code class="highlighter-rouge">Complete</code> and committing the transaction, even though part of your operation failed.</p> <div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">void</span> <span class="nf">Update</span><span class="p">()</span> <span class="p">{</span> <span class="k">using</span> <span class="p">(</span><span class="kt">var</span> <span class="n">tx</span> <span class="p">=</span> <span class="nf">TransactionScope</span><span class="p">())</span> <span class="p">{</span> <span class="nf">DeleteExisting</span><span class="p">();</span> <span class="c1">//Delete some data</span> <span class="nf">InsertNew</span><span class="p">();</span> <span class="c1">//Tries to save some data, but SQL Errors, but the exception doesn't reach .NET</span> <span class="n">tx</span><span class="p">.</span><span class="nf">Complete</span><span class="p">();</span> <span class="p">}</span> <span class="p">}</span> </code></pre></div></div> <p>In the above hypothentical example if <code class="highlighter-rouge">InsertNew()</code> happens to call a stored procedure like before and is using C# like in the previous examples. It <strong>will</strong> delete the existing entry, but <strong>will not</strong> insert a new entry.</p> <h2 id="when-does-it-happen">When does it happen?</h2> <p>To figure out when this does and doesn’t happen I wrote a number of tests.</p> <p>Using 3 different stored procedures and 4 different ways of calling it from C#.</p> <h3 id="stored-procedures">Stored Procedures</h3> <div class="language-sql highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">create</span> <span class="k">procedure</span> <span class="n">ThrowFirst</span> <span class="k">as</span> <span class="n">raiserror</span> <span class="p">(</span><span class="n">N</span><span class="s1">'Oops'</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span> <span class="k">select</span> <span class="n">name</span> <span class="k">from</span> <span class="n">sys</span><span class="p">.</span><span class="n">databases</span> <span class="k">go</span> <span class="k">create</span> <span class="k">procedure</span> <span class="n">ThrowSecond</span> <span class="k">as</span> <span class="k">select</span> <span class="n">name</span> <span class="k">from</span> <span class="n">sys</span><span class="p">.</span><span class="n">databases</span> <span class="n">raiserror</span> <span class="p">(</span><span class="n">N</span><span class="s1">'Oops'</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span> <span class="k">go</span> <span class="k">create</span> <span class="k">procedure</span> <span class="n">Works</span> <span class="k">as</span> <span class="k">select</span> <span class="n">name</span> <span class="k">from</span> <span class="n">sys</span><span class="p">.</span><span class="n">databases</span> <span class="k">go</span> </code></pre></div></div> <h3 id="csharp">CSharp</h3> <div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">void</span> <span class="nf">ExecuteNonQuery</span><span class="p">(</span><span class="n">SqlCommand</span> <span class="n">cmd</span><span class="p">)</span> <span class="p">{</span> <span class="n">cmd</span><span class="p">.</span><span class="nf">ExecuteNonQuery</span><span class="p">();</span> <span class="p">}</span> <span class="k">void</span> <span class="nf">ExecuteReaderOnly</span><span class="p">(</span><span class="n">SqlCommand</span> <span class="n">cmd</span><span class="p">)</span> <span class="p">{</span> <span class="k">using</span> <span class="p">(</span><span class="kt">var</span> <span class="n">reader</span> <span class="p">=</span> <span class="n">cmd</span><span class="p">.</span><span class="nf">ExecuteReader</span><span class="p">())</span> <span class="p">{</span> <span class="p">}</span> <span class="p">}</span> <span class="k">void</span> <span class="nf">ExecuteReaderReadOneResultSet</span><span class="p">(</span><span class="n">SqlCommand</span> <span class="n">cmd</span><span class="p">)</span> <span class="p">{</span> <span class="k">using</span> <span class="p">(</span><span class="kt">var</span> <span class="n">reader</span> <span class="p">=</span> <span class="n">cmd</span><span class="p">.</span><span class="nf">ExecuteReader</span><span class="p">())</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">names</span> <span class="p">=</span> <span class="k">new</span> <span class="n">List</span><span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;();</span> <span class="k">while</span><span class="p">(</span><span class="n">reader</span><span class="p">.</span><span class="nf">Read</span><span class="p">())</span> <span class="n">names</span><span class="p">.</span><span class="nf">Add</span><span class="p">(</span><span class="n">reader</span><span class="p">.</span><span class="nf">GetString</span><span class="p">(</span><span class="m">0</span><span class="p">));</span> <span class="p">}</span> <span class="p">}</span> <span class="k">void</span> <span class="nf">ExecuteReaderLookForAnotherResultSet</span><span class="p">(</span><span class="n">SqlCommand</span> <span class="n">cmd</span><span class="p">)</span> <span class="p">{</span> <span class="k">using</span> <span class="p">(</span><span class="kt">var</span> <span class="n">reader</span> <span class="p">=</span> <span class="n">cmd</span><span class="p">.</span><span class="nf">ExecuteReader</span><span class="p">())</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">names</span> <span class="p">=</span> <span class="k">new</span> <span class="n">List</span><span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;();</span> <span class="k">while</span> <span class="p">(</span><span class="n">reader</span><span class="p">.</span><span class="nf">Read</span><span class="p">())</span> <span class="n">names</span><span class="p">.</span><span class="nf">Add</span><span class="p">(</span><span class="n">reader</span><span class="p">.</span><span class="nf">GetString</span><span class="p">(</span><span class="m">0</span><span class="p">));</span> <span class="n">reader</span><span class="p">.</span><span class="nf">NextResult</span><span class="p">();</span> <span class="p">}</span> <span class="p">}</span> </code></pre></div></div> <h3 id="results">Results</h3> <p>The results are as follows:</p> <table> <thead> <tr> <th>Test</th> <th>Procedure</th> <th>Throws Exception</th> </tr> </thead> <tbody> <tr> <td>ExecuteNonQuery</td> <td>ThrowFirst</td> <td>✅</td> </tr> <tr> <td>ExecuteNonQuery</td> <td>ThrowSecond</td> <td>✅</td> </tr> <tr> <td>ExecuteNonQuery</td> <td>Works</td> <td>n/a</td> </tr> <tr> <td>ExecuteReader Only</td> <td>ThrowFirst</td> <td>✅</td> </tr> <tr> <td>ExecuteReader Only</td> <td>ThrowSecond</td> <td>❌</td> </tr> <tr> <td>ExecuteReader Only</td> <td>Works</td> <td>n/a</td> </tr> <tr> <td>ExecuteReader Read One ResultSet</td> <td>ThrowFirst</td> <td>✅</td> </tr> <tr> <td>ExecuteReader Read One ResultSet</td> <td>ThrowSecond</td> <td>❌</td> </tr> <tr> <td>ExecuteReader Read One ResultSet</td> <td>Works</td> <td>n/a</td> </tr> <tr> <td>ExecuteReader Look For Another ResultSet</td> <td>ThrowFirst</td> <td>✅</td> </tr> <tr> <td>ExecuteReader Look For Another ResultSet</td> <td>ThrowSecond</td> <td>✅</td> </tr> <tr> <td>ExecuteReader Look For Another ResultSet</td> <td>Works</td> <td>n/a</td> </tr> </tbody> </table> <h3 id="explained">Explained</h3> <p>The two problematic examples have a ❌ against them.</p> <p>Those are when you call <code class="highlighter-rouge">ExecuteReader</code> with the <code class="highlighter-rouge">ThrowSecond</code> stored procedure, and don’t go near the second result set.</p> <p>The only times where calling <code class="highlighter-rouge">ThrowSecond</code> will raise an exception in the .NET code is when using either, <code class="highlighter-rouge">ExecuteNonQuery()</code> (no good if you have results) or you call <code class="highlighter-rouge">reader.NextReslt()</code> even when you only expect a single result set.</p> <h3 id="xact_abort">XACT_ABORT</h3> <p>I tried setting <code class="highlighter-rouge">SET XACT_ABORT ON</code> but that made no difference, so I’ve left it out of the example.</p> <h2 id="conclusion">Conclusion</h2> <p>I’m not sure what my conclusion is for this. I could say, <em>don’t write SQL like this</em>. Perform all your data-manipulation (DML) queries first, then return the data you want. This should stop errors from the DML been a problem because they will always be prior to the result set you try and read.</p> <p>However, I don’t like that. SQL Management Studio does raise the error and I wouldn’t want to advocate writing your SQL to suite how .NET works. This feels like a .NET problem, not a SQL one.</p> <p>I will say don’t write stored procedures that return results, and then write C# that ignores them. That’s just wasteful.</p> <p>The only other solution would be to ensure you leave an extra <code class="highlighter-rouge">reader.NextResult()</code> after reading all of your expected result sets. This feels a little unusual too, and would probably be removed by the next developer, who could be unaware of why it is there in the first place.</p> <p>So in the end, I don’t know what’s the best approach, if anyone has any thoughts/comments about this, feel free to contact me on twitter.</p> <h2 id="downloads">Downloads</h2> <p>You can download the fully runnable examples from here:</p> <ul> <li><a href="/blog/Content/data-reader-gotcha-sql.linq">SQL</a></li> <li><a href="/blog/Content/data-reader-gotcha-csharp.linq">C#</a></li> </ul> <p>They are LINQPad scripts that run against a LocalDB called “Dave”.</p> Sun, 08 Apr 2018 16:23:00 +0000 https://taeguk.co.uk/blog/sqldatareader-gotcha/ https://taeguk.co.uk/blog/sqldatareader-gotcha/ blog SQL Server .NET XMAS Pi Fun <p>I’ve had a few days off over XMAS, so I decided to have a play with my Raspberry Pi and the <a href="https://thepihut.com/products/3d-xmas-tree-for-raspberry-pi">3D XMAS Tree</a> from <a href="https://thepihut.com">ThePiHut.com</a>.</p> <p>With my (very) basic Python skills I managed to come up with a way of using a <a href="https://thepihut.com/products/status-board-pro">Status Board</a> on one Pi to control the four different light settings on the XMAS Tree, running on another Pi (the “tree-berry”).</p> <iframe width="560" height="315" src="https://www.youtube.com/embed/l4qUhXMrNwo" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen=""></iframe> <p><em>(sorry about the camera work, I just shot it on my phone on the floor)</em></p> <p>All the source for this is on <a href="https://github.com/xdaDaveShaw/rasp-projects/tree/master/tree-berry">my GitHub</a>, if you want to see it.</p> <h2 id="how-it-works">How it works</h2> <p>The “tree-berry” Pi has a Python SocketServer running on it, receiving commands from the client, another Python program running on the other Pi.</p> <p>The server is very rudimentary. Each light setting was initially written as a separate python script with different characteristics on how it runs: some have <code class="highlighter-rouge">while True:</code> loops, others just set the lights and <code class="highlighter-rouge">pause</code>. To save me from figuring out in how to “teardown” each setting and start a new one, I decided to fork a new process from the server, and then kill it before changing to the next setting. This makes it slow to change, but ensures I clean up before starting another program.</p> <p>The 2 consoles can be seen side by side here:</p> <p><img src="/blog/Content/xmas-consoles.png" alt="Console Outputs" /></p> <p>There’s a lot I need to learn about Python, but this is only for a few weeks a year ;).</p> Sat, 23 Dec 2017 16:37:00 +0000 https://taeguk.co.uk/blog/xmas-pi-fun/ https://taeguk.co.uk/blog/xmas-pi-fun/ blog Development Raspberry Pi Drag and Drop Batch Files <p>This is a little trick that can make dealing with batch files a real breeze.</p> <p>You can make a batch file support drag and drop.</p> <p><img src="/blog/Content/drag-drop.gif" alt="Drag and drop" /></p> <p>Here I’ve create a simple batch file that takes in a single argument tells you that it is listing the file then prints the contents using the <a href="https://technet.microsoft.com/en-us/library/bb491026.aspx"><code class="highlighter-rouge">TYPE</code></a> command and then <code class="highlighter-rouge">PAUSE</code>’s.</p> <pre><code class="language-cmd">@echo off echo Listing the contents of %1 echo. type %1 echo. echo. pause </code></pre> <p>This works because when you drop a file on a executable in Windows the first argument passed to that program is the name of the file you dropped on it. So in the above script <code class="highlighter-rouge">%1</code> is the full path to whatever file you drop on the batch file.</p> <p>I’ve used this in a few different ways:</p> <ol> <li><a href="https://docs.microsoft.com/en-us/sysinternals/downloads/sdelete">SDelete</a>: I have a batch file to call SDelete with 64 passes . I created a shortcut to the batch file with an icon (so it looks nice), that I use for deleting sensitive files at work.</li> <li>Restoring development databases: I have another a batch file to restore development database backups, first it unzips the archive and then runs restore via SQLCMD.</li> </ol> <p>I’m sure there are a lot more uses for this. If you want to process multiple files you can <a href="https://stackoverflow.com/a/19837690/383710">iterate through all the arguments</a>.</p> <p>Thanks to <a href="https://forum.xda-developers.com/member.php?u=270777">bepe</a> from XDA Developers who was the person who first showed me this technique in his ROM Kitchen videos many years ago.</p> Thu, 19 Oct 2017 20:46:00 +0000 https://taeguk.co.uk/blog/drag-and-drop-batch-files/ https://taeguk.co.uk/blog/drag-and-drop-batch-files/ blog Development Handling results from complex operations <p>In this article I am going to look at a number of different approaches to model the results of a complex operation in C#. This is a technique I find useful when I have to perform some logic, and it can return different types of results, depending on the outcome. I’ll start with some naive approaches, before looking at two options that look more promising.</p> <p>I’ll be using the following logic for my “complex operation”:</p> <div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">if</span> <span class="n">length</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="n">is</span> <span class="n">even</span> <span class="k">return</span> <span class="n">length</span><span class="p">(</span><span class="n">s</span><span class="p">);</span> <span class="k">else</span> <span class="k">return</span> <span class="s">"Length is odd"</span><span class="p">;</span> </code></pre></div></div> <p>With an additional a requirement that any exceptions in the “complex operation” will be handled in a suitable way.</p> <p>This is actually something I have had to implement at work in a number of cases; try an operation, then depending on the outcome handle it in the most appropriate way.</p> <p>In the examples I’ll be using <code class="highlighter-rouge">Console.WriteLine</code> for simplicity, but in the real world there could be database calls, UI updates, HTML rendering, service calls, whatever usually makes testing hard.</p> <p>The inputs to and outputs from every example will be the same.</p> <p>Inputs:</p> <ul> <li><code class="highlighter-rouge">"Food"</code> (returns <em>4</em>)</li> <li><code class="highlighter-rouge">"Foo"</code> (returns <em>Length is odd.</em>)</li> <li><code class="highlighter-rouge">null</code> (returns <em>NullReferenceException</em>)</li> </ul> <p>All the code for the following is available in my GitHub repo <a href="https://github.com/xdaDaveShaw/ResultType-Blog">ResultType-blog</a> - these are <a href="http://www.linqpad.net/">Linqpad</a> scripts, but can easily be modified to be C# by removing the first line.</p> <h1 id="1-just-do-it">1. Just do it</h1> <p>Here’s the most trivial approach to solve the issue:</p> <div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">void</span> <span class="nf">Main</span><span class="p">()</span> <span class="p">{</span> <span class="nf">ComplexOperation</span><span class="p">(</span><span class="s">"Food"</span><span class="p">);</span> <span class="nf">ComplexOperation</span><span class="p">(</span><span class="s">"Foo"</span><span class="p">);</span> <span class="nf">ComplexOperation</span><span class="p">(</span><span class="k">null</span><span class="p">);</span> <span class="p">}</span> <span class="k">void</span> <span class="nf">ComplexOperation</span><span class="p">(</span><span class="n">String</span> <span class="n">input</span><span class="p">)</span> <span class="p">{</span> <span class="k">try</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">input</span><span class="p">.</span><span class="n">Length</span> <span class="p">%</span> <span class="m">2</span> <span class="p">==</span> <span class="m">0</span><span class="p">)</span> <span class="n">Console</span><span class="p">.</span><span class="nf">WriteLine</span><span class="p">(</span><span class="s">$"Even length: </span><span class="p">{</span><span class="n">input</span><span class="p">.</span><span class="n">Length</span><span class="p">}</span><span class="s">."</span><span class="p">);</span> <span class="k">else</span> <span class="n">Console</span><span class="p">.</span><span class="nf">WriteLine</span><span class="p">(</span><span class="s">"Length is odd."</span><span class="p">);</span> <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="n">Exception</span> <span class="n">ex</span><span class="p">)</span> <span class="p">{</span> <span class="n">Console</span><span class="p">.</span><span class="nf">WriteLine</span><span class="p">(</span><span class="n">ex</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> </code></pre></div></div> <p>This meets our requirements, but lets see if we can see a few issues with it.</p> <p>Firstly, it isn’t possible to test the Complex Operation on it’s own, you have no way to mock out the dependencies. Secondly, you’re mixing business logic, with side effects. Readers of Mark Seemann’s <a href="http://blog.ploeh.dk/2017/02/02/dependency-rejection/">blog</a> will know that this makes code harder to reason about.</p> <p>A common approach to solve this is to introduce a type to model the result of the complex operation. The remaining examples look at different approaches to do this.</p> <h1 id="2-implicit-results">2. Implicit Results</h1> <p>Let’s start with an Implicit Result type:</p> <div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Result</span> <span class="p">{</span> <span class="k">public</span> <span class="n">String</span> <span class="n">FailureMessage</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;}</span> <span class="k">public</span> <span class="n">Int32</span><span class="p">?</span> <span class="n">EvenLength</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span> <span class="k">public</span> <span class="n">Exception</span> <span class="n">Error</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;}</span> <span class="p">}</span> </code></pre></div></div> <p>I call it Implicit because if I passed you an instance of it, you have no obvious way of knowing what the result is or how to figure out what happened during the complex operation. You could check that <code class="highlighter-rouge">EvenLength</code> is not null and assume success, but what’s to say I didn’t put set it to <code class="highlighter-rouge">0</code> and populate <code class="highlighter-rouge">FailureMessage</code> instead? It it mostly guess work and assumptions to know what this result contains.</p> <p>Here’s the new program and complex operation using this type:</p> <div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">void</span> <span class="nf">Main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">inputs</span> <span class="p">=</span> <span class="k">new</span><span class="p">[]</span> <span class="p">{</span> <span class="s">"Food"</span><span class="p">,</span> <span class="s">"Foo"</span><span class="p">,</span> <span class="k">null</span><span class="p">,</span> <span class="p">};</span> <span class="k">foreach</span> <span class="p">(</span><span class="kt">var</span> <span class="n">result</span> <span class="k">in</span> <span class="n">inputs</span><span class="p">.</span><span class="nf">Select</span><span class="p">(</span><span class="n">ComplexOperation</span><span class="p">))</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">result</span><span class="p">.</span><span class="n">Error</span> <span class="p">!=</span> <span class="k">null</span><span class="p">)</span> <span class="n">Console</span><span class="p">.</span><span class="nf">WriteLine</span><span class="p">(</span><span class="n">result</span><span class="p">.</span><span class="n">Error</span><span class="p">);</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">result</span><span class="p">.</span><span class="n">FailureMessage</span> <span class="p">!=</span> <span class="k">null</span><span class="p">)</span> <span class="n">Console</span><span class="p">.</span><span class="nf">WriteLine</span><span class="p">(</span><span class="n">result</span><span class="p">.</span><span class="n">FailureMessage</span><span class="p">);</span> <span class="k">else</span> <span class="n">Console</span><span class="p">.</span><span class="nf">WriteLine</span><span class="p">(</span><span class="s">$"Even length: </span><span class="p">{</span><span class="n">result</span><span class="p">.</span><span class="n">EvenLength</span><span class="p">}</span><span class="s">."</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="n">Result</span> <span class="nf">ComplexOperation</span><span class="p">(</span><span class="n">String</span> <span class="n">input</span><span class="p">)</span> <span class="p">{</span> <span class="k">try</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">input</span><span class="p">.</span><span class="n">Length</span> <span class="p">%</span> <span class="m">2</span> <span class="p">==</span> <span class="m">0</span><span class="p">)</span> <span class="k">return</span> <span class="k">new</span> <span class="n">Result</span> <span class="p">{</span> <span class="n">EvenLength</span> <span class="p">=</span> <span class="n">input</span><span class="p">.</span><span class="n">Length</span><span class="p">,</span> <span class="p">};</span> <span class="k">else</span> <span class="k">return</span> <span class="k">new</span> <span class="n">Result</span> <span class="p">{</span> <span class="n">FailureMessage</span> <span class="p">=</span> <span class="s">"Length is odd."</span><span class="p">,</span> <span class="p">};</span> <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="n">Exception</span> <span class="n">ex</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="k">new</span> <span class="n">Result</span> <span class="p">{</span> <span class="n">Error</span> <span class="p">=</span> <span class="n">ex</span><span class="p">,</span> <span class="p">};</span> <span class="p">}</span> <span class="p">}</span> </code></pre></div></div> <p>Now you see the implementation you know there is no funny business, but I made you read the complex operation to be sure.</p> <p>There are some other problems with this too:</p> <ul> <li>The type is mutable, meaning something might change the result after the operation.</li> <li>When the type is constructed, it is half-full, some members will have values, some won’t.</li> <li>You have to know how to process a result, I’d find myself asking <em>can an error also have a message?</em>.</li> <li>It violates the <a href="https://en.wikipedia.org/wiki/Open/closed_principle">Open/closed principle</a> because changes to Success, Failure or Error require a change to this one type.</li> </ul> <p>This does, however, separate the operation from the outputs, making the operation testable without it needing any <em>additional</em> depencies for the output. The operation is now Pure, which is why I can use <code class="highlighter-rouge">Select</code> on each input to return the result.</p> <h1 id="3-explicit-results">3. Explicit Results</h1> <p>I’ve harped on enough about how bad it it that the result is implied in the previous example. So let’s have a go at been more explicit.</p> <p>Here’s a result type with an enum to tell you what happened:</p> <div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Result</span> <span class="p">{</span> <span class="k">public</span> <span class="n">String</span> <span class="n">FailureMessage</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;}</span> <span class="k">public</span> <span class="n">Int32</span><span class="p">?</span> <span class="n">EvenLength</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span> <span class="k">public</span> <span class="n">Exception</span> <span class="n">Error</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span> <span class="k">public</span> <span class="n">ResultType</span> <span class="n">Type</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="k">enum</span> <span class="n">ResultType</span> <span class="p">{</span> <span class="n">Success</span><span class="p">,</span> <span class="n">Failure</span><span class="p">,</span> <span class="n">Error</span><span class="p">,</span> <span class="p">}</span> </code></pre></div></div> <p>Now when you get a result you can first check the <code class="highlighter-rouge">Type</code> and know it is an error.</p> <p>Here’s the application code for it:</p> <div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">void</span> <span class="nf">Main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">inputs</span> <span class="p">=</span> <span class="k">new</span><span class="p">[]</span> <span class="p">{</span> <span class="s">"Food"</span><span class="p">,</span> <span class="s">"Foo"</span><span class="p">,</span> <span class="k">null</span><span class="p">,</span> <span class="p">};</span> <span class="k">foreach</span> <span class="p">(</span><span class="kt">var</span> <span class="n">result</span> <span class="k">in</span> <span class="n">inputs</span><span class="p">.</span><span class="nf">Select</span><span class="p">(</span><span class="n">ComplexOperation</span><span class="p">))</span> <span class="p">{</span> <span class="k">switch</span> <span class="p">(</span><span class="n">result</span><span class="p">.</span><span class="n">Type</span><span class="p">)</span> <span class="p">{</span> <span class="k">case</span> <span class="n">ResultType</span><span class="p">.</span><span class="n">Success</span><span class="p">:</span> <span class="n">Console</span><span class="p">.</span><span class="nf">WriteLine</span><span class="p">(</span><span class="s">$"Even length: </span><span class="p">{</span><span class="n">result</span><span class="p">.</span><span class="n">EvenLength</span><span class="p">}</span><span class="s">."</span><span class="p">);</span> <span class="k">break</span><span class="p">;</span> <span class="k">case</span> <span class="n">ResultType</span><span class="p">.</span><span class="n">Failure</span><span class="p">:</span> <span class="n">Console</span><span class="p">.</span><span class="nf">WriteLine</span><span class="p">(</span><span class="n">result</span><span class="p">.</span><span class="n">FailureMessage</span><span class="p">);</span> <span class="k">break</span><span class="p">;</span> <span class="k">case</span> <span class="n">ResultType</span><span class="p">.</span><span class="n">Error</span><span class="p">:</span> <span class="n">Console</span><span class="p">.</span><span class="nf">WriteLine</span><span class="p">(</span><span class="n">result</span><span class="p">.</span><span class="n">Error</span><span class="p">);</span> <span class="k">break</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="n">Result</span> <span class="nf">ComplexOperation</span><span class="p">(</span><span class="n">String</span> <span class="n">input</span><span class="p">)</span> <span class="p">{</span> <span class="k">try</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">input</span><span class="p">.</span><span class="n">Length</span> <span class="p">%</span> <span class="m">2</span> <span class="p">==</span> <span class="m">0</span><span class="p">)</span> <span class="k">return</span> <span class="k">new</span> <span class="n">Result</span> <span class="p">{</span> <span class="n">EvenLength</span> <span class="p">=</span> <span class="n">input</span><span class="p">.</span><span class="n">Length</span><span class="p">,</span> <span class="n">Type</span> <span class="p">=</span> <span class="n">ResultType</span><span class="p">.</span><span class="n">Success</span><span class="p">,</span> <span class="p">};</span> <span class="k">else</span> <span class="k">return</span> <span class="k">new</span> <span class="n">Result</span> <span class="p">{</span> <span class="n">FailureMessage</span> <span class="p">=</span> <span class="s">"Length is odd."</span><span class="p">,</span> <span class="n">Type</span> <span class="p">=</span> <span class="n">ResultType</span><span class="p">.</span><span class="n">Failure</span><span class="p">,</span> <span class="p">};</span> <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="n">Exception</span> <span class="n">ex</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="k">new</span> <span class="n">Result</span> <span class="p">{</span> <span class="n">Error</span> <span class="p">=</span> <span class="n">ex</span><span class="p">,</span> <span class="n">Type</span> <span class="p">=</span> <span class="n">ResultType</span><span class="p">.</span><span class="n">Error</span><span class="p">,</span> <span class="p">};</span> <span class="p">}</span> <span class="p">}</span> </code></pre></div></div> <p>This still has some of the problems of the previous version: mutability, Open/closed principle, mixed bag of properties, and not knowing what to do with them. It also has the problem that nothing is forcing you to check the <code class="highlighter-rouge">Type</code>, I might just say <em>It’s OK, this won’t fail, just get my the <code class="highlighter-rouge">EvenLength</code></em> - famous last words…</p> <p>So, whilst it is a little better, it can still lead to unreasonable code.</p> <h1 id="4-explicit---with-factory-methods">4. Explicit - with factory methods</h1> <p>To solve the problem of people creating a “mixed bag” of mutable properties, a factory method could be created on the type to initialise the result in the correct state depending on the outcome of the operation.</p> <div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Result</span> <span class="p">{</span> <span class="k">public</span> <span class="n">String</span> <span class="n">FailureMessage</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">private</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span> <span class="k">public</span> <span class="n">Int32</span><span class="p">?</span> <span class="n">EvenLength</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">private</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span> <span class="k">public</span> <span class="n">Exception</span> <span class="n">Error</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">private</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span> <span class="k">public</span> <span class="n">ResultType</span> <span class="n">Type</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">private</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span> <span class="k">public</span> <span class="k">static</span> <span class="n">Result</span> <span class="nf">CreateFailure</span><span class="p">(</span><span class="n">String</span> <span class="n">message</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="k">new</span> <span class="n">Result</span> <span class="p">{</span> <span class="n">FailureMessage</span> <span class="p">=</span> <span class="n">message</span><span class="p">,</span> <span class="n">Type</span> <span class="p">=</span> <span class="n">ResultType</span><span class="p">.</span><span class="n">Failure</span><span class="p">,</span> <span class="p">};</span> <span class="p">}</span> <span class="k">public</span> <span class="k">static</span> <span class="n">Result</span> <span class="nf">CreateSuccess</span><span class="p">(</span><span class="n">Int32</span> <span class="k">value</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="k">new</span> <span class="n">Result</span> <span class="p">{</span> <span class="n">EvenLength</span> <span class="p">=</span> <span class="k">value</span><span class="p">,</span> <span class="n">Type</span> <span class="p">=</span> <span class="n">ResultType</span><span class="p">.</span><span class="n">Success</span><span class="p">,</span> <span class="p">};</span> <span class="p">}</span> <span class="k">public</span> <span class="k">static</span> <span class="n">Result</span> <span class="nf">CreateError</span><span class="p">(</span><span class="n">Exception</span> <span class="n">ex</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="k">new</span> <span class="n">Result</span> <span class="p">{</span> <span class="n">Error</span> <span class="p">=</span> <span class="n">ex</span><span class="p">,</span> <span class="n">Type</span> <span class="p">=</span> <span class="n">ResultType</span><span class="p">.</span><span class="n">Error</span><span class="p">,</span> <span class="p">};</span> <span class="p">}</span> <span class="p">}</span> </code></pre></div></div> <p>This changes the complex operation to look like this:</p> <div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">Result</span> <span class="nf">ComplexOperation</span><span class="p">(</span><span class="n">String</span> <span class="n">input</span><span class="p">)</span> <span class="p">{</span> <span class="k">try</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">input</span><span class="p">.</span><span class="n">Length</span> <span class="p">%</span> <span class="m">2</span> <span class="p">==</span> <span class="m">0</span><span class="p">)</span> <span class="k">return</span> <span class="n">Result</span><span class="p">.</span><span class="nf">CreateSuccess</span><span class="p">(</span><span class="n">input</span><span class="p">.</span><span class="n">Length</span><span class="p">);</span> <span class="k">else</span> <span class="k">return</span> <span class="n">Result</span><span class="p">.</span><span class="nf">CreateFailure</span><span class="p">(</span><span class="s">"Length is odd."</span><span class="p">);</span> <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="n">Exception</span> <span class="n">ex</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">Result</span><span class="p">.</span><span class="nf">CreateError</span><span class="p">(</span><span class="n">ex</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> </code></pre></div></div> <p>The rest of the program is unchanged from the previous version.</p> <p>We now have a way to know that the Result with the type success will only have an <code class="highlighter-rouge">EvenValue</code>, however we still need to ignore the other properties that don’t relate to success. There’s still nothing forcing people to check the <code class="highlighter-rouge">Type</code>, and this requires additional factory methods for every state.</p> <p>I’ve seen a number of people stop at this level, and call it “good enough” to avoid having to go to the next level. You still have unreasonable code, and have to understand things in the operation.</p> <h1 id="5-exceptions-for-control-flow">5. Exceptions for control flow</h1> <p>This is another approach I have seen used, I do not like it, but I thought I would include it, as I <em>almost</em> used it years ago before using one of the approaches in the following sections.</p> <div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">void</span> <span class="nf">Main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">inputs</span> <span class="p">=</span> <span class="k">new</span><span class="p">[]</span> <span class="p">{</span> <span class="s">"Food"</span><span class="p">,</span> <span class="s">"Foo"</span><span class="p">,</span> <span class="k">null</span><span class="p">,</span> <span class="p">};</span> <span class="k">foreach</span> <span class="p">(</span><span class="kt">var</span> <span class="n">input</span> <span class="k">in</span> <span class="n">inputs</span><span class="p">)</span> <span class="p">{</span> <span class="k">try</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">result</span> <span class="p">=</span> <span class="nf">ComplexOperation</span><span class="p">(</span><span class="n">input</span><span class="p">);</span> <span class="n">Console</span><span class="p">.</span><span class="nf">WriteLine</span><span class="p">(</span><span class="s">$"Even length: </span><span class="p">{</span><span class="n">result</span><span class="p">}</span><span class="s">."</span><span class="p">);</span> <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="n">BusinessException</span> <span class="n">be</span><span class="p">)</span> <span class="p">{</span> <span class="k">switch</span> <span class="p">(</span><span class="n">be</span><span class="p">)</span> <span class="p">{</span> <span class="k">case</span> <span class="n">FailureException</span> <span class="n">f</span><span class="p">:</span> <span class="n">Console</span><span class="p">.</span><span class="nf">WriteLine</span><span class="p">(</span><span class="n">f</span><span class="p">.</span><span class="n">Message</span><span class="p">);</span> <span class="k">break</span><span class="p">;</span> <span class="k">case</span> <span class="n">ErrorException</span> <span class="n">e</span><span class="p">:</span> <span class="n">Console</span><span class="p">.</span><span class="nf">WriteLine</span><span class="p">(</span><span class="n">e</span><span class="p">.</span><span class="n">InnerException</span><span class="p">);</span> <span class="k">break</span><span class="p">;</span> <span class="k">default</span><span class="p">:</span> <span class="k">throw</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="n">Int32</span> <span class="nf">ComplexOperation</span><span class="p">(</span><span class="n">String</span> <span class="n">input</span><span class="p">)</span> <span class="p">{</span> <span class="k">try</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">input</span><span class="p">.</span><span class="n">Length</span> <span class="p">%</span> <span class="m">2</span> <span class="p">==</span> <span class="m">0</span><span class="p">)</span> <span class="k">return</span> <span class="n">input</span><span class="p">.</span><span class="n">Length</span><span class="p">;</span> <span class="k">else</span> <span class="k">throw</span> <span class="k">new</span> <span class="nf">FailureException</span><span class="p">(</span><span class="s">"Length is odd."</span><span class="p">);</span> <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="n">Exception</span> <span class="n">ex</span><span class="p">)</span> <span class="nf">when</span> <span class="p">(!(</span><span class="n">ex</span> <span class="k">is</span> <span class="n">BusinessException</span><span class="p">))</span> <span class="p">{</span> <span class="k">throw</span> <span class="k">new</span> <span class="nf">ErrorException</span><span class="p">(</span><span class="n">ex</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="k">class</span> <span class="nc">FailureException</span> <span class="p">:</span> <span class="n">BusinessException</span> <span class="p">{</span> <span class="k">public</span> <span class="nf">FailureException</span><span class="p">(</span><span class="n">String</span> <span class="n">message</span><span class="p">)</span> <span class="p">:</span> <span class="k">base</span><span class="p">(</span><span class="n">message</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span> <span class="p">}</span> <span class="k">class</span> <span class="nc">ErrorException</span> <span class="p">:</span> <span class="n">BusinessException</span> <span class="p">{</span> <span class="k">public</span> <span class="nf">ErrorException</span><span class="p">(</span><span class="n">Exception</span> <span class="n">inner</span><span class="p">)</span> <span class="p">:</span> <span class="k">base</span><span class="p">(</span><span class="n">inner</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span> <span class="p">}</span> <span class="k">abstract</span> <span class="k">class</span> <span class="nc">BusinessException</span> <span class="p">:</span> <span class="n">Exception</span> <span class="p">{</span> <span class="k">public</span> <span class="nf">BusinessException</span><span class="p">(</span><span class="n">String</span> <span class="n">message</span><span class="p">)</span> <span class="p">:</span> <span class="k">base</span><span class="p">(</span><span class="n">message</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span> <span class="k">public</span> <span class="nf">BusinessException</span><span class="p">(</span><span class="n">Exception</span> <span class="n">inner</span><span class="p">)</span> <span class="p">:</span> <span class="k">base</span><span class="p">(</span><span class="s">"Something bad happened"</span><span class="p">,</span> <span class="n">inner</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span> <span class="p">}</span> </code></pre></div></div> <p>I hope by looking at this code you can see it isn’t an ideal appraoch.</p> <p>I’ve introduced the concept of a <code class="highlighter-rouge">BusinessException</code> that the program will handle in a <code class="highlighter-rouge">try...catch</code> block. All problems in the complex operation will throw some sort of exception derived from <code class="highlighter-rouge">BusinessException</code>, which the program will then type match on. I’ve used <a href="https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7#pattern-matching">pattern matching</a> here, but I’ve see other approaches such as a <code class="highlighter-rouge">Dictionary&lt;Exception, Action&lt;Exception&gt;&gt;</code> that has a list of exceptions and the delegate to call.</p> <p>Using exceptions like this is the equivelent of <code class="highlighter-rouge">goto</code>, many people have said it before, so I won’t go into detail on that aspect. I did notice when writing this how hard it is to not accidentally catch your own <code class="highlighter-rouge">BusinessException</code>, this is why I have an exception filter to not handle them twice: <code class="highlighter-rouge">catch (Exception ex) when (!(ex is BusinessException))</code>. I could imagine the case where one stray <code class="highlighter-rouge">try...catch</code> could cause a lot of problems.</p> <h1 id="6-type-per-result">6. Type per Result</h1> <p>I’ve now harped one enough about not knowing what to do with results. This example removes the ambiguity and uses a separate type for each result.</p> <div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Success</span> <span class="p">:</span> <span class="n">Result</span> <span class="p">{</span> <span class="k">public</span> <span class="n">Int32</span> <span class="n">EvenLength</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="p">}</span> <span class="k">public</span> <span class="nf">Success</span><span class="p">(</span><span class="n">Int32</span> <span class="k">value</span><span class="p">)</span> <span class="p">{</span> <span class="n">EvenLength</span> <span class="p">=</span> <span class="k">value</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="k">class</span> <span class="nc">Failure</span> <span class="p">:</span> <span class="n">Result</span> <span class="p">{</span> <span class="k">public</span> <span class="n">String</span> <span class="n">FailureMessage</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="p">}</span> <span class="k">public</span> <span class="nf">Failure</span><span class="p">(</span><span class="n">String</span> <span class="n">message</span><span class="p">)</span> <span class="p">{</span> <span class="n">FailureMessage</span> <span class="p">=</span> <span class="n">message</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="k">class</span> <span class="nc">Error</span> <span class="p">:</span> <span class="n">Result</span> <span class="p">{</span> <span class="k">public</span> <span class="n">Exception</span> <span class="n">Exception</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="p">}</span> <span class="k">public</span> <span class="nf">Error</span><span class="p">(</span><span class="n">Exception</span> <span class="n">ex</span><span class="p">)</span> <span class="p">{</span> <span class="n">Exception</span> <span class="p">=</span> <span class="n">ex</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="k">abstract</span> <span class="k">class</span> <span class="nc">Result</span> <span class="p">{</span> <span class="p">}</span> </code></pre></div></div> <p>Each result now has its own type. Each type only has properties relating to that type of result. The results are immutable.</p> <p>The base class in this case is empty, but it might capture the input, elapsed time, or anything else you need in every result.</p> <p>The program and complex operation now are much easier to reason about, and it is a lot harder to mix things up:</p> <div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">void</span> <span class="nf">Main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">inputs</span> <span class="p">=</span> <span class="k">new</span><span class="p">[]</span> <span class="p">{</span> <span class="s">"Food"</span><span class="p">,</span> <span class="s">"Foo"</span><span class="p">,</span> <span class="k">null</span><span class="p">,</span> <span class="p">};</span> <span class="k">foreach</span> <span class="p">(</span><span class="kt">var</span> <span class="n">result</span> <span class="k">in</span> <span class="n">inputs</span><span class="p">.</span><span class="nf">Select</span><span class="p">(</span><span class="n">ComplexOperation</span><span class="p">))</span> <span class="p">{</span> <span class="k">switch</span> <span class="p">(</span><span class="n">result</span><span class="p">)</span> <span class="p">{</span> <span class="k">case</span> <span class="n">Success</span> <span class="n">s</span><span class="p">:</span> <span class="n">Console</span><span class="p">.</span><span class="nf">WriteLine</span><span class="p">(</span><span class="s">$"Even length: </span><span class="p">{</span><span class="n">s</span><span class="p">.</span><span class="n">EvenLength</span><span class="p">}</span><span class="s">."</span><span class="p">);</span> <span class="k">break</span><span class="p">;</span> <span class="k">case</span> <span class="n">Failure</span> <span class="n">f</span><span class="p">:</span> <span class="n">Console</span><span class="p">.</span><span class="nf">WriteLine</span><span class="p">(</span><span class="n">f</span><span class="p">.</span><span class="n">FailureMessage</span><span class="p">);</span> <span class="k">break</span><span class="p">;</span> <span class="k">case</span> <span class="n">Error</span> <span class="n">e</span><span class="p">:</span> <span class="n">Console</span><span class="p">.</span><span class="nf">WriteLine</span><span class="p">(</span><span class="n">e</span><span class="p">.</span><span class="n">Exception</span><span class="p">);</span> <span class="k">break</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="n">Result</span> <span class="nf">ComplexOperation</span><span class="p">(</span><span class="n">String</span> <span class="n">input</span><span class="p">)</span> <span class="p">{</span> <span class="k">try</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">input</span><span class="p">.</span><span class="n">Length</span> <span class="p">%</span> <span class="m">2</span> <span class="p">==</span> <span class="m">0</span><span class="p">)</span> <span class="k">return</span> <span class="k">new</span> <span class="nf">Success</span><span class="p">(</span><span class="n">input</span><span class="p">.</span><span class="n">Length</span><span class="p">);</span> <span class="k">else</span> <span class="k">return</span> <span class="k">new</span> <span class="nf">Failure</span><span class="p">(</span><span class="s">"Length is odd."</span><span class="p">);</span> <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="n">Exception</span> <span class="n">ex</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="k">new</span> <span class="nf">Error</span><span class="p">(</span><span class="n">ex</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> </code></pre></div></div> <p>I’m using C# 7’s <a href="https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7#pattern-matching">Pattern Matching</a> feature in the program to compare each type of the result. When I get a match it is already cast into the correct type, so <code class="highlighter-rouge">s</code> will be an instance of <code class="highlighter-rouge">Success</code>, and <code class="highlighter-rouge">Success</code> only has properties relating to a successful outcome.</p> <p>To me this is very clear what I can do next after a complex operation and what happened in the operation.</p> <p>It is reassuring to know that if I have a <code class="highlighter-rouge">Success</code> type I can only see properties relating to a successful operation, I can pass the result to another method, that accepts an instance of <code class="highlighter-rouge">Success</code> knowing it can’t be called with an <code class="highlighter-rouge">Error</code> by mistake - the type safety in the language is on your side.</p> <p>Consider these 2 methods:</p> <div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">void</span> <span class="nf">DisplaySuccess</span><span class="p">(</span><span class="n">Result</span> <span class="n">r</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span> <span class="k">void</span> <span class="nf">DisplaySuccess</span><span class="p">(</span><span class="n">Success</span> <span class="n">s</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span> </code></pre></div></div> <p>In all previous examples you had to have the first version, and that would either assume you called it correctly, or would have to check that <code class="highlighter-rouge">r</code> is a success. The second method can only be called with an instance of <code class="highlighter-rouge">Success</code>, you cannot pass an <code class="highlighter-rouge">Error</code> to it, making it much harder to get wrong.</p> <p>There are a few negatives to this approach, in C#’s pattern matching the compiler doesn’t check you have every case matched, adding a new result type means I need to find all instances of result handlers and update them. If you have only one handler, then this isn’t so bad.</p> <p>Another consideration is that the result’s “next step” logic - what happens next - is separated from the type. Sometimes this could be desirable, other times you might want it contained in a single place, it depends on how your application is designed and what works best. The next exmaple looks at keeping the behaviour with the result.</p> <h1 id="7-types-with-behaviour">7. Types with Behaviour</h1> <p>I’ve fleshed the following code out a little more, to highlight one of the drawbacks of the approach. In all previous examples, I’ve left out how you might test the entire operation - passing in test doubles for <code class="highlighter-rouge">Console.WriteLine</code> into the program from the composition root would be trivial.</p> <p>However, in this case I wanted to show the extra effort needed to keep things testable.</p> <p>First, we’ll look at the base result type:</p> <div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">abstract</span> <span class="k">class</span> <span class="nc">Result</span> <span class="p">{</span> <span class="k">public</span> <span class="nf">Result</span><span class="p">(</span><span class="n">IProcessor</span> <span class="n">processor</span><span class="p">)</span> <span class="p">{</span> <span class="n">Processor</span> <span class="p">=</span> <span class="n">processor</span><span class="p">;</span> <span class="p">}</span> <span class="k">protected</span> <span class="n">IProcessor</span> <span class="n">Processor</span> <span class="p">{</span> <span class="k">get</span><span class="p">;}</span> <span class="k">public</span> <span class="k">abstract</span> <span class="k">void</span> <span class="nf">Process</span><span class="p">();</span> <span class="p">}</span> <span class="k">interface</span> <span class="nc">IProcessor</span> <span class="p">{</span> <span class="k">void</span> <span class="nf">WriteMessage</span><span class="p">(</span><span class="n">Object</span> <span class="n">message</span><span class="p">);</span> <span class="p">}</span> <span class="k">class</span> <span class="nc">Processor</span> <span class="p">:</span> <span class="n">IProcessor</span> <span class="p">{</span> <span class="k">public</span> <span class="k">void</span> <span class="nf">WriteMessage</span><span class="p">(</span><span class="n">Object</span> <span class="n">message</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="n">Console</span><span class="p">.</span><span class="nf">WriteLine</span><span class="p">(</span><span class="n">message</span><span class="p">);</span> <span class="p">}</span> </code></pre></div></div> <p>There’s now an additional <code class="highlighter-rouge">Process</code> member on every result and every result needs access to a <code class="highlighter-rouge">IProcessor</code> which facilitates the injection of the dependencies for the <code class="highlighter-rouge">Process</code> method.</p> <p>This is what the calling program will use to handle the result:</p> <div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">void</span> <span class="nf">Main</span><span class="p">()</span> <span class="p">{</span> <span class="kt">var</span> <span class="n">inputs</span> <span class="p">=</span> <span class="k">new</span><span class="p">[]</span> <span class="p">{</span> <span class="s">"Food"</span><span class="p">,</span> <span class="s">"Foo"</span><span class="p">,</span> <span class="k">null</span><span class="p">,</span> <span class="p">};</span> <span class="k">foreach</span> <span class="p">(</span><span class="kt">var</span> <span class="n">result</span> <span class="k">in</span> <span class="n">inputs</span><span class="p">.</span><span class="nf">Select</span><span class="p">(</span><span class="n">ComplexOperation</span><span class="p">))</span> <span class="p">{</span> <span class="n">result</span><span class="p">.</span><span class="nf">Process</span><span class="p">();</span> <span class="p">}</span> <span class="p">}</span> </code></pre></div></div> <p>This looks very neat, I get a result, I call <code class="highlighter-rouge">Process</code>.</p> <p>The problems are getting the dependencies managed in an nice way. When deriving instances of the <code class="highlighter-rouge">Result</code> you need to write the code to pass <code class="highlighter-rouge">IProcessor</code> through:</p> <div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Success</span> <span class="p">:</span> <span class="n">Result</span> <span class="p">{</span> <span class="k">public</span> <span class="n">Int32</span> <span class="n">EvenLength</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="p">}</span> <span class="k">public</span> <span class="nf">Success</span><span class="p">(</span><span class="n">IProcessor</span> <span class="n">p</span><span class="p">,</span> <span class="n">Int32</span> <span class="k">value</span><span class="p">)</span> <span class="p">:</span> <span class="k">base</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="p">{</span> <span class="n">EvenLength</span> <span class="p">=</span> <span class="k">value</span><span class="p">;</span> <span class="p">}</span> <span class="k">public</span> <span class="k">override</span> <span class="k">void</span> <span class="nf">Process</span><span class="p">()</span> <span class="p">=&gt;</span> <span class="n">Processor</span><span class="p">.</span><span class="nf">WriteMessage</span><span class="p">(</span><span class="s">$"Even length: </span><span class="p">{</span><span class="n">EvenLength</span><span class="p">}</span><span class="s">."</span><span class="p">);</span> <span class="p">}</span> <span class="k">class</span> <span class="nc">Failure</span> <span class="p">:</span> <span class="n">Result</span> <span class="p">{</span> <span class="k">public</span> <span class="n">String</span> <span class="n">FailureMessage</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="p">}</span> <span class="k">public</span> <span class="nf">Failure</span><span class="p">(</span><span class="n">IProcessor</span> <span class="n">p</span><span class="p">,</span> <span class="n">String</span> <span class="n">message</span><span class="p">)</span> <span class="p">:</span> <span class="k">base</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="p">{</span> <span class="n">FailureMessage</span> <span class="p">=</span> <span class="n">message</span><span class="p">;</span> <span class="p">}</span> <span class="k">public</span> <span class="k">override</span> <span class="k">void</span> <span class="nf">Process</span><span class="p">()</span> <span class="p">=&gt;</span> <span class="n">Processor</span><span class="p">.</span><span class="nf">WriteMessage</span><span class="p">(</span><span class="n">FailureMessage</span><span class="p">);</span> <span class="p">}</span> <span class="k">class</span> <span class="nc">Error</span> <span class="p">:</span> <span class="n">Result</span> <span class="p">{</span> <span class="k">public</span> <span class="n">Exception</span> <span class="n">Exception</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="p">}</span> <span class="k">public</span> <span class="nf">Error</span><span class="p">(</span><span class="n">IProcessor</span> <span class="n">p</span><span class="p">,</span> <span class="n">Exception</span> <span class="n">ex</span><span class="p">)</span> <span class="p">:</span> <span class="k">base</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="p">{</span> <span class="n">Exception</span> <span class="p">=</span> <span class="n">ex</span><span class="p">;</span> <span class="p">}</span> <span class="k">public</span> <span class="k">override</span> <span class="k">void</span> <span class="nf">Process</span><span class="p">()</span> <span class="p">=&gt;</span> <span class="n">Processor</span><span class="p">.</span><span class="nf">WriteMessage</span><span class="p">(</span><span class="n">Exception</span><span class="p">);</span> <span class="p">}</span> </code></pre></div></div> <p>Each result now has an implementation of the logic to handle it. If you want to know what happens given a success, I can just look at the <code class="highlighter-rouge">Success</code> type.</p> <p>But when you create an instance, you also need to pass in an <code class="highlighter-rouge">IProcessor</code>, so the complex operation will have to do this:</p> <div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">IProcessor</span> <span class="n">processor</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">Processor</span><span class="p">();</span> <span class="n">Result</span> <span class="nf">ComplexOperation</span><span class="p">(</span><span class="n">String</span> <span class="n">input</span><span class="p">)</span> <span class="p">{</span> <span class="k">try</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">input</span><span class="p">.</span><span class="n">Length</span> <span class="p">%</span> <span class="m">2</span> <span class="p">==</span> <span class="m">0</span><span class="p">)</span> <span class="k">return</span> <span class="k">new</span> <span class="nf">Success</span><span class="p">(</span><span class="n">processor</span><span class="p">,</span> <span class="n">input</span><span class="p">.</span><span class="n">Length</span><span class="p">);</span> <span class="k">else</span> <span class="k">return</span> <span class="k">new</span> <span class="nf">Failure</span><span class="p">(</span><span class="n">processor</span><span class="p">,</span> <span class="s">"Length is odd."</span><span class="p">);</span> <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="n">Exception</span> <span class="n">ex</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="k">new</span> <span class="nf">Error</span><span class="p">(</span><span class="n">processor</span><span class="p">,</span> <span class="n">ex</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> </code></pre></div></div> <p>This is quite a lot of ceremony, and now the complex operation has knowledge of the <code class="highlighter-rouge">IProcessor</code>. An instance of an <code class="highlighter-rouge">IProcessor</code> would have to be injected so that it can be passed into each result. The complex operation doesn’t depend on <code class="highlighter-rouge">IProcessor</code> though, just the results, making this a kind of transient dependency.</p> <p>This example isn’t perfect, but I have used it in a number of places where I wanted to keep the logic of what to do with a result with the result, and not separated out across the code base. Usually when there’s a lot of code related to handling the result.</p> <p>I also like that I am able to write code such as:</p> <div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">var</span> <span class="n">result</span> <span class="p">=</span> <span class="nf">ComplexOperation</span><span class="p">(</span><span class="n">input</span><span class="p">);</span> <span class="n">resut</span><span class="p">.</span><span class="nf">Process</span><span class="p">();</span> </code></pre></div></div> <p>If you need to add a new result, type (e.g. Timeout) you can do so by just deriving a new type from <code class="highlighter-rouge">Result</code> and implementing all the logic there. The only other place that needs a change is the complex operation to return <code class="highlighter-rouge">new Timeout(processor)</code>, the program doesn’t have to change.</p> <h1 id="8-bonus-f-version">8. Bonus F# version</h1> <p>I am a big fan of F# so I thought I would model the same problem in F#.</p> <p>I’ve deliberately kept it similar to the C# examples to avoid it getting too functional. This is quite close to example #6 above.</p> <div class="language-ocaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">type</span> <span class="nc">Result</span> <span class="o">=</span> <span class="o">|</span> <span class="nc">Success</span> <span class="k">of</span> <span class="kt">int</span> <span class="o">|</span> <span class="nc">Failure</span> <span class="k">of</span> <span class="kt">string</span> <span class="o">|</span> <span class="nc">Error</span> <span class="k">of</span> <span class="nc">Exception</span> <span class="c">(* Unchecked.defaultof&lt;String&gt; is used for null to make it crash - F# doesn't do null really. *)</span> <span class="k">let</span> <span class="n">inputs</span> <span class="o">=</span> <span class="p">[</span> <span class="s2">"Food"</span><span class="p">;</span> <span class="s2">"Foo"</span><span class="p">;</span> <span class="nn">Unchecked</span><span class="p">.</span><span class="n">defaultof</span><span class="o">&lt;</span><span class="nc">String</span><span class="o">&gt;;</span> <span class="p">]</span> <span class="k">let</span> <span class="n">complexOp</span> <span class="p">(</span><span class="n">input</span><span class="o">:</span> <span class="kt">string</span><span class="p">)</span> <span class="o">=</span> <span class="k">try</span> <span class="k">if</span> <span class="n">input</span><span class="o">.</span><span class="nc">Length</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">=</span> <span class="mi">0</span> <span class="k">then</span> <span class="nc">Success</span> <span class="n">input</span><span class="o">.</span><span class="nc">Length</span> <span class="k">else</span> <span class="nc">Failure</span> <span class="s2">"Length is odd."</span> <span class="k">with</span> <span class="o">|</span> <span class="n">ex</span> <span class="o">-&gt;</span> <span class="nc">Error</span> <span class="n">ex</span> <span class="k">let</span> <span class="n">processResult</span> <span class="n">r</span> <span class="o">=</span> <span class="k">match</span> <span class="n">r</span> <span class="k">with</span> <span class="o">|</span> <span class="nc">Success</span> <span class="n">s</span> <span class="o">-&gt;</span> <span class="n">printfn</span> <span class="s2">"Even length: %d"</span> <span class="n">s</span> <span class="o">|</span> <span class="nc">Failure</span> <span class="n">f</span> <span class="o">-&gt;</span> <span class="n">printfn</span> <span class="s2">"%s"</span> <span class="n">f</span> <span class="o">|</span> <span class="nc">Error</span> <span class="n">e</span> <span class="o">-&gt;</span> <span class="n">printfn</span> <span class="s2">"%A"</span> <span class="n">e</span> <span class="k">let</span> <span class="n">main</span> <span class="bp">()</span> <span class="o">=</span> <span class="k">let</span> <span class="n">results</span> <span class="o">=</span> <span class="n">inputs</span> <span class="o">|&gt;</span> <span class="nn">Seq</span><span class="p">.</span><span class="n">map</span> <span class="n">complexOp</span> <span class="n">results</span> <span class="o">|&gt;</span> <span class="nn">Seq</span><span class="p">.</span><span class="n">iter</span> <span class="n">processResult</span> <span class="n">main</span> <span class="bp">()</span> </code></pre></div></div> <p>I’ve modelled the result as a <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/discriminated-unions">Discriminated Union</a> with 3 cases, one for each outcome. The complex operation, like the C# version returns 1 of these 3 cases. What is nice in F# is that in <code class="highlighter-rouge">processResult</code> where I take in a single result and handle it, the pattern match must be complete. If I added another case to the <code class="highlighter-rouge">Result</code> type, the compiler will complain that is isn’t handled in the <code class="highlighter-rouge">match</code>.</p> <h1 id="conclusion">Conclusion</h1> <p>This won’t be an exhaustive list of ways to handle results, but it does provide some different approaches to the problem that should help keep your code base a little cleaner. Options 6 and 7 are ones I would use in C#, the rest create unreasonable code that I would not like to have to think about. The complex operation is never going to be a few lines of code like in my scenario, it might be many classes working to do many different operations, building one final result. I like it when I don’t have to know the implementation details of an operation to know what the behaviour is for a given outcome.</p> <p>Above, I have only used Success, Failure and Error as the outcomes of my operation, but I could have modelled different states for success too: a MatchFound/NoMatch result could be suitable for a result type.</p> Sun, 24 Sep 2017 18:41:00 +0000 https://taeguk.co.uk/blog/handling-results-from-complex-operations/ https://taeguk.co.uk/blog/handling-results-from-complex-operations/ blog C# Controlling VS2017 Developer Console Start Directory <p>At work I use <a href="https://conemu.github.io/">ConEmu</a> for my console, it’s a great console to work on Windows with. To keep things tidy I have all my code on my <code class="highlighter-rouge">X:\</code> partition. In ConEmu I have different “Tasks” setup for different configurations of Visual Studio and pass <code class="highlighter-rouge">/Dir X:\</code> as one of the task parameters so that a new Console’s current Dir is <code class="highlighter-rouge">X:\</code>.</p> <p><img src="/blog/Content/vs2017-cmd-conemu.png" alt="ConEnum Settings" /></p> <p>When running “Developer Command Prompt for VS 2017” on my work computer I noticed that the directory it was opening in wasn’t the current directory that ConEmu was setting, but <code class="highlighter-rouge">C:\Dave\Source</code>.</p> <pre><code class="language-plain">********************************************************************** ** Visual Studio 2017 Developer Command Prompt v15.0.26228.9 ** Copyright (c) 2017 Microsoft Corporation ********************************************************************** C:\Dave\Source&gt; </code></pre> <p>After a bit of digging through the batch files I found the reason for this is because of this bit code in:</p> <p><code class="highlighter-rouge">C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\vsdevcmd\core\vsdevcmd_end.bat</code></p> <pre><code class="language-batch">... @REM Set the current directory that users will be set after the script completes @REM in the following order: @REM 1. [VSCMD_START_DIR] will be used if specified in the user environment @REM 2. [USERPROFILE]\source if it exists @REM 3. current directory if "%VSCMD_START_DIR%" NEQ "" ( cd /d "%VSCMD_START_DIR%" ) else ( if EXIST "%USERPROFILE%\Source" ( cd /d "%USERPROFILE%\Source" ) ) ... </code></pre> <p>As you can see, it has two chances to pick a different directory before using your current one.</p> <p>In my case, I had a folder at <code class="highlighter-rouge">%USERPROFILE%\Source</code>, which was empty, so I deleted it.</p> <p>The other alternative is to set the <code class="highlighter-rouge">VSCMD_START_DIR</code> environment variable for your user account to your preferred directory.</p> Tue, 28 Mar 2017 09:20:00 +0000 https://taeguk.co.uk/blog/controlling-vs2017-developer-console-start-directory/ https://taeguk.co.uk/blog/controlling-vs2017-developer-console-start-directory/ blog Visual Studio TFS 2015 does not support 2012 build agents <p>This post is part PSA, part debugging story.</p> <p>The important bit:</p> <h1 id="team-foundation-server-2012-xaml-build-agents-do-not-work-with-tfs-2015">Team Foundation Server 2012 XAML Build Agents do not work with TFS 2015</h1> <p>I discover this fact the weekend just gone whilst performing an upgrade to TFS 2015.3 from TFS 2012.4.</p> <p>The plan was to only upgrade the TFS Server and leave the build infrastructure running on TFS 2012. This seemed like a sound idea as I know Microsoft care about compatibility, and the upgrade was more complicated than your usual one. I figured it would just keep working and that I’d upgrade the build agents later, boy was I wrong.</p> <p>I may have even checked the <a href="https://www.visualstudio.com/en-us/docs/setup-admin/requirements">documentation</a>, which does not show a compatibility, but it isn’t explicitly called out, so I could have glanced over it.</p> <p><img src="/blog/Content/tfs2015-build-compat-table.png" alt="TFS build compatibility" /> <em>Look - No 2012</em></p> <p>The problems with TFS 2012 build agents against TFS 2015 manifested as two different errors when I queued a build without a Drop Location. Queuing a build with a drop location worked just fine.</p> <h2 id="error-1---build-agents-not-using-the-fqdn">Error 1 - Build agents not using the FQDN</h2> <p>The build infrastructure runs on a different domain to the Team Foundation Server.</p> <p>We have <code class="highlighter-rouge">tfs-server.corp.com</code> for TFS and <code class="highlighter-rouge">build-server.corp-development.com</code> for builds.</p> <p>The error manifested as:</p> <p><img src="/blog/Content/tfs2015-build-fqdn-error.png" alt="FQDN error message" /></p> <p>The error that appeared twice was not very helpful.</p> <blockquote> <p>An error occurred while copying diagnostic activity logs to the drop location. Details: An error occurred while sending the request.</p> </blockquote> <p>I eventually debugged this (details later) and found out that the last task on the build agent was trying to access <code class="highlighter-rouge">tfs-server</code> with no DNS suffix of <code class="highlighter-rouge">.corp.com</code> to publish some logs. As a temporary workaround I bobbed an entry in the <a href="https://en.wikipedia.org/wiki/Hosts_(file)">hosts</a> file entry to make <code class="highlighter-rouge">tfs-server</code> point to the actual IP of the TFS server.</p> <h2 id="error-2---the-bad-request">Error 2 - the bad request</h2> <p>With the all the steps of the build resolving the server name, I came across the second error.</p> <p><img src="/blog/Content/tfs2015-build-bad-request.png" alt="Bad request error message" /></p> <p>The error message was still no more use than the last one:</p> <blockquote> <p>An error occurred while copying diagnostic activity logs to the drop location. Details: TF270002: An error occurred copying files from ‘C:\Users\tfsbuild\AppData\Local\Temp\BuildAgent\172\Logs\151436\LogsToCopy\ActivityLog.AgentScope.172.xml’ to ‘ActivityLog.AgentScope.172.xml’. Details: BadRequest: Bad Request</p> <p>An error occurred while copying diagnostic activity logs to the drop location. Details: An error occurred while sending the request.</p> </blockquote> <p>My debugging would lead me to see that this was caused by TFS returning an HTTP 400 (Bad Request) for the exact same step as the first error.</p> <p>It was at this point I figured something was really wrong and started searching for compatibility problems. In my effort to find a KB or update I re-checked the documentation and noticed the lack of support as well as finding <a href="https://social.msdn.microsoft.com/Forums/office/en-US/68d84ffc-3bcc-41cc-80f0-8fc778894ee4/tfs-online-build-fails-on-local-build-server-with-tf270016-tf270002?forum=tfsbuild">an MSDN forum post</a> from RicRak where they solved the problem by upgrading their agents off of TFS 2012.</p> <h2 id="solution">Solution</h2> <p>My solution was to upgrade our entire build infrastructure (some 9/10 servers) to TFS 2015, and discovering you <strong>must</strong> install VS2015 on the servers too to get the Test Runner to work.</p> <p>One day of diagnosis and testing to get to the point of knowing TFS 2015 build agents would solve the problem <strong>and</strong> still build our codebase. Another half-day was spend upgrading all the servers.</p> <h1 id="diagnostics">Diagnostics</h1> <p>How do you figure out when something like this goes wrong? TFS diagnostic logging did not provide any more information than minimum logging did. The error only appeared at the very end of a build, it wasn’t related to a step in the XAML workflow, nor any variables in the build process.</p> <p>The solution (as always) came from <a href="http://stackoverflow.com/questions/15143107/httpclient-httprequestexception">Charlie Kilian</a> on Stack Overflow.</p> <p>I stopped the Build Service and opened up <code class="highlighter-rouge">TFSBuildServiceHost.exe.config</code> and added the following section:</p> <div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;system.diagnostics&gt;</span> <span class="nt">&lt;sources&gt;</span> <span class="nt">&lt;source</span> <span class="na">name=</span><span class="s">"System.Net"</span> <span class="na">tracemode=</span><span class="s">"includehex"</span> <span class="na">maxdatasize=</span><span class="s">"1024"</span><span class="nt">&gt;</span> <span class="nt">&lt;listeners&gt;</span> <span class="nt">&lt;add</span> <span class="na">name=</span><span class="s">"System.Net"</span><span class="nt">/&gt;</span> <span class="nt">&lt;/listeners&gt;</span> <span class="nt">&lt;/source&gt;</span> <span class="nt">&lt;/sources&gt;</span> <span class="nt">&lt;switches&gt;</span> <span class="nt">&lt;add</span> <span class="na">name=</span><span class="s">"System.Net"</span> <span class="na">value=</span><span class="s">"Verbose"</span><span class="nt">/&gt;</span> <span class="nt">&lt;/switches&gt;</span> <span class="nt">&lt;sharedListeners&gt;</span> <span class="nt">&lt;add</span> <span class="na">name=</span><span class="s">"System.Net"</span> <span class="na">type=</span><span class="s">"System.Diagnostics.TextWriterTraceListener"</span> <span class="na">initializeData=</span><span class="s">"C:\Logs\network.log"</span> <span class="nt">/&gt;</span> <span class="nt">&lt;/sharedListeners&gt;</span> <span class="nt">&lt;trace</span> <span class="na">autoflush=</span><span class="s">"true"</span><span class="nt">/&gt;</span> <span class="nt">&lt;/system.diagnostics&gt;</span> </code></pre></div></div> <p>Then restarted the build service and ran the smallest build I could to produce minimal logs.</p> <p>The log folder looked something like this:</p> <p><img src="/blog/Content/tfs2015-build-log-files.png" alt="Log files on disk" /></p> <p>The <code class="highlighter-rouge">network.log</code> file had a few errors, but nothing fatal looking, so I looked in the other files for errors and finally found this line:</p> <pre><code class="language-plain">System.Net Error: 0 : [4916] Exception in HttpWebRequest#13319471:: - The remote name could not be resolved: 'tfs-server'. </code></pre> <p>That was proceeded by:</p> <pre><code class="language-plain">System.Net Verbose: 0 : [4928] HttpWebRequest#13319471::HttpWebRequest(http://tfs-server:8080/tfs/DefaultCollection/_apis/resources/containers/122598?itemPath=logs%2FActivityLog.AgentScope.172.xml#752534963) </code></pre> <p>Here you can see the server name without the necessary DNS suffix during some HTTP POST to <code class="highlighter-rouge">_apis/resources/containers</code>.</p> <p>This was the point I added the hosts file entry and then got the next error.</p> <p>For the second error I repeated the diagnostic logging steps and this time found the following errors (searching for Bad Request):</p> <pre><code class="language-plain">System.Net Information: 0 : [16628] Connection#50276392 - Received status line: Version=1.1, StatusCode=400, StatusDescription=Bad Request. </code></pre> <p>By tracing the ID (in this case <code class="highlighter-rouge">16628</code>) back up the file I found it was a call to the same endpoint, but this time a PUT:</p> <pre><code class="language-plain">System.Net Information: 0 : [16628] HttpWebRequest#9100089 - Request: PUT /tfs/DefaultCollection/_apis/resources/containers/122603?itemPath=logs%2FActivityLog.AgentScope.59.xml HTTP/1.1 </code></pre> <p>This was the point I gave up thinking this could be fixed by a configuration change.</p> <h1 id="conclusion">Conclusion</h1> <p>I wish I had read something like this before I planned the weekend. I did do testing, but because testing TFS in live is risky I had most of the test instance network isolated and that required a lot of configurations; I just thought this error was just configuration based, lesson well and truly learned.</p> <p>It would have been nice to see this called out more explicitly on MSDN. In my opinion these are two bugs that Microsoft decided not to fix in the TFS 2012 product life-cycle.</p> <p>On the plus side, I learned some really neat debugging skills I didn’t know before.</p> <p>Remember, if you’re upgrading from TFS 2012, plan to upgrade your build agents at the same time!</p> Thu, 10 Nov 2016 20:49:00 +0000 https://taeguk.co.uk/blog/tfs2015-does-not-support-2012-build-agents/ https://taeguk.co.uk/blog/tfs2015-does-not-support-2012-build-agents/ blog TFS Deployment Pipeline with VSTS and Release Management <p>Back in 2014 I wrote a <a href="/blog/unc-to-uri-path-converter/">UNC to URI Path Converter</a> using ASP MVC 4 and Visual Studio Team Services with a XAML Build process template to continuously deploy the changes to an Azure Website. This was my first Azure Website and most of it was just using the default settings from the New Project dialog in Visual Studio, all very “point and click”.</p> <p>It worked well and had an average of a few hundred page requests a week and so far, I’ve been happy with everything as it “just worked”. The other day I wanted to add a small feature and noticed that after pushing and deploying the change that Azure was warning me XAML builds would soon be deprecated. So, whilst I was making some changes I decided it would be a good opportunity for me to get up to date on a few new technologies that I have not used in anger.</p> <p>I planned to setup the following for the website:</p> <ul> <li>Rewrite in .NET Core.</li> <li>Custom VSTS Build vNext.</li> <li>Deployment Pipeline using Microsoft Release Management.</li> </ul> <h1 id="rewrite-in-net-core">Rewrite in .NET Core</h1> <p>My previous .NET Core app at this point was a console application, so I took this as an opportunity to get to grips with setting up a build and a suite of unit tests using xUnit.net. Getting this working in Visual Studio was straight forward following the <a href="https://xunit.github.io/docs/getting-started-dotnet-core.html">xUnit.net documentation</a>, but getting the build to run on VSTS was a bit hit and miss. I eventually settled on a mix-match combination of <code class="highlighter-rouge">dotnet</code> command line tools and the Visual Studio Test Runner.</p> <p><img src="/blog/Content/deployment-pipeline-build-steps.png" alt="VSTS Build Steps" /></p> <p>Using the VS Test step solved the problem with <code class="highlighter-rouge">dotnet test</code> not been able to run the xUnit.net tests on the build server. I kept the individual <code class="highlighter-rouge">dotnet restore</code>, <code class="highlighter-rouge">dotnet publish</code> (site) and <code class="highlighter-rouge">dotnet build</code> (tests) as I wanted control over the <code class="highlighter-rouge">publish</code>. I also have a suite of deployment tests that based on the Full .NET Framework which I build using VS Build. These were the building blocks of my pipeline.</p> <h1 id="custom-vsts-build-vnext">Custom VSTS Build vNext</h1> <p>By keeping control over <code class="highlighter-rouge">dotnet publish</code> I could <a href="https://docs.asp.net/en/latest/publishing/vsts-continuous-deployment.html">pack</a> the website ready to by pushed to Azure using Microsoft Release Management. I took the output of <code class="highlighter-rouge">dotnet publish</code> and zipped it up into an archive and published this as a build artifact.</p> <p>The build process also took the output of DeploymentTests build and zipped it into a separate archive and published that too.</p> <p>I now had a website and a suite of “Deployment Tests” as artifacts from my build.</p> <h1 id="deployment-pipeline-using-microsoft-release-management">Deployment Pipeline using Microsoft Release Management</h1> <p>A deployment pipeline is where code goes through various stages and <em>each stage provides increasing confidence, usually at the cost of extra time</em> (Martin Fowler: <a href="http://martinfowler.com/bliki/DeploymentPipeline.html">DeploymentPipeline</a>). My pipeline was quite simple:</p> <pre><code class="language-plain">Build -&gt; Fast Tests -&gt; Deploy to Pre-Prod -&gt; Test Via API -&gt; Deploy to Live -&gt; Test Via API </code></pre> <p>This process meant that the build was fast and only ran isolated fast unit tests against the code. Only then did it deploy onto a Pre-Production server (another Free Azure Website), and run a set of integration tests against the Website via the API, if these tests passed, then I repeated the process onto the Live website.</p> <p>Using Microsoft Release Management, I was able to orchestrate this using a single Release definition, and defining two environments to deploy to.</p> <p><img src="/blog/Content/deployment-pipeline-release.png" alt="Release Management" /></p> <p>I considered using Deployment Slots on Azure to do a deploy and then <em>swap</em> to the Slots after the tests passed, but Slots are only available on the Standard pricing tier and I wanted to keep this free, so I setup <a href="https://pathconverter-pp.azurewebsites.net">another free Website instance</a> and ran the tests on there.</p> <p>I used a Variable against each Environment in Release Management to store the Azure Website Name.</p> <p><img src="/blog/Content/deployment-pipeline-release-vars.png" alt="Environment's variables" /></p> <p>These variables had two uses, the first was to keep the steps for each environment the same, I only need to set the variable to a different value.</p> <p>The second was very cool, because the variables in TFS Build and RM are actually environment variables I could write the following method in the code of my deployment tests:</p> <pre><code class="language-C#">public static String BaseUri =&gt; $"http://{Environment.GetEnvironmentVariable("AzureWebSiteName")}.azurewebsites.net/"; </code></pre> <p>And then run the API integration tests against the value of <code class="highlighter-rouge">BaseUri</code>.</p> <p>I planned to write some User Interface tests using either Coded-UI or Selenium, but due to the Hosted Build agents not supported Interactive Mode which is needed to run User Interface tests, I made them conditional and they only run in Visual Studio locally. I do have a plan to get these running in the future.</p> <p>The whole process looks like this:</p> <p><img src="/blog/Content/deployment-pipeline-flowchart.png" alt="Deployment Pipeline Flowchart" /></p> <h1 id="conclusion">Conclusion</h1> <p>Whilst this is a massively over engineered solution for such a simple website, it was fun to learn some new tricks and understand how to put a release pipeline together using the VSTS and Azure platforms. I also used it as opportunity to tidy up my resources in Azure and consolidate all my related resources into an Azure RM Resource Group, including the Application Insights I use to monitor it.</p> Mon, 31 Oct 2016 20:34:00 +0000 https://taeguk.co.uk/blog/deployment-pipeline-with-vsts-and-release-management/ https://taeguk.co.uk/blog/deployment-pipeline-with-vsts-and-release-management/ blog VSTS .NET Core Release Management Now using SSL <p>Today I’ve changed over to using SSL by default.</p> <p><img src="/blog/Content/ssl.png" alt="SSL in Chrome" /></p> <p>The main reason for moving is that SSL <a href="http://www.troyhunt.com/2015/08/were-struggling-to-get-traction-with.html">gives better SEO</a> - and that my old blog was SSL so I’m sure there will be some SSL links scattered about the web. It also prevents any silly public networks injecting anything into any of my pages.</p> <p>I’m using CloudFlare to secure to the communications from your browser to them. Thanks to Sheharyar Naseer for his <a href="https://sheharyar.me/blog/free-ssl-for-github-pages-with-custom-domains/">excellent guide</a> that got me up and running in no time, and to <a href="https://dnsimple.com">DNSimple</a> for their excellent DNS Service that made it a piece of cake changing my Nameservers.</p> Sun, 03 Apr 2016 15:49:00 +0000 https://taeguk.co.uk/blog/now-using-ssl/ https://taeguk.co.uk/blog/now-using-ssl/ blog Meta Using SignalR in FSharp without Dynamic <p>I’ve been building an FSharp Dashboard by following along <a href="http://coding.fitness/f-powered-realtime-dashboard/">this post</a> from <a href="https://github.com/lbacaj">Louie Bacaj’s</a> which was part of last years FSharp Advent calendar. I have to say it’s a great post and has got me up and running in no time.</p> <blockquote> <p>If you want to skip the story and get to the FSharp and SignalR part scroll down to <strong>Changing the Hub</strong>.</p> </blockquote> <p>One small problem I noticed was that I could not use any of the features of FSharp Core v4. For example, the new <code class="highlighter-rouge">tryXXX</code> functions such as <code class="highlighter-rouge">Array.tryLast</code> were not available.</p> <p>After a bit of digging I happened across the Project Properties which were stuck on <code class="highlighter-rouge">3.1.2.1</code>.</p> <p><img src="/blog/Content/fsharp-signalr-project-props.png" alt="Project Properties" /></p> <p>Turns out that the <code class="highlighter-rouge">FSharp.Interop.Dynamic</code> package is dependant on <code class="highlighter-rouge">FSharp.Core v3.1.2.1</code>.</p> <p>So this turned into a challenge of how do I use SignalR without Dynamic. After a bit of googling I landed on <a href="http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server#stronglytypedhubs">this page</a> that showed Strongly Typed Hubs. So I knew it was possible…</p> <h1 id="removing-dependencies">Removing Dependencies</h1> <p>The first step to fixing this was to remove the <code class="highlighter-rouge">FSharp.Core</code> dependencies I no longer needed, these were:</p> <div class="language-posh highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Uninstall-Package FSharp.Interop.Dynamic Uninstall-Package Dynamitey Uninstall-Package FSharp.Core </code></pre></div></div> <p>I then just browsed through the source and removed all the <code class="highlighter-rouge">open</code> declarations.</p> <h1 id="re-adding-fsharp-core">Re-adding FSharp Core</h1> <p>Slight problem now, I no longer had any FSharp Core references, so I needed to add one in. I’m not sure if this is the best way to solve this, but I just copied and pasted these lines from a empty FSharp project I just created:</p> <div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;Reference</span> <span class="na">Include=</span><span class="s">"mscorlib"</span> <span class="nt">/&gt;</span> <span class="c">&lt;!--Add this bit--&gt;</span> <span class="nt">&lt;Reference</span> <span class="na">Include=</span><span class="s">"FSharp.Core, Version=$(TargetFSharpCoreVersion), Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"</span><span class="nt">&gt;</span> <span class="nt">&lt;Private&gt;</span>True<span class="nt">&lt;/Private&gt;</span> <span class="nt">&lt;/Reference&gt;</span> <span class="c">&lt;!--End--&gt;</span> <span class="nt">&lt;Reference</span> <span class="na">Include=</span><span class="s">"Newtonsoft.Json"</span><span class="nt">&gt;</span> </code></pre></div></div> <h1 id="changing-the-hub">Changing the Hub</h1> <p>Now all I had to do was update the code to use the statically typed hub.</p> <p>First step was to create an interface for the <code class="highlighter-rouge">metricsHub</code>:</p> <div class="language-fsharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">type</span> <span class="nc">IMetricsHub</span> <span class="p">=</span> <span class="k">abstract</span> <span class="k">member</span> <span class="nc">AddMessage</span><span class="p">:</span> <span class="kt">string</span> <span class="p">-&gt;</span> <span class="kt">unit</span> <span class="k">abstract</span> <span class="k">member</span> <span class="nc">BroadcastPerformance</span><span class="p">:</span> <span class="nc">PerfModel</span> <span class="n">seq</span> <span class="p">-&gt;</span> <span class="kt">unit</span> </code></pre></div></div> <p>Then change our <code class="highlighter-rouge">Hub</code> to inherit from the generic <code class="highlighter-rouge">Hub&lt;T&gt;</code>:</p> <div class="language-fsharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">[&lt;</span><span class="nc">HubName</span><span class="p">(</span><span class="s2">"metricsHub"</span><span class="o">)&gt;]</span> <span class="k">type</span> <span class="n">metricsHub</span><span class="bp">()</span> <span class="p">=</span> <span class="k">inherit</span> <span class="nc">Hub</span><span class="p">&lt;</span><span class="nc">IMetricsHub</span><span class="o">&gt;()</span> <span class="c1">// &lt; Generic version of our interface.</span> </code></pre></div></div> <p>And changed all the calls from:</p> <div class="language-fsharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nn">Clients</span><span class="p">.</span><span class="nc">All</span><span class="p">?</span><span class="n">message</span><span class="p">(</span><span class="n">message</span><span class="p">)</span> </code></pre></div></div> <p>to</p> <div class="language-fsharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nn">Clients</span><span class="p">.</span><span class="nn">All</span><span class="p">.</span><span class="nc">Message</span> <span class="n">message</span> </code></pre></div></div> <h1 id="getting-the-context">Getting the Context</h1> <p>With SignalR you cannot just <code class="highlighter-rouge">new</code> up an instance of a <code class="highlighter-rouge">Hub</code>, you have to use <code class="highlighter-rouge">GlobalHost.ConnectionManager.GetHubContext&lt;THub&gt;</code>. The problem is that this gives you and <code class="highlighter-rouge">IHubContext</code> which only exposes the dynamic interface again. A bit more googling and I found that you need to pass our interface as a second generic parameter and you will get an <code class="highlighter-rouge">IHubContext&lt;IMetricsHub&gt;</code>.</p> <p>So this:</p> <div class="language-fsharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">let</span> <span class="n">context</span> <span class="p">=</span> <span class="nn">GlobalHost</span><span class="p">.</span><span class="nn">ConnectionManager</span><span class="p">.</span><span class="nc">GetHubContext</span><span class="p">&lt;</span><span class="n">metricsHub</span><span class="o">&gt;()</span> </code></pre></div></div> <p>Becomes:</p> <div class="language-fsharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">let</span> <span class="n">context</span> <span class="p">=</span> <span class="nn">GlobalHost</span><span class="p">.</span><span class="nn">ConnectionManager</span><span class="p">.</span><span class="nc">GetHubContext</span><span class="p">&lt;</span><span class="n">metricsHub</span><span class="p">,</span> <span class="nc">IMetricsHub</span><span class="o">&gt;()</span> </code></pre></div></div> <p>Now you can call <code class="highlighter-rouge">Context.Clients.All.BroadcastPerformance</code> and not worry about that pesky dynamic any more.</p> <h1 id="conclusion">Conclusion</h1> <p>The documentation on SignalR isn’t very good, it was easy enough to find out about the statically typed version, but finding out how to get one out of the context was a right pain.</p> <p>I’ve published a fork of Louies GitHub repo with four commits that show the steps needed to move from dynamic to statically typed SignalR <a href="https://github.com/xdaDaveShaw/LouiesGuiAdventDash">here</a> so you can see the changes I needed to make.</p> Mon, 29 Feb 2016 21:13:00 +0000 https://taeguk.co.uk/blog/using-signalr-in-fsharp-without-dynamic/ https://taeguk.co.uk/blog/using-signalr-in-fsharp-without-dynamic/ blog FSharp Adding Cloudapp DNS to Azure VM <p>I’ve recently just deployed a new Azure Linux VM for hosting a <a href="https://discourse.org">Discourse</a> instance I run and noticed that is didn’t have a DNS entry on cloudapp.net. Last time I deployed one it was instantly given one in the format <code class="highlighter-rouge">server-name.cloudapp.net</code>, but this time it wasn’t and I had to set it up by myself.</p> <p>I suspect it is something new for <a href="https://azure.microsoft.com/en-gb/features/resource-manager/">Resource Managed</a> deployments.</p> <p>Here’s a list of the steps you need to follow if you ever need to do the same.</p> <p>Assuming you have just deployed a VM and it doesn’t have a DNS on cloudapp.net you will see something like this:</p> <p><img src="/blog/Content/azure-dns-new-vm.png" alt="newly deployed vm" /></p> <h2 id="dissociate-public-ip">Dissociate Public IP</h2> <p>First you need to Dissociate the Public IP so you can make changes.</p> <p>Click the <strong>Public IP Address</strong> to open the settings:</p> <p><img src="/blog/Content/azure-dns-public-ip.png" alt="public ip settings" /></p> <p>Then click <strong>Dissociate</strong> and confirm when prompted.</p> <p><img src="/blog/Content/azure-dns-public-ip-settings.png" alt="public ip settings dissociate" /></p> <blockquote> <p>You cannot change any settings whilst the Public IP is in use.</p> </blockquote> <h2 id="configuring-the-dns">Configuring the DNS</h2> <p>From the Public IP page, click <strong>All Settings</strong> then <strong>Configuration</strong> to open up the settings:</p> <p><img src="/blog/Content/azure-dns-public-ip-configuration.png" alt="public ip settings configuration" /></p> <p>Then you can enter a new DNS prefix for <em>datacentre</em>.cloudapp.azure.net:</p> <p><img src="/blog/Content/azure-dns-public-ip-configuration-new-dns.png" alt="public ip configuration new dns" /></p> <h2 id="reassociate-the-public-ip">Reassociate the Public IP</h2> <p>Now you need to reassociate the Public IP with the VM.</p> <p>From the VM Screen (First Image) click <strong>All Settings</strong>, then <strong>Network Interfaces</strong>:</p> <p><img src="/blog/Content/azure-dns-vm-network-interfaces.png" alt="vm network interfaces" /></p> <p>Click on the Interface listed:</p> <p><img src="/blog/Content/azure-dns-vm-network-interfaces2.png" alt="all vm network interfaces" /></p> <p>Click on <strong>IP Addresses</strong> from the <strong>Settings</strong> blade:</p> <p><img src="/blog/Content/azure-dns-nic-ip-addresses.png" alt="network interfaces ip addresses" /></p> <p>Click on <strong>Enable</strong> then click on the <strong>IP Address Configure Required…</strong> and select the default (highlighted) Public IP Address from the list.</p> <p><img src="/blog/Content/azure-dns-enable-public-ip.png" alt="select public ip" />.</p> <p>Then click <strong>Save</strong>.</p> <h1 id="validation-and-testing">Validation and Testing</h1> <p>Now if you close and re-open the VM blade you should see a new Public IP address appear.</p> <p>Click on the <strong>Public IP Address</strong> to open the blade and you will see your full DNS Entry and a <strong>Copy to clipboard</strong> button when you hover on it:</p> <p><img src="/blog/Content/azure-dns-new-dns.png" alt="vm with new dns" /></p> <p>To test, ping the VM and see if the DNS resolves:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>C:\&gt; ping taeguk-test-dns.northeurope.cloudapp.azure.com Pinging taeguk-test-dns.northeurope.cloudapp.azure.com [40.127.129.7] </code></pre></div></div> <p>The requests will timeout because Azure has ICMP disabled, but so long as the DNS resolves, you’ve done it.</p> <h1 id="conclusion">Conclusion</h1> <p>This seems to be a change that I can’t find a source for to do with Resource Managed VM’s instead of Classic VM’s. It used to work OK on classic VM’s.</p> <p><strong>Note</strong>: I have deleted the VM in this post now.</p> Sun, 15 Nov 2015 20:26:00 +0000 https://taeguk.co.uk/blog/adding-cloudapp-dns-to-azure-vm/ https://taeguk.co.uk/blog/adding-cloudapp-dns-to-azure-vm/ blog Azure