This is a valid Atom 1.0 feed.
This feed is valid, but interoperability with the widest range of feed readers could be improved by implementing the following recommendations.
line 167, column 0: (4 occurrences) [help]
<script src="https://gist.github.com/a4e5566a53a4b4c72424c59fdbc4eba ...
... ished><updated>2016-12-29T00:00:00+00:00</updated><id>https://thomasfreu ...
^
<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.2">Jekyll</generator><link href="https://thomasfreudenberg.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://thomasfreudenberg.com/" rel="alternate" type="text/html" /><updated>2022-08-30T07:46:49+00:00</updated><id>https://thomasfreudenberg.com/feed.xml</id><title type="html">Thomas Freudenberg</title><subtitle>Thomas Freudenberg - Confessions of a caffeine addict</subtitle><author><name>Thomas Freudenberg</name></author><entry><title type="html">Async/await in Desktop Applications</title><link href="https://thomasfreudenberg.com/archive/2019/01/28/async-await-in-desktop-applications/" rel="alternate" type="text/html" title="Async/await in Desktop Applications" /><published>2019-01-28T00:00:00+00:00</published><updated>2019-01-28T00:00:00+00:00</updated><id>https://thomasfreudenberg.com/archive/2019/01/28/async-await-in-desktop-applications</id><content type="html" xml:base="https://thomasfreudenberg.com/archive/2019/01/28/async-await-in-desktop-applications/"><p><em>This is a transcript of a demonstration I gave in our company internally
about async/await “challenges” in UI programming. You can find the accompanying
repository in <a href="https://github.com/thoemmi/CrossThreadingTests">my repository on Github</a>.</em></p>
<p><img src="/files/archive/async-await-winforms-app.png" alt="Demo App" title="Demo App" class="align-center" /></p>
<p>The form contains three buttons which are intended to download the
the content from <a href="http://microsoft.com">http://microsoft.com</a> asynchronously and show it
in a TextBox.</p>
<h2 id="cross-thread-issue-">Cross-Thread issue :(</h2>
<div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">private</span> <span class="k">async</span> <span class="k">void</span> <span class="nf">button1_Click</span><span class="p">(</span><span class="kt">object</span> <span class="n">sender</span><span class="p">,</span> <span class="n">EventArgs</span> <span class="n">e</span><span class="p">)</span>
<span class="p">{</span>
<span class="c1">// await a completed task =&gt; will continue synchronously</span>
<span class="k">await</span> <span class="nf">SomeFastAsyncOperation</span><span class="p">().</span><span class="nf">ConfigureAwait</span><span class="p">(</span><span class="k">false</span><span class="p">);</span>
<span class="c1">// await a slow task =&gt; will continue in another thread</span>
<span class="kt">var</span> <span class="n">t</span> <span class="p">=</span> <span class="k">await</span> <span class="nf">SomeSlowAsyncOperation</span><span class="p">().</span><span class="nf">ConfigureAwait</span><span class="p">(</span><span class="k">false</span><span class="p">);</span>
<span class="c1">// write text to text box</span>
<span class="n">textBox1</span><span class="p">.</span><span class="n">Text</span> <span class="p">=</span> <span class="n">t</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>
<p>When clicking the first button, an <code class="language-plaintext highlighter-rouge">InvalidOperationException</code> will
be thrown with the message</p>
<blockquote>
<p>Cross-thread operation not valid: Control ‘textBox1’ accessed from a
thread other than the thread it was created on.</p>
</blockquote>
<p>That’s because in Win32 UIs you must access controls from the same thread
that created them. Because the after awaiting <code class="language-plaintext highlighter-rouge">SomeSlowAsyncOperation</code>
the method continues not in the main thread but a nackground thread,
accessing <code class="language-plaintext highlighter-rouge">textBox1</code> is forbidden.</p>
<p>When you debug <code class="language-plaintext highlighter-rouge">button1_Click</code>, pay attention to the <em>Threads</em> tool window.</p>
<p><img src="/files/archive/async-await-debugger.png" alt="Debugger" title="Debugger Thread Toolwindow" class="align-center" /></p>
<ol>
<li>Entering the method, you’ll be on thread #1, the main thread.</li>
<li>After calling <code class="language-plaintext highlighter-rouge">SomeFastAsyncOperation</code>, the code continues on thread #1.
That’s because the method returns an already completed task, so the code
can continue synchronously.</li>
<li>In contrast, <code class="language-plaintext highlighter-rouge">SomeSlowAsyncOperation</code> returns a not-completed task,
therefore the succeeding code will continue not another one. (The UI thread
will be released here and continue pumping the Win32 message queue)</li>
<li>The property <code class="language-plaintext highlighter-rouge">textBox1.Text</code> will be set in said background thread and
fail, because Win32 controls mist be accessed from the same thread that
created them.</li>
</ol>
<h2 id="blocking-">Blocking :(</h2>
<div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">private</span> <span class="k">async</span> <span class="k">void</span> <span class="nf">button2_Click</span><span class="p">(</span><span class="kt">object</span> <span class="n">sender</span><span class="p">,</span> <span class="n">EventArgs</span> <span class="n">e</span><span class="p">)</span>
<span class="p">{</span>
<span class="c1">// await a completed task =&gt; will continue synchronously</span>
<span class="k">await</span> <span class="nf">SomeFastAsyncOperation</span><span class="p">().</span><span class="nf">ConfigureAwait</span><span class="p">(</span><span class="k">false</span><span class="p">);</span>
<span class="c1">// await a slow task =&gt; will continue in another thread</span>
<span class="kt">var</span> <span class="n">t</span> <span class="p">=</span> <span class="nf">SomeSlowAsyncOperation</span><span class="p">().</span><span class="nf">ConfigureAwait</span><span class="p">(</span><span class="k">false</span><span class="p">).</span><span class="nf">GetAwaiter</span><span class="p">().</span><span class="nf">GetResult</span><span class="p">();</span>
<span class="c1">// write text to text box</span>
<span class="n">textBox1</span><span class="p">.</span><span class="n">Text</span> <span class="p">=</span> <span class="n">t</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>
<p>Clicking the second button will freeze the application. The
<code class="language-plaintext highlighter-rouge">GetAwaiter().GetResult()</code> invocation will try to re-enter the
the main thread, which is waiting for the task, so we’ll run into a dead-lock.</p>
<h2 id="run-smoothly-">Run smoothly :)</h2>
<div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">private</span> <span class="k">async</span> <span class="k">void</span> <span class="nf">button3_Click</span><span class="p">(</span><span class="kt">object</span> <span class="n">sender</span><span class="p">,</span> <span class="n">EventArgs</span> <span class="n">e</span><span class="p">)</span>
<span class="p">{</span>
<span class="c1">// force switch to threadpool thread</span>
<span class="k">await</span> <span class="n">TaskScheduler</span><span class="p">.</span><span class="n">Default</span><span class="p">;</span>
<span class="c1">// await a completed task =&gt; will continue synchronously</span>
<span class="k">await</span> <span class="nf">SomeFastAsyncOperation</span><span class="p">().</span><span class="nf">ConfigureAwait</span><span class="p">(</span><span class="k">false</span><span class="p">);</span>
<span class="c1">// await a slow task =&gt; will continue in another thread</span>
<span class="kt">var</span> <span class="n">t</span> <span class="p">=</span> <span class="k">await</span> <span class="nf">SomeSlowAsyncOperation</span><span class="p">().</span><span class="nf">ConfigureAwait</span><span class="p">(</span><span class="k">false</span><span class="p">);</span>
<span class="c1">// switch to main thread</span>
<span class="k">await</span> <span class="n">_joinableTaskFactory</span><span class="p">.</span><span class="nf">SwitchToMainThreadAsync</span><span class="p">();</span>
<span class="c1">// write text to text box</span>
<span class="n">textBox1</span><span class="p">.</span><span class="n">Text</span> <span class="p">=</span> <span class="n">t</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>
<p>Using the <code class="language-plaintext highlighter-rouge">JoinableTaskFactory</code> from <a href="https://github.com/Microsoft/vs-threading"><em>Microsoft.VisualStudio.Threading</em></a>
(NuGet package <a href="https://nuget.org/packages/Microsoft.VisualStudio.Threading">here</a>),
we are able to “switch” back to the UI thread.</p>
<p>As the namespace implies, <em>Microsoft.VisualStudio.Threading</em> originates from
the Visual Studio team. I stumbled over this library while reading the documentation
for Visual Studio extensibility. Visual Studio is quite a complex application, and
there are myriads of extensions available. To improve the start-up time, Microsoft
strongly recommends to make use of asynchronous programming (see
<a href="https://docs.microsoft.com/en-us/visualstudio/extensibility/managing-multiple-threads-in-managed-code?view=vs-2017"><em>How to: Manage multiple threads in managed code</em></a>
and <a href="https://docs.microsoft.com/en-us/visualstudio/extensibility/how-to-use-asyncpackage-to-load-vspackages-in-the-background?view=vs-2017"><em>How to: Use AsyncPackage to load VSPackages in the background</em></a>)</p>
<p>I won’t go into details of how async/await works. Basically, the compiler generates
a state machine, which Dixin explains pretty good in his blog serie
<a href="https://weblogs.asp.net/dixin/understanding-c-sharp-async-await-1-compilation"><em>Understanding C# async / await (1) Compilation</em></a>
(<a href="https://weblogs.asp.net/dixin/understanding-c-sharp-async-await-2-awaitable-awaiter-pattern">Part 2</a>,
<a href="https://weblogs.asp.net/dixin/understanding-c-sharp-async-await-3-runtime-context">Part 3</a>).</p>
<p>In our case, two calls are interesting:</p>
<ol>
<li><code class="language-plaintext highlighter-rouge">await TaskScheduler.Default;</code> will continue the succeeding code in a threadpool thread.<br />
<small>(actually, the library provides an extension method <code class="language-plaintext highlighter-rouge">GetAwaiter(this TaskScheduler this)</code>.
This works because the compiler uses a naming convention instead of requiring an interface implemenntation)</small></li>
<li><code class="language-plaintext highlighter-rouge">await _joinableTaskFactory.SwitchToMainThreadAsync();</code> will continue the succeeding code in the
main thread.<br />
<small>(actually, in the thread with instantiated <code class="language-plaintext highlighter-rouge">_joinableTaskFactory</code>).</small></li>
</ol>
<p>As you could see, <em>Microsoft.VisualStudio.Threading</em> makes asynchronous programming
in desktop applications, both WinForms and WPF, much simpler.</p>
<p>BTW, I’ve learned a lot reading the <a href="https://github.com/Microsoft/vs-threading">code of that library</a>. It provides
much more async helpers like <code class="language-plaintext highlighter-rouge">AsyncEventHandlers</code>.</p>
<p>Here are some more links if you want to learn more about async programming:</p>
<ul>
<li><a href="https://www.slideshare.net/aarnott/the-3-vs-threading-rules"><em>The 3 VS Threading Rules</em></a> by Andrew Arnott</li>
<li><a href="https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html"><em>Don’t Block on Async Code</em></a> by Stephen Cleary</li>
<li><a href="https://lesen.amazon.de/kp/embed?asin=B00KCY2CB4&amp;preview=newtab&amp;linkCode=kpe&amp;ref_=cm_sw_r_kb_dp_WWQtCbZETE973"><em>Concurrency in C# Cookbook: Asynchronous, Parallel, and Multithreaded Programming</em></a> by Stephen Cleary</li>
</ul></content><author><name>Thomas Freudenberg</name></author><category term="async" /><category term="WinForms" /><category term="WPF" /><category term="multi-threading" /><summary type="html">This is a transcript of a demonstration I gave in our company internally about async/await “challenges” in UI programming. You can find the accompanying repository in my repository on Github.</summary></entry><entry><title type="html">Generating workflow diagrams for TFS work items</title><link href="https://thomasfreudenberg.com/archive/2018/01/16/generating-workflow-diagram-for-witd/" rel="alternate" type="text/html" title="Generating workflow diagrams for TFS work items" /><published>2018-01-16T00:00:00+00:00</published><updated>2018-01-16T00:00:00+00:00</updated><id>https://thomasfreudenberg.com/archive/2018/01/16/generating-workflow-diagram-for-witd</id><content type="html" xml:base="https://thomasfreudenberg.com/archive/2018/01/16/generating-workflow-diagram-for-witd/"><p>In my current position as the Technical Lead of Product Development I have several
responsibilities. One of them is the definition and implementation of our development
processes. We’re using Team Foundation Server, which supports rich customization
of the process configuration, especially the workflow of work items.</p>
<p>To document the workflow of our work items, I wanted to create perspicuous charts.
However, if you’re a nerd like me, you don’t want to use Powerpoint or Visio to
create high gloss charts. Instead I like to automate the creation of the charts.</p>
<p>Fortunately, the XML format of work item template definitions (WITD) is well-documented, see
<a href="https://docs.microsoft.com/en-us/vsts/work/customize/reference/all-workflow-xml-elements-reference?toc=/vsts/work/customize/toc.json&amp;bc=/vsts/work/customize/breadcrumb/toc.json">All WORKFLOW XML elements reference</a>.
To get the XML file of a WITD, you can use either the Visual Studio Add-in
<a href="https://marketplace.visualstudio.com/items?itemName=KarthikBalasubramanianMSFT.TFSProcessTemplateEditor">TFS Process Template Editor</a>
or use <a href="https://docs.microsoft.com/en-us/vsts/work/customize/reference/witadmin/witadmin-import-export-manage-wits"><code class="language-plaintext highlighter-rouge">witadmin</code></a>:</p>
<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>witadmin exportwitd /collection:CollectionURL /p:Project /n:TypeName <span class="o">[</span>/f:FileName]
</code></pre></div></div>
<p>On the other end, the <a href="http://www.graphviz.org/">Graphviz suite</a> includes a nice
small tool named <strong><code class="language-plaintext highlighter-rouge">dot</code></strong> to draw directed graphs as PNGs, reading the defintion
of the graph from a text file.</p>
<p>The challenge was now to convert the XML of the WITD to the <a href="https://graphviz.gitlab.io/_pages/doc/info/lang.html">DOT language</a>.
But that’s quite easy to accomplish using Powershell. But before I show the script,
first a picture of the default workflow for bugs from the Scrum process template:</p>
<p><img src="/files/archive/witd.bug.default.png" alt="Pretzel and Azure" class="align-center" /></p>
<p>And here’s the script:</p>
<script src="https://gist.github.com/a4e5566a53a4b4c72424c59fdbc4eba4.js?file=Create-WorkflowDiagramFromWitd.ps1"> </script>
<p>If you pay close attention, you may notice that if only certain users or groups
are permitted to change a work item to a specific state, the graph will show
this too. E.g. if only members of the QA are allowed to move a but from the
<em>Done</em> state, the graph will look like this:</p>
<p><img src="/files/archive/witd.bug.customized.png" alt="Pretzel and Azure" class="align-center" /></p>
<p>Nevertheless, the script was written in a short time, it does what it should
do without any error handling. However, it suits my needs. Maybe yours as well.</p></content><author><name>Thomas Freudenberg</name></author><category term="TFS" /><category term="Graphviz" /><category term="Powershell" /><summary type="html">In my current position as the Technical Lead of Product Development I have several responsibilities. One of them is the definition and implementation of our development processes. We’re using Team Foundation Server, which supports rich customization of the process configuration, especially the workflow of work items.</summary></entry><entry><title type="html">ResourceLib, PE Format, and WiX</title><link href="https://thomasfreudenberg.com/archive/2017/12/11/resourcelib-pe-format-and-wix/" rel="alternate" type="text/html" title="ResourceLib, PE Format, and WiX" /><published>2017-12-11T00:00:00+00:00</published><updated>2017-12-11T00:00:00+00:00</updated><id>https://thomasfreudenberg.com/archive/2017/12/11/resourcelib-pe-format-and-wix</id><content type="html" xml:base="https://thomasfreudenberg.com/archive/2017/12/11/resourcelib-pe-format-and-wix/"><p><em>Some time ago I reported a bug and provided a pull request to
<a href="https://github.com/resourcelib/resourcelib"><strong>resourcelib</strong></a>, a managed
library to read and write Win32 resources in executables or DLL’s.
And unawarely, the next morning I was a maintainer of that library.</em></p>
<p><em>This blog post is about an issue we’ve received: someone tried to
patched the Win32 resources of a</em> setup.exe <em>, an executable installer
created with <a href="http://wixtoolset.org/">WiX</a>. However, after changing
the resources with resourdelib, the setup didn’t work anymore.</em></p>
<p><em>I’ve spent some time investigating this issue using <a href="https://msdn.microsoft.com/de-de/library/756as972.aspx"><code class="language-plaintext highlighter-rouge">dumpbin</code></a>
and reading the <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms680547(v=vs.85).aspx#section_table__section_headers_">PE format specification</a>.
Because I don’t know how good Google is at indexing Github issues, I’ll
also post my analysis here in my blog for reference. The original
thread is <a href="https://github.com/resourcelib/resourcelib/issues/19#issuecomment-350891529">here</a>.</em></p>
<p><strong>TL;DR:</strong> to me it looks like WiX is doing it wrong.</p>
<p>According to the output of <code class="language-plaintext highlighter-rouge">dumpbin</code> there are 7 sections in the executable the issuer provided:</p>
<table>
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Range</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><code class="language-plaintext highlighter-rouge">.text</code></td>
<td>0x00000400 to 0x00049FFF</td>
</tr>
<tr>
<td>2</td>
<td><code class="language-plaintext highlighter-rouge">.rdata</code></td>
<td>0x0004A000 to 0x00068DFF</td>
</tr>
<tr>
<td>3</td>
<td><code class="language-plaintext highlighter-rouge">.data</code></td>
<td>0x00068E00 to 0x000697FF</td>
</tr>
<tr>
<td>4</td>
<td><code class="language-plaintext highlighter-rouge">.wixburn</code></td>
<td>0x00069800 to 0x000699FF</td>
</tr>
<tr>
<td>5</td>
<td><code class="language-plaintext highlighter-rouge">.tls</code></td>
<td>0x00069A00 to 0x00069BFF</td>
</tr>
<tr>
<td>6</td>
<td><code class="language-plaintext highlighter-rouge">.rsrc</code></td>
<td>0x00069C00 to 0x0006D7FF</td>
</tr>
<tr>
<td>7</td>
<td><code class="language-plaintext highlighter-rouge">.reloc</code></td>
<td>0x0006D800 to 0x000715FF</td>
</tr>
</tbody>
</table>
<p>In a hex viewer you can see that after the last section, the file continues for another <strong>104205 bytes</strong>, starting with 0x4D 0x53 0x43 0x46 (<code class="language-plaintext highlighter-rouge">MSCF</code>, the magic number starting a <a href="https://de.wikipedia.org/wiki/CAB_(Dateiformat)">cab file</a>).</p>
<p>I patched the <code class="language-plaintext highlighter-rouge">StringFileInfo</code> resource using <strong>resourcelib</strong>, which changed the content of the <code class="language-plaintext highlighter-rouge">.rsrc</code> section only. Afterwards the file ended at 0x000715FF, i.e. the following <strong>104205 bytes</strong> were missing.</p>
<p>By the way, the <code class="language-plaintext highlighter-rouge">.wixburn</code> section contains following bytes:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>RAW DATA #4
0046C000: 00 43 F1 00 02 00 00 00 04 12 28 81 2C 64 40 48 .C±.......(.,d@H
0046C010: B2 B1 34 64 EC 08 65 64 00 16 07 00 00 00 00 00 ▓▒4d∞.ed........
0046C020: 00 00 00 00 00 00 00 00 01 00 00 00 02 00 00 00 ................
0046C030: 92 8E 01 00 7B 08 00 00 ....{...
</code></pre></div></div>
<p>which means</p>
<table>
<thead>
<tr>
<th>Field</th>
<th style="text-align: right">Bytes</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>magic number</td>
<td style="text-align: right">0-3</td>
<td>0x00f14300</td>
</tr>
<tr>
<td>Version</td>
<td style="text-align: right">4-7</td>
<td>0x00000002</td>
</tr>
<tr>
<td>Bundled GUID</td>
<td style="text-align: right">8-23</td>
<td>{81281204-642c-4840-b2b1-3464ec086564}</td>
</tr>
<tr>
<td>Engine (stub) size</td>
<td style="text-align: right">24-27</td>
<td>0x00071600</td>
</tr>
<tr>
<td>Original checksum</td>
<td style="text-align: right">28-31</td>
<td>0x00000000</td>
</tr>
<tr>
<td>Original signature offset</td>
<td style="text-align: right">32-35</td>
<td>0x00000000</td>
</tr>
<tr>
<td>Original signature size</td>
<td style="text-align: right">36-39</td>
<td>0x00000000</td>
</tr>
<tr>
<td>Container Type (1 = CAB)</td>
<td style="text-align: right">40-43</td>
<td>1</td>
</tr>
<tr>
<td>Container Count</td>
<td style="text-align: right">44-47</td>
<td>2</td>
</tr>
<tr>
<td>Byte count of manifest + UX container</td>
<td style="text-align: right">48-51</td>
<td>102,034</td>
</tr>
<tr>
<td>Byte count of attached container</td>
<td style="text-align: right">52-55</td>
<td>2,171</td>
</tr>
</tbody>
</table>
<h3 id="intermediateresult">Intermediate result</h3>
<ul>
<li>the <code class="language-plaintext highlighter-rouge">.wixburn</code> section points to 104,205 bytes (102,034 + 2,171), starting at 0x00071600.</li>
<li>the last PE section ends at 0x000715ff.</li>
<li>after using the official Win32 API to edit resources, the file ends at 0x000715ff, and the following 104,205 byte are gone.</li>
</ul>
<p>So after editing the resources, the exact payload WiX is referring to is eliminated.</p>
<h3 id="therefore-my-conclusion-is-that-wix">Therefore my conclusion is that WiX</h3>
<ul>
<li>adds a (small) section <code class="language-plaintext highlighter-rouge">.wixburn</code> pointing beyond the last section, and</li>
<li>appends the payload (read: the cabinet file) at that location-</li>
</ul>
<p>As far as I understand the specification, this procedure is not compliant with the PE format. That might be the reason why <code class="language-plaintext highlighter-rouge">EndUpdateResource</code> cuts the file after the last section when writing the changed resources.</p></content><author><name>Thomas Freudenberg</name></author><category term="WiX" /><category term="ResourceLib" /><summary type="html">Some time ago I reported a bug and provided a pull request to resourcelib, a managed library to read and write Win32 resources in executables or DLL’s. And unawarely, the next morning I was a maintainer of that library.</summary></entry><entry><title type="html">Cleaning NuGet’s cache</title><link href="https://thomasfreudenberg.com/archive/2017/04/21/cleaning-nugets-cache/" rel="alternate" type="text/html" title="Cleaning NuGet’s cache" /><published>2017-04-21T00:00:00+00:00</published><updated>2017-04-21T00:00:00+00:00</updated><id>https://thomasfreudenberg.com/archive/2017/04/21/cleaning-nugets-cache</id><content type="html" xml:base="https://thomasfreudenberg.com/archive/2017/04/21/cleaning-nugets-cache/"><p>From the beginning NuGet used a per-solution folder <code class="language-plaintext highlighter-rouge">packages</code> to store all packages for
the projects in a solution. (Does anyone else remember the numerous discussion whether
that folder belongs into the VCS or not?).</p>
<p><a href="http://blog.nuget.org/20151008/NuGet-3-What-and-Why.html">That changed</a> with NuGet 3 and
<code class="language-plaintext highlighter-rouge">project.json</code>-based projects:</p>
<blockquote>
<h3 id="global-packages-folder">Global Packages Folder</h3>
<p>With Project.JSON managed projects, there is now a packages folder that is shared for
all projects that you work with. Packages are downloaded and stored in the
<code class="language-plaintext highlighter-rouge">%userprofile%\.nuget\packages</code> folder. This means that if you are working on multiple
UWP projects on your workstation, you only have one copy of the EntityFramework package
and its dependencies on your machine. All .NET projects will acquire package references
from this global shared folder. This also means that when you need to configure a new
project, your project will not take time starting so that it can download a fresh copy
of <code class="language-plaintext highlighter-rouge">EntityFramework.nupkg</code> Instead, it will simply and quickly reference the files you
have already downloaded. ASP.NET 5 uses the <code class="language-plaintext highlighter-rouge">%userprofile%\.dnx\packages</code> folder and as
that framework nears completion it will use the <code class="language-plaintext highlighter-rouge">%userprofile%\.nuget\packages</code> folder
as well.</p>
</blockquote>
<p>Well, I didn’t pay much attention to that change, until I ran out of disk space last
week and used <a href="https://windirstat.net/">WinDirTree</a> to find the culprit. Indeed, the size
of my packages folder was more than 6 GB.</p>
<p>Therefore I wrote a small PowerShell script which deletes all packages which haven’t
been accessed for a configurable number of days (150 by default):</p>
<script src="https://gist.github.com/c76f1a5533fa86e631b2ed9bbc43c412.js?file=Clear-NuGetCache.ps1"> </script>
<p>Don’t worry that you could delete a package which you would need later again. NuGet will
just download the missing package again.</p>
<p>The script support the <code class="language-plaintext highlighter-rouge">-WhatIf</code> parameter, so calling</p>
<div class="language-powershell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">.</span><span class="n">\Clear-NuGetCache.ps1</span><span class="w"> </span><span class="nt">-CutOffDays</span><span class="w"> </span><span class="nx">90</span><span class="w"> </span><span class="nt">-WhatIf</span><span class="w">
</span></code></pre></div></div>
<p>wouldn’t delete a single byte but log which folders the script would remove.</p></content><author><name>Thomas Freudenberg</name></author><category term="NuGet" /><category term="PowerShell" /><summary type="html">From the beginning NuGet used a per-solution folder packages to store all packages for the projects in a solution. (Does anyone else remember the numerous discussion whether that folder belongs into the VCS or not?).</summary></entry><entry><title type="html">Pretzel and Kudu on Azure</title><link href="https://thomasfreudenberg.com/archive/2017/03/21/pretzel-and-kudu-on-azure/" rel="alternate" type="text/html" title="Pretzel and Kudu on Azure" /><published>2017-03-21T00:00:00+00:00</published><updated>2017-03-21T00:00:00+00:00</updated><id>https://thomasfreudenberg.com/archive/2017/03/21/pretzel-and-kudu-on-azure</id><content type="html" xml:base="https://thomasfreudenberg.com/archive/2017/03/21/pretzel-and-kudu-on-azure/"><p>I’ve published a couple of posts about that I’m using <a href="https://github.com/Code52/pretzel">Pretzel</a>
to generate the HTML pages of my blog. However, I didn’t talk about the hosting.</p>
<p>Actually, it’s quite simple: The source files for the site are hosted in a
<a href="https://github.com/thoemmi/thomasfreudenberg.com">git repository on GitHub</a>. The generated site is
hosted in Azure. Whenever I push changes to the git repository, the web site will be updated
automatically.</p>
<p><img src="/files/archive/pretzel-and-azure.png" alt="Pretzel and Azure" class="align-center" /></p>
<p>The setup is a two-stage process: first, you have to create a Azure App Service and connect it to
your git repository. The steps involved are documented very well in
<a href="https://docs.microsoft.com/en-us/azure/app-service-web/app-service-continuous-deployment">Continuous Deployment to Azure App Service</a>.</p>
<p>The second step is to execute Pretzel on the Azure side. Enter <strong>Kudu</strong>. Kudu is the engine
behind git deployments in Azure. It’s well documented in the <a href="https://github.com/projectkudu/kudu/wiki">wiki at GitHub</a>.
<a href="https://github.com/projectkudu/kudu/wiki/Deployment">By default</a>, Kudu will locate the relevant
<code class="language-plaintext highlighter-rouge">csproj</code> file, compile it, and copy the artifacts to wwwroot. That’s why many web sizes running on
Azure contain an empty “shim project”.</p>
<p>However, you can simplify the setup by <a href="https://github.com/projectkudu/kudu/wiki/Customizing-deployments">customizing Kudu’s behavior</a>.
In my case I want Kudu to run <code class="language-plaintext highlighter-rouge">pretzel.exe</code> to generate the static HTML files from my sources:</p>
<ol>
<li>
<p>Add <code class="language-plaintext highlighter-rouge">pretzel.exe</code> (and all its dependencies) to your git repository (I’ve used a subfolder named
<code class="language-plaintext highlighter-rouge">_pretzel</code>)</p>
</li>
<li>
<p>Add a batch file <code class="language-plaintext highlighter-rouge">deploy.cmd</code> to execute <code class="language-plaintext highlighter-rouge">pretzel.exe</code>:</p>
<div class="language-batch highlighter-rouge"><div class="highlight"><pre class="highlight"><code>@echo <span class="na">off</span>
<span class="nb">echo</span> <span class="kd">Running</span> <span class="kd">Pretzel</span>...
_pretzel\pretzel.exe <span class="kd">bake</span> <span class="na">--destination</span><span class="o">=</span><span class="nv">%DEPLOYMENT_TARGET%</span>
</code></pre></div> </div>
<p><code class="language-plaintext highlighter-rouge">bake</code> is the Pretzel’s command to generate the files, and the destination folder is <code class="language-plaintext highlighter-rouge">%DEPLOYMENT_TARGET%</code>,
which is the wwwroot folder.</p>
</li>
<li>
<p>Instruct Kudu to execute that <code class="language-plaintext highlighter-rouge">deploy.cmd</code> by creating a file <code class="language-plaintext highlighter-rouge">.deployment</code> with following
content:</p>
<div class="language-ini highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nn">[config]</span>
<span class="py">command</span> <span class="p">=</span> <span class="s">deploy.cmd</span>
</code></pre></div> </div>
</li>
</ol>
<p>That’s all. Whenever I push changes to the git repository, Kudu will get the current files,
execute Pretzel, and the updated web site is public. The whole process takes less than a minute.</p>
<p>Of course this can be adapted to any other static site generator too, e.g. Jekyll.</p></content><author><name>Thomas Freudenberg</name></author><category term="Pretzel" /><summary type="html">I’ve published a couple of posts about that I’m using Pretzel to generate the HTML pages of my blog. However, I didn’t talk about the hosting.</summary></entry><entry><title type="html">Encrypting values when serializing with JSON.NET</title><link href="https://thomasfreudenberg.com/archive/2017/02/11/encrypting-values-when-serializing-with-json-net/" rel="alternate" type="text/html" title="Encrypting values when serializing with JSON.NET" /><published>2017-02-11T00:00:00+00:00</published><updated>2017-02-11T00:00:00+00:00</updated><id>https://thomasfreudenberg.com/archive/2017/02/11/encrypting-values-when-serializing-with-json-net</id><content type="html" xml:base="https://thomasfreudenberg.com/archive/2017/02/11/encrypting-values-when-serializing-with-json-net/"><p>In a small inhouse app I wrote recently I store the settings in a json file, using the
popular <a href="http://www.newtonsoft.com/json">Json.NET</a> library. However, the settings
include a password, which should’t appear as clear text in the json file.</p>
<p>I stumbled over <a href="http://stackoverflow.com/a/29240043/4747">this</a> answer at Stack Overflow
by Brian Rogers. This solution uses a custom
<a href="http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Serialization_IContractResolver.htm"><code class="language-plaintext highlighter-rouge">IContractResolver</code></a>
and a new marker attribute <code class="language-plaintext highlighter-rouge">JsonEncryptAttribute</code>. Adding the attribute is quite easy:</p>
<div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">public</span> <span class="k">class</span> <span class="nc">Settings</span> <span class="p">{</span>
<span class="p">[</span><span class="n">JsonEncrypt</span><span class="p">]</span>
<span class="k">public</span> <span class="kt">string</span> <span class="n">Password</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>
</code></pre></div></div>
<p>But you have to remember to add the <code class="language-plaintext highlighter-rouge">ContractResolver</code> additionally:</p>
<div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">JsonConvert</span><span class="p">.</span><span class="nf">SerializeObject</span><span class="p">(</span><span class="n">book</span><span class="p">,</span> <span class="n">Formatting</span><span class="p">.</span><span class="n">Indented</span><span class="p">,</span> <span class="k">new</span> <span class="n">JsonSerializerSettings</span> <span class="p">{</span>
<span class="n">ContractResolver</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">EncryptedStringPropertyResolver</span> <span class="p">(</span><span class="s">"#my*S3cr3t"</span><span class="p">)</span>
<span class="p">});</span>
</code></pre></div></div>
<p>Though the solution is clever, I don’t like the custom <code class="language-plaintext highlighter-rouge">IContractResolver</code>. Therefore
I read through Json.NET’s documentation to find an easier way, i.e. by only applying
an attribute to the property to encrypt.</p>
<p>In fact, Json.NET supports custom converters by annotating properties with
<a href="http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConverterAttribute.htm"><code class="language-plaintext highlighter-rouge">JsonConverterAttribute</code></a>.
That attribute even allows you to pass additional parameters to your custom converter,
like in this case the encryption key.</p>
<p>Therefore I took Brian’s code and converted it into a <code class="language-plaintext highlighter-rouge">JsonConverter</code> (also published as a
<a href="https://gist.github.com/thoemmi/e118c15e7588750b1cc18dab00be31fd#file-encryptingjsonconverter-cs">Gist</a>):</p>
<script src="https://gist.github.com/e118c15e7588750b1cc18dab00be31fd.js?file=EncryptingJsonConverter.cs"> </script>
<p>And the usage is pretty simple:</p>
<div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">public</span> <span class="k">class</span> <span class="nc">Settings</span> <span class="p">{</span>
<span class="p">[</span><span class="nf">JsonConverter</span><span class="p">(</span><span class="k">typeof</span><span class="p">(</span><span class="n">EncryptingJsonConverter</span><span class="p">),</span> <span class="s">"#my*S3cr3t"</span><span class="p">)]</span>
<span class="k">public</span> <span class="kt">string</span> <span class="n">Password</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>
</code></pre></div></div>
<p>There’s no need for any additional code like a custom <code class="language-plaintext highlighter-rouge">ContractResolver</code>.
And you can even use different encryption keys for different properties.</p>
<p>My code works only for <code class="language-plaintext highlighter-rouge">string</code> properties though, but that’s all I needed.</p></content><author><name>Thomas Freudenberg</name></author><category term="JSON.NET" /><summary type="html">In a small inhouse app I wrote recently I store the settings in a json file, using the popular Json.NET library. However, the settings include a password, which should’t appear as clear text in the json file.</summary></entry><entry><title type="html">Presenting Byte Size Values in WPF</title><link href="https://thomasfreudenberg.com/archive/2017/01/21/presenting-byte-size-values-in-wpf/" rel="alternate" type="text/html" title="Presenting Byte Size Values in WPF" /><published>2017-01-21T00:00:00+00:00</published><updated>2017-01-21T00:00:00+00:00</updated><id>https://thomasfreudenberg.com/archive/2017/01/21/presenting-byte-size-values-in-wpf</id><content type="html" xml:base="https://thomasfreudenberg.com/archive/2017/01/21/presenting-byte-size-values-in-wpf/"><p>Though it’s not my main job, I still enjoy writing WPF application. Small tools, making
my colleagues’ and my own life easier.</p>
<p>Recently I had the requirement to display size values in bytes, kilobytes, etc in a well-rounded way.
You will find many examples for formatting such values in the internet. Most look like this:</p>
<div class="language-c# highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">string</span> <span class="n">result</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">number</span> <span class="p">&gt;=</span> <span class="m">1024</span> <span class="p">*</span> <span class="m">1024</span> <span class="p">*</span> <span class="m">1024</span><span class="p">)</span> <span class="p">{</span>
<span class="n">result</span> <span class="p">=</span> <span class="p">(</span><span class="n">number</span> <span class="p">/</span> <span class="m">1024.0</span> <span class="p">/</span> <span class="m">1024</span> <span class="p">/</span> <span class="m">1024</span><span class="p">).</span><span class="nf">ToString</span><span class="p">(</span><span class="s">"F1"</span><span class="p">)</span> <span class="p">+</span> <span class="s">" GB"</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">number</span> <span class="p">&gt;=</span> <span class="m">1024</span> <span class="p">*</span> <span class="m">1024</span><span class="p">)</span> <span class="p">{</span>
<span class="n">result</span> <span class="p">=</span> <span class="p">(</span><span class="n">number</span> <span class="p">/</span> <span class="m">1024.0</span> <span class="p">/</span> <span class="m">1024</span><span class="p">).</span><span class="nf">ToString</span><span class="p">(</span><span class="s">"F1"</span><span class="p">)</span> <span class="p">+</span> <span class="s">" MB"</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">number</span> <span class="p">&gt;=</span> <span class="m">1024</span><span class="p">)</span> <span class="p">{</span>
<span class="n">result</span> <span class="p">=</span> <span class="p">(</span><span class="n">number</span> <span class="p">/</span> <span class="m">1024.0</span><span class="p">).</span><span class="nf">ToString</span><span class="p">(</span><span class="s">"F1"</span><span class="p">)</span> <span class="p">+</span> <span class="s">" KB"</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="n">result</span> <span class="p">=</span> <span class="n">number</span> <span class="p">+</span> <span class="s">" Bytes"</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>
<p>or, in a smarter way</p>
<div class="language-c# highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">string</span><span class="p">[]</span> <span class="n">sizes</span> <span class="p">=</span> <span class="p">{</span> <span class="s">"B"</span><span class="p">,</span> <span class="s">"KB"</span><span class="p">,</span> <span class="s">"MB"</span><span class="p">,</span> <span class="s">"GB"</span><span class="p">,</span> <span class="s">"TB"</span> <span class="p">};</span>
<span class="kt">double</span> <span class="n">len</span> <span class="p">=</span> <span class="n">number</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">order</span> <span class="p">=</span> <span class="m">0</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="n">len</span> <span class="p">&gt;=</span> <span class="m">1024</span> <span class="p">&amp;&amp;</span> <span class="n">order</span> <span class="p">&lt;</span> <span class="n">sizes</span><span class="p">.</span><span class="n">Length</span> <span class="p">-</span> <span class="m">1</span><span class="p">)</span> <span class="p">{</span>
<span class="n">order</span><span class="p">++;</span>
<span class="n">len</span> <span class="p">=</span> <span class="n">len</span> <span class="p">/</span> <span class="m">1024</span><span class="p">;</span>
<span class="p">}</span>
<span class="kt">string</span> <span class="n">result</span> <span class="p">=</span> <span class="s">$"</span><span class="p">{</span><span class="n">len</span><span class="p">:</span><span class="m">0.</span><span class="err">##</span><span class="p">}</span><span class="s"> </span><span class="p">{</span><span class="n">sizes</span><span class="p">[</span><span class="n">order</span><span class="p">]}</span><span class="s">"</span><span class="p">;</span>
</code></pre></div></div>
<p>However, if you’re on the Windows platform, there’s a much easier option:
<strong><a href="https://msdn.microsoft.com/en-us/library/windows/desktop/bb759975(v=vs.85).aspx"><code class="language-plaintext highlighter-rouge">StrFormatByteSize</code></a></strong>.
That’s the same method that Explorer is using to display file sizes. Its advantages are that
you don’t have any localization issues, and it it has a fixed precision of 3 digits.</p>
<p>Because my application is using WPF, I wrote an <code class="language-plaintext highlighter-rouge">IValueConverter</code> to be used in bindings:</p>
<script src="https://gist.github.com/03b81c07586f63accb83521d783d6749.js?file=FormatKbSizeConverter.cs"> </script>
<p>Formatting binded values in XAML becomes quite easy with that converter
(see <a href="https://gist.github.com/thoemmi/03b81c07586f63accb83521d783d6749#file-mainwindow-xaml">full XAML</a>):</p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;DataGrid</span>
<span class="na">AutoGenerateColumns=</span><span class="s">"False"</span>
<span class="na">IsReadOnly=</span><span class="s">"True"</span>
<span class="na">ItemsSource=</span><span class="s">"{StaticResource numbers}"</span><span class="nt">&gt;</span>
<span class="nt">&lt;DataGrid.Resources&gt;</span>
<span class="c">&lt;!-- the actual converter --&gt;</span>
<span class="nt">&lt;local:FormatKbSizeConverter</span> <span class="na">x:Key=</span><span class="s">"FormatKbSizeConverter"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/DataGrid.Resources&gt;</span>
<span class="nt">&lt;DataGrid.Columns&gt;</span>
<span class="c">&lt;!-- First column shows the plain values --&gt;</span>
<span class="nt">&lt;DataGridTextColumn</span>
<span class="na">Binding=</span><span class="s">"{Binding}"</span>
<span class="na">ElementStyle=</span><span class="s">"{StaticResource RightCell}"</span>
<span class="na">Header=</span><span class="s">"Plain"</span> <span class="nt">/&gt;</span>
<span class="c">&lt;!-- Second column shows the formatted values --&gt;</span>
<span class="nt">&lt;DataGridTextColumn</span>
<span class="na">Binding=</span><span class="s">"{Binding Converter={StaticResource FormatKbSizeConverter}}"</span>
<span class="na">ElementStyle=</span><span class="s">"{StaticResource RightCell}"</span>
<span class="na">Header=</span><span class="s">"Formatted"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/DataGrid.Columns&gt;</span>
<span class="nt">&lt;/DataGrid&gt;</span>
</code></pre></div></div>
<p><img src="/files/archive/FormatKbSizeConverter.png" alt="FormatKbSizeConverter" class="align-center" /></p></content><author><name>Thomas Freudenberg</name></author><category term="WPF" /><summary type="html">Though it’s not my main job, I still enjoy writing WPF application. Small tools, making my colleagues’ and my own life easier.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://thomasfreudenberg.com/files/archive/FormatKbSizeConverter.png" /><media:content medium="image" url="https://thomasfreudenberg.com/files/archive/FormatKbSizeConverter.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Pretzel Plugin: Redirect</title><link href="https://thomasfreudenberg.com/archive/2017/01/08/pretzel-redirect-plugin/" rel="alternate" type="text/html" title="Pretzel Plugin: Redirect" /><published>2017-01-08T00:00:00+00:00</published><updated>2017-01-08T00:00:00+00:00</updated><id>https://thomasfreudenberg.com/archive/2017/01/08/pretzel-redirect-plugin</id><content type="html" xml:base="https://thomasfreudenberg.com/archive/2017/01/08/pretzel-redirect-plugin/"><p>A while back I wrote about the migration of my blog from Community Server over Jekyll to Pretzel.</p>
<p>One golden rule when restructuring a web site is to avoid dead links. The original web engine was
a classic ASP.NET application, where every URL ends in <code class="language-plaintext highlighter-rouge">.aspx</code> (at least if you don’t configure
extensionless URLs). Tens years ago every URLs ended in an extension like <code class="language-plaintext highlighter-rouge">.html</code>, <code class="language-plaintext highlighter-rouge">.php</code>, whatever.
Now-a-days, you barely see any file extensions anymore. The web servers don’t expose the underlying
technology, and the user just doesn’t care. And so does Pretzel.</p>
<p>To use a new URL schema, but preserve the old links and redirect them to the new schema, I wrote
an plugin for Pretzel, <a href="https://github.com/thoemmi/Pretzel.RedirectFrom">Pretzel.RedirectFrom</a>.
I’ve used the <a href="https://github.com/jekyll/jekyll-redirect-from">jekyll-redirect-from</a> plugin as
a guide, so the syntax is the same. Just add alternative URLs in the page’s YAML front-matter:</p>
<div class="language-yaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nn">---</span>
<span class="na">title</span><span class="pi">:</span> <span class="s2">"</span><span class="s">My</span><span class="nv"> </span><span class="s">First</span><span class="nv"> </span><span class="s">Post"</span>
<span class="na">redirect_from</span><span class="pi">:</span>
<span class="pi">-</span> <span class="s">/pages/page1</span>
<span class="nn">---</span>
</code></pre></div></div>
<p>This will generate a small html page at <code class="language-plaintext highlighter-rouge">\pages\page1\index.html</code>, which will redirect to the new
location:</p>
<div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">&lt;!DOCTYPE html&gt;</span>
<span class="nt">&lt;meta</span> <span class="na">charset=</span><span class="s">"utf-8"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;title&gt;</span>Redirecting...<span class="nt">&lt;/title&gt;</span>
<span class="nt">&lt;link</span> <span class="na">rel=</span><span class="s">"canonical"</span> <span class="na">href=</span><span class="s">"/2016/10/31/myfirstpost.html"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;meta</span> <span class="na">http-equiv=</span><span class="s">"refresh"</span> <span class="na">content=</span><span class="s">"0; url=/2016/10/31/myfirstpost.html"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;h1&gt;</span>Redirecting...<span class="nt">&lt;/h1&gt;</span>
<span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">"/2016/10/31/myfirstpost.html"</span><span class="nt">&gt;</span>Click here if you are not redirected.<span class="nt">&lt;/a&gt;</span>
<span class="nt">&lt;script&gt;</span>
<span class="nx">location</span><span class="o">=</span><span class="dl">"</span><span class="s2">/2016/10/31/myfirstpost.html</span><span class="dl">"</span>
<span class="nt">&lt;/script&gt;</span>
</code></pre></div></div>
<p>Pretzel is a static file generator, so the redirection must be performed at the client-side.</p>
<p>However, I prefer a “real” redirect, i.e. a response with the correct HTTP status <code class="language-plaintext highlighter-rouge">301 Moved Permanently</code>.
Therefore I’ve implemented an additional switch. If you’re using IIS (or Azure, or any other web server
supporting ASP.NET), you can specify the switch <code class="language-plaintext highlighter-rouge">redirect_generate_aspx: true</code> in Pretzel’s
<code class="language-plaintext highlighter-rouge">_config.yml</code>. In this case the generated page will look like this:</p>
<div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;</span><span class="err">%@</span> <span class="na">Page</span> <span class="na">Language=</span><span class="s">"C#"</span><span class="err">%</span><span class="nt">&gt;</span>
<span class="nt">&lt;script </span><span class="na">runat=</span><span class="s">"server"</span><span class="nt">&gt;</span>
<span class="kr">private</span> <span class="k">void</span> <span class="nx">Page_Load</span><span class="p">(</span><span class="nx">object</span> <span class="nx">sender</span><span class="p">,</span> <span class="nx">System</span><span class="p">.</span><span class="nx">EventArgs</span> <span class="nx">e</span><span class="p">)</span>
<span class="p">{</span>
<span class="nx">Response</span><span class="p">.</span><span class="nx">StatusCode</span> <span class="o">=</span> <span class="mi">301</span><span class="p">;</span>
<span class="nx">Response</span><span class="p">.</span><span class="nx">Status</span> <span class="o">=</span> <span class="dl">"</span><span class="s2">301 Moved Permanently</span><span class="dl">"</span><span class="p">;</span>
<span class="nx">Response</span><span class="p">.</span><span class="nx">AddHeader</span><span class="p">(</span><span class="dl">"</span><span class="s2">Location</span><span class="dl">"</span><span class="p">,</span><span class="dl">"</span><span class="s2">/2016/10/31/myfirstpost.html</span><span class="dl">"</span><span class="p">);</span>
<span class="nx">Response</span><span class="p">.</span><span class="nx">End</span><span class="p">();</span>
<span class="p">}</span>
<span class="nt">&lt;/script&gt;</span>
</code></pre></div></div>
<p>This ensures that the server returns the correct HTTP status.</p>
<p>Anyway, this plugin is a <a href="https://github.com/Code52/pretzel/wiki/create-plugins">simple ScriptCs file</a>,
so you only have to copy
<a href="https://github.com/thoemmi/Pretzel.RedirectFrom/blob/master/Pretzel.RedirectFrom.csx">Pretzel.RedirectFrom.csx</a>
to the <code class="language-plaintext highlighter-rouge">_plugin</code> folder of your Pretzel site, and you will be ready to use redirects.</p></content><author><name>Thomas Freudenberg</name></author><category term="Pretzel" /><summary type="html">A while back I wrote about the migration of my blog from Community Server over Jekyll to Pretzel.</summary></entry><entry><title type="html">Pretzel Plugin: Sitemap</title><link href="https://thomasfreudenberg.com/archive/2016/12/29/pretzel-sitemap/" rel="alternate" type="text/html" title="Pretzel Plugin: Sitemap" /><published>2016-12-29T00:00:00+00:00</published><updated>2016-12-29T00:00:00+00:00</updated><id>https://thomasfreudenberg.com/archive/2016/12/29/pretzel-sitemap</id><content type="html" xml:base="https://thomasfreudenberg.com/archive/2016/12/29/pretzel-sitemap/"><p>A couple of month ago <a href="/archive/2016/05/16/from-jekyll-to-pretzel/">I wrote</a> that
I switched to <strong>Pretzel</strong> to drive my site.</p>
<p>What I really like about Pretzel (except that it’s written in .NET) is that it is
so <a href="https://github.com/Code52/pretzel/wiki/create-plugins">easy to extend</a>. You can
write plugins either as a .NET assembly, or—even simpler—throw in an
<code class="language-plaintext highlighter-rouge">.csx</code> file, because Pretzel supports <a href="http://scriptcs.net/">ScriptCs</a>.</p>
<p>One of the first extensions I wrote was a <strong><a href="https://en.wikipedia.org/wiki/Site_map">site map</a></strong>
plugin. By default Pretzel already creates a site map, but only for static pages.
Unfortunately, this doesn’t include paginated pages like the home page. Those pages
are generated dynamically at runtime of Pretzel, and the default <code class="language-plaintext highlighter-rouge">sitemap.xml</code> doesn’t
take those dynamic pages into account.</p>
<p>Therefore I wrote this plugin which creates the <code class="language-plaintext highlighter-rouge">sitemap.xml</code> including all generated
pages, even the paginated ones. It uses the same technique as
<a href="https://github.com/Code52/pretzel/blob/master/src/Pretzel.Logic/Templating/JekyllEngineBase.cs#L89"><code class="language-plaintext highlighter-rouge">JekyllEngineBase.ProcessFile</code></a>:
for each post and page it adds an <code class="language-plaintext highlighter-rouge">url</code> entry to the sitemap. Additionally it checks
the page’s front-matter if <code class="language-plaintext highlighter-rouge">paginate</code> is specified, and adds relevant URLs to the sitemap
too.</p>
<p>The plugin is <a href="https://github.com/thoemmi/Pretzel.Sitemap">hosted on Github</a> including
some basic tests, but in fact you only have to copy
<a href="https://github.com/thoemmi/Pretzel.Sitemap/blob/master/Pretzel.Sitemap.csx"><code class="language-plaintext highlighter-rouge">Pretzel.Sitemap.csx</code></a>
to the <code class="language-plaintext highlighter-rouge">_plugin</code> folder of your Pretzel site.</p></content><author><name>Thomas Freudenberg</name></author><category term="Pretzel" /><summary type="html">A couple of month ago I wrote that I switched to Pretzel to drive my site.</summary></entry><entry><title type="html">Naming Events in .NET</title><link href="https://thomasfreudenberg.com/archive/2016/07/01/naming-events-in-dotnet/" rel="alternate" type="text/html" title="Naming Events in .NET" /><published>2016-07-01T00:00:00+00:00</published><updated>2016-12-29T00:00:00+00:00</updated><id>https://thomasfreudenberg.com/archive/2016/07/01/naming-events-in-dotnet</id><content type="html" xml:base="https://thomasfreudenberg.com/archive/2016/07/01/naming-events-in-dotnet/"><p><strong>Update 2016/12/29</strong>: I’ve complained about the <code class="language-plaintext highlighter-rouge">On</code> prefix in the <a href="https://docs.microsoft.com/en-us/dotnet/articles/csharp/events-overview#comments-container">comment section</a>
of the documentation (and <a href="http://blog.gauffin.org/">Jonas Gauffin</a> too). In fact the prefix was a mistake and
is <a href="https://github.com/dotnet/docs/pull/1012">fixed</a> by now.</p>
<hr />
<p>Things are changing in the .NET world. A couple of days ago Microsoft released
<a href="https://blogs.msdn.microsoft.com/dotnet/2016/06/27/announcing-net-core-1-0/">.NET Core 1.0</a>,
the new cross-platform, open source, and modular .NET platform.</p>
<p>Unfortunately, not only the managed framework’s changing, but naming guidelines
too.</p>
<p>Lets start with the old .NET framework. Microsoft says in the
<a href="https://msdn.microsoft.com/en-us/library/ms229012.aspx#Anchor_2">Naming Guideline for Events</a>:</p>
<blockquote>
<p><strong>✓ DO</strong> name events with a verb or a verb phrase.</p>
<p>Examples include <code class="language-plaintext highlighter-rouge">Clicked</code>, <code class="language-plaintext highlighter-rouge">Painting</code>, <code class="language-plaintext highlighter-rouge">DroppedDown</code>, and so on.</p>
<p><strong>✓ DO</strong> give events names with a concept of before and after, using the present
and past tenses.</p>
<p>For example, a close event that is raised before a window is closed would be
called <code class="language-plaintext highlighter-rouge">Closing</code>, and one that is raised after the window is closed would be
called <code class="language-plaintext highlighter-rouge">Closed</code>.</p>
</blockquote>
<p>The (slightly outdated) <a href="https://msdn.microsoft.com/en-us/library/h0eyck3s(VS.71).aspx">Event Naming Guidelines for .NET Framework 1.1</a>
even says more explicitly:</p>
<blockquote>
<ul>
<li>Do not use a prefix or suffix on the event declaration on the type. For
example, use <code class="language-plaintext highlighter-rouge">Close</code> instead of <code class="language-plaintext highlighter-rouge">OnClose</code>.</li>
</ul>
</blockquote>
<p>That’s what we’ve been taught for the last 15 years: Events are named without
a prefix.</p>
<p>Let’s repeat: Events are named without a prefix.</p>
<h2 id="entry-net-core">Entry .NET Core!</h2>
<p><a href="https://github.com/MrRoundRobin">Robin Müller</a>, maintainer of the <a href="https://github.com/MrRoundRobin/telegram.bot">telegram.bot</a>
library, changed the names of all event from an unprefixed name to prefixed with <code class="language-plaintext highlighter-rouge">On</code>
in a <a href="https://github.com/MrRoundRobin/telegram.bot/commit/54860e7048c2a0b76a206739d1bc1a2795e31199">recent commit</a>.
I complained that this renaming would contradict the guidelines recommendations by Microsoft
and what we’ve learnt the last 15 years.</p>
<p>However, Robin pointed me to the <a href="https://docs.microsoft.com/en-us/dotnet/articles/csharp/events-overview#language-support-for-events">new guidelines</a>
(emphasis mine):</p>
<blockquote>
<p>There are a number of conventions that you should follow when declaring an event.
Typically, the event delegate type has a void return. <strong>Prefix event declarations
with ‘On’.</strong> The remainder of the name is a verb.</p>
</blockquote>
<p>WTF? Do we have to forget the old habits and rename all events when moving from good ol’
.NET Framework to .NET Core?</p>
<p>The new guidelines include a comment section at the bottom where I asked for the rational
behind the changed guideline a few days ago, but I still got no answer. I’d really like to
now…</p>
<p><strong>Sidenote:</strong> Most of us are used to name the <strong>handler</strong> for an event
<code class="language-plaintext highlighter-rouge">On&lt;name of the event&gt;</code>, e.g.</p>
<div class="language-c# highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">foo</span><span class="p">.</span><span class="n">SomethingHappened</span> <span class="p">+=</span> <span class="n">OnSomethingHappened</span><span class="p">;</span>
</code></pre></div></div>
<p>What should we call those event handlers now? <code class="language-plaintext highlighter-rouge">OnOnSomethingHappened</code>?</p></content><author><name>Thomas Freudenberg</name></author><category term=".NET" /><summary type="html">Update 2016/12/29: I’ve complained about the On prefix in the comment section of the documentation (and Jonas Gauffin too). In fact the prefix was a mistake and is fixed by now.</summary></entry></feed>
If you would like to create a banner that links to this page (i.e. this validation result), do the following:
Download the "valid Atom 1.0" banner.
Upload the image to your own server. (This step is important. Please do not link directly to the image on this server.)
Add this HTML to your page (change the image src
attribute if necessary):
If you would like to create a text link instead, here is the URL you can use:
http://www.feedvalidator.org/check.cgi?url=http%3A//thomasfreudenberg.com/feed.xml