<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
      <title>Kristof&#x27;s blog</title>
      <link>https://mattei.io</link>
      <description>Kristof is a software engineer living in Phoenix, AZ. He enjoys learning new computer languages, solving computer puzzles, tinkering with his home network, hiking and doing long road trips.</description>
      <generator>Zola</generator>
      <language>en</language>
      <atom:link href="https://mattei.io/feed.xml" rel="self" type="application/rss+xml"/>
      <lastBuildDate>Wed, 21 Jan 2026 15:49:01 -0700</lastBuildDate>
      <item>
          <title>Tokio: receive variable-sized data from a UdpSocket</title>
          <pubDate>Wed, 21 Jan 2026 15:49:01 -0700</pubDate>
          <author>Kristof</author>
          <link>https://mattei.io/tokio-receive-variable-sized-data-from-a-udpsocket/</link>
          <guid>https://mattei.io/tokio-receive-variable-sized-data-from-a-udpsocket/</guid>
          <description xml:base="https://mattei.io/tokio-receive-variable-sized-data-from-a-udpsocket/">&lt;p&gt;Imagine you&#x27;re listening on a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;latest&#x2F;tokio&#x2F;net&#x2F;struct.UdpSocket.html&quot;&gt;&lt;code&gt;tokio::net::UdpSocket&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; and you know you&#x27;re going to receive a packet, but don&#x27;t know the size.&lt;&#x2F;p&gt;
&lt;p&gt;The first option was to find out what the other party might send, but that information might not always be available.&lt;&#x2F;p&gt;
&lt;p&gt;Another way might be to dynamically allocate a buffer based on the incoming packet size:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;use&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; tokio&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;::&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;net&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;::&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;UdpSocket&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;async fn&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; get_buffer_size&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;(socket&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;UdpSocket&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;usize&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;, std&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;::&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;io&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;::&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Error&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; result&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; socket&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        .&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;async_io&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Interest&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;::&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;READABLE&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; ||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-storage&quot;&gt;            let mut&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; mini_buf&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;0_&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;u8&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 32&lt;&#x2F;span&gt;&lt;span&gt;];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;            &#x2F;&#x2F; SAFETY: libc call&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;            let&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; result&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; = unsafe&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;                libc&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;::&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;recv&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;                    socket&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;as_raw_fd&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;                    mini_buf&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;as_mut_ptr&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;cast&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;                    mini_buf&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;                    libc&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;::&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;MSG_PEEK&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; |&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; libc&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;::&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;MSG_TRUNC&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;            if&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; result&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;                return&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Err&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;std&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;::&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;io&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;::&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Error&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;::&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;last_os_error&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;            Ok&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;(result&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;unsigned_abs&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        })&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        .await?&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Ok&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;(result)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Couple of things to note:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;We&#x27;re using &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.man7.org&#x2F;linux&#x2F;man-pages&#x2F;man2&#x2F;recv.2.html#:~:text=with%20such%20protocols.-,MSG_PEEK,-This%20flag%20causes&quot;&gt;&lt;code&gt;MSG_PEEK&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; to ensure we don&#x27;t pull the data from the buffer, we just look at it.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.man7.org&#x2F;linux&#x2F;man-pages&#x2F;man2&#x2F;recv.2.html#:~:text=the%20same%20data.-,MSG_TRUNC,-%28since%20Linux%202.2&quot;&gt;&lt;code&gt;MSG_TRUNC&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; to ensure we get back the total number of bytes pending in the kernel&#x27;s buffer, not just the amount written to OUR buffer&lt;&#x2F;li&gt;
&lt;li&gt;We stack-allocated the buffer to avoid the allocation hit every time.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</description>
      </item>
      <item>
          <title>Ubuntu 24.04: disable systemd-resolved&#x27;s stub listener</title>
          <pubDate>Sun, 19 May 2024 13:43:01 -0700</pubDate>
          <author>Kristof</author>
          <link>https://mattei.io/ubuntu-24-04-disable-systemd-resolved-s-stub-listener/</link>
          <guid>https://mattei.io/ubuntu-24-04-disable-systemd-resolved-s-stub-listener/</guid>
          <description xml:base="https://mattei.io/ubuntu-24-04-disable-systemd-resolved-s-stub-listener/">&lt;p&gt;I&#x27;m rebuilding my server and I need to make sure the server itself does not listen to port 53 as we&#x27;ll be hosting a Docker container with AdGuard on port 53.&lt;&#x2F;p&gt;
&lt;p&gt;To do that we need to disable systemd-resolved&#x27;s stub listener.&lt;&#x2F;p&gt;
&lt;p&gt;The trick is to set &lt;code&gt;DNSStubListener=no&lt;&#x2F;code&gt; in &lt;code&gt;&#x2F;etc&#x2F;systemd&#x2F;resolved.conf&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Resolve]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#DNSStubListener=yes&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, this will cause APT to ask you to resolve configuration discrepancies should systemd-resolved be updated (e.g. when updating Ubuntu to vNext).&lt;&#x2F;p&gt;
&lt;p&gt;So we take a step back, read the comments, and ...:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# Entries in this file show the compile time defaults. Local configuration&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# should be created by either modifying this file (or a copy of it placed in&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# &#x2F;etc&#x2F; if the original file is shipped in &#x2F;usr&#x2F;), or by creating &amp;quot;drop-ins&amp;quot; in&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# the &#x2F;etc&#x2F;systemd&#x2F;resolved.conf.d&#x2F; directory.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Cool, we create the directory:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# Create directory&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;sudo mkdir -p &#x2F;etc&#x2F;systemd&#x2F;resolved.conf.d&#x2F;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# Set permissions&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;sudo chmod 755 &#x2F;etc&#x2F;systemd&#x2F;resolved.conf.d&#x2F;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# Disable&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;printf &amp;quot;[Resolve]\nDNSStubListener=no\n&amp;quot; | sudo tee &#x2F;etc&#x2F;systemd&#x2F;resolved.conf.d&#x2F;noresolved.conf&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# Restart systemd-resolved&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;sudo systemctl restart systemd-resolved.service&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# Test&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cat &#x2F;etc&#x2F;resolv.conf&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And notice that it no longer contains something like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;nameserver 127.0.0.53&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;options edns0 trust-ad&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;search .&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But something like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;nameserver 192.168.1.1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;search home.domain.com&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Until next time!&lt;&#x2F;p&gt;
&lt;p&gt;-K&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>You cannot disable rDNS (IPv6) nameservers with netplan</title>
          <pubDate>Sat, 11 Feb 2023 16:23:01 -0800</pubDate>
          <author>Kristof</author>
          <link>https://mattei.io/you-cannot-disable-rdns-ipv6-nameservers-with-netplan/</link>
          <guid>https://mattei.io/you-cannot-disable-rdns-ipv6-nameservers-with-netplan/</guid>
          <description xml:base="https://mattei.io/you-cannot-disable-rdns-ipv6-nameservers-with-netplan/">&lt;p&gt;I am playing with IPv6 and wanted to statically configure &lt;code&gt;&#x2F;etc&#x2F;resolv.conf&lt;&#x2F;code&gt;&#x27;s &lt;code&gt;nameserver&lt;&#x2F;code&gt;s. I wanted to avoid getting any from the Router, even though I wanted an IPv6 address through SLAAC.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;first-try&quot;&gt;First try&lt;&#x2F;h3&gt;
&lt;p&gt;Since the server runs Ubuntu, netplan is the default network configuration tool:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;network:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  ethernets:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    eno1:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      addresses:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        # static IPv4 address&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        - 10.0.10.10&#x2F;24&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      routes:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        # static IPv4 gateway&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        - to: default&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          via: 10.0.10.1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          metric: 100&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          on-link: true&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        # IPv6 gateway comes via RA&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      nameservers:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        addresses:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          - 10.0.10.20&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        # always use FQDN as short names clash with SSL&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        search: [&amp;quot;&amp;quot;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      # disabled by default&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      # dhcp4: false&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      # dhcp4-overrides:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        # use-dns: false&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        # use-domains: false&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      # IPv6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      # We want IPv6 to be configured by RA only&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      accept-ra: true&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      # dhcp6: false&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      # dhcp6-overrides:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        # use-dns: false&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        # use-domains: false&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      ipv6-privacy: false&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  renderer: networkd&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  version: 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;One &lt;code&gt;sudo netplan apply&lt;&#x2F;code&gt; later and we have&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;a static IPv4 address&lt;&#x2F;li&gt;
&lt;li&gt;a static DNS server&lt;&#x2F;li&gt;
&lt;li&gt;a SLAAC IPv6 address&lt;&#x2F;li&gt;
&lt;li&gt;ping to an IPv4 address work&lt;&#x2F;li&gt;
&lt;li&gt;ping6 to an IPv6 address work&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;However, we have one issue. We&#x27;re getting a IPv6 DNS address pushed...:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt; cat &#x2F;etc&#x2F;resolv.conf&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;nameserver 10.0.10.20&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;nameserver 2600:xxxx:xxxx:xxxx::1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;search .&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Parsing netplan&#x27;s reference yielded some reference to stateless configuration &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;netplan.io&#x2F;reference#:~:text=Off%20by%20default.-,dhcp6,-(bool)&quot;&gt;here&lt;&#x2F;a&gt;. Time to try it out.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;second-try&quot;&gt;Second try&lt;&#x2F;h3&gt;
&lt;p&gt;Let&#x27;s switch it on.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;network:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  ethernets:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    eno1:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      # ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      # IPv6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      # We want IPv6 to be configured by RA only&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      accept-ra: true&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      dhcp6: true&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      dhcp6-overrides:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        use-dns: false&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        use-domains: false&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      ipv6-privacy: false&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  # ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Running &lt;code&gt;&amp;gt; sudo netplan apply &amp;amp;&amp;amp; sudo systemctl restart systemd-resolved.service&lt;&#x2F;code&gt; and ...&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt; cat &#x2F;etc&#x2F;resolv.conf&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;nameserver 10.0.10.20&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;nameserver 2600:xxxx:xxxx:xxxx::1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;search .&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Still there... What&#x27;s going on?&lt;&#x2F;p&gt;
&lt;p&gt;Digging deeper reveals that netplan is an abstraction tool over systemd.network. Reading through its configuration reference something caught my attention:
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;manpages.ubuntu.com&#x2F;manpages&#x2F;bionic&#x2F;man5&#x2F;systemd.network.5.html#%5Bnetwork%5D%20section%20options:~:text=Defaults%20to%20%22no%22.-,IPv6AcceptRA%3D,-Enable%20or%20disable&quot;&gt;&lt;code&gt;IPv6AcceptRA=&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Notice the reference to an &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;manpages.ubuntu.com&#x2F;manpages&#x2F;bionic&#x2F;man5&#x2F;systemd.network.5.html#%5Bipv6acceptra%5D%20section%20options&quot;&gt;&lt;code&gt;[IPv6AcceptRA]&lt;&#x2F;code&gt; section&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;And this rang a bell... The DNS address that I got pushed comes from my router&#x27;s rDNS.&lt;&#x2F;p&gt;
&lt;p&gt;So how do we configure this with netplan? We cannot. If we look in netplan&#x27;s code for &lt;code&gt;IPv6AcceptRA&lt;&#x2F;code&gt; which only yields the following result:&lt;&#x2F;p&gt;

&lt;figure&gt;
    &lt;img
    src=&quot;&amp;#x2F;you-cannot-disable-rdns-ipv6-nameservers-with-netplan&amp;#x2F;&#x2F;ipv6acceptra-github.png&quot;
    &#x2F;&gt;
    &lt;figcaption&gt;Netplan doesn&amp;#x27;t support rendering the IPv6AcceptRA section.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;

&lt;h3 id=&quot;last-try&quot;&gt;Last try&lt;&#x2F;h3&gt;
&lt;p&gt;Eventually I ended up setting it manually in the networkd configuration:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt; cat &#x2F;run&#x2F;systemd&#x2F;network&#x2F;10-netplan-eno1.network&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Match]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Name=eno1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Network]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;DHCP=ipv6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;LinkLocalAddressing=ipv6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Address=10.0.10.10&#x2F;24&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;IPv6AcceptRA=yes&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;DNS=10.0.10.20&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Route]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Destination=0.0.0.0&#x2F;0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Gateway=10.0.10.1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;GatewayOnLink=true&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Metric=100&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[DHCP]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;RouteMetric=100&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;UseMTU=true&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;UseDNS=false&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;UseDomains=false&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# vvvvvvvv&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[IPv6AcceptRA]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;UseDNS=false&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;UseDomains=false&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# ^^^^^^^^&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;No more &lt;code&gt;sudo netplan apply&lt;&#x2F;code&gt;. This time we do &lt;code&gt;sudo systemctl restart systemd-networkd.service &amp;amp;&amp;amp; sudo systemctl restart systemd-resolved.service&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Checking &lt;code&gt;&#x2F;etc&#x2F;resolv.conf&lt;&#x2F;code&gt; one more time:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;gt; cat &#x2F;etc&#x2F;resolv.conf&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;nameserver 10.0.10.20&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;search .&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Bingo!&lt;&#x2F;p&gt;
&lt;p&gt;After some more searching I did stumble upon &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;bugs.launchpad.net&#x2F;netplan&#x2F;+bug&#x2F;1982010&quot;&gt;an existing bug report&lt;&#x2F;a&gt; on launchpad. At least I&#x27;m not the only one.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;wrapping-up&quot;&gt;Wrapping up&lt;&#x2F;h3&gt;
&lt;p&gt;I removed the config for netplan. That way it cannot overwrite the network configuration that I edited.&lt;&#x2F;p&gt;
&lt;p&gt;-Kristof&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>How to use Wireguard without Masquarade</title>
          <pubDate>Sat, 08 Jan 2022 11:27:01 -0800</pubDate>
          <author>Kristof</author>
          <link>https://mattei.io/how-to-use-wireguard-without-masquarade/</link>
          <guid>https://mattei.io/how-to-use-wireguard-without-masquarade/</guid>
          <description xml:base="https://mattei.io/how-to-use-wireguard-without-masquarade/">&lt;p&gt;For a while now I&#x27;ve been running &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.linuxserver.io&#x2F;images&#x2F;docker-wireguard&quot;&gt;linuxserver.io&#x27;s Docker container of Wireguard&lt;&#x2F;a&gt; to be able to get access to my home network when I&#x27;m on the go.&lt;&#x2F;p&gt;
&lt;p&gt;Initially, we had a network for the computers at home, &lt;code&gt;192.168.25.0&#x2F;24&lt;&#x2F;code&gt;. (Of course IOT sits in another network).&lt;&#x2F;p&gt;
&lt;p&gt;The Wireguard VPN device sat on &lt;code&gt;192.168.25.20&lt;&#x2F;code&gt;. And since it uses NAT, and that doesn&#x27;t allow me to track individual peers at the network level. All traffic coming over the Wireguard VPN would look like it originates from THAT IP.&lt;&#x2F;p&gt;
&lt;p&gt;Time to split that up.&lt;&#x2F;p&gt;
&lt;p&gt;So what are the steps that we need to do?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;define-the-vpn-network-and-set-up-the-route&quot;&gt;Define the VPN network and set up the route&lt;&#x2F;h3&gt;
&lt;p&gt;Note: I contemplated keeping my Wireguard clients in the same subnet, but that would mean that EVERY device in that subnet would need to get static routes to the Wireguard clients. Whereas if I move them to a separate subnet, only the router needs to get the static route.&lt;&#x2F;p&gt;
&lt;p&gt;I chose to use &lt;code&gt;192.168.30.0&#x2F;24&lt;&#x2F;code&gt; for all my Wireguard clients.&lt;&#x2F;p&gt;
&lt;p&gt;Our Wireguard server sits at &lt;code&gt;192.168.25.20&lt;&#x2F;code&gt;, so on the router we add the static route:&lt;&#x2F;p&gt;

&lt;figure&gt;
    &lt;img
    src=&quot;&amp;#x2F;how-to-use-wireguard-without-masquarade&amp;#x2F;&#x2F;static-route-screenshot.png&quot;
    &#x2F;&gt;
    &lt;figcaption&gt;Static route screenshot.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;

&lt;h3 id=&quot;update-wireguard-s-config-to-not-use-masquerade&quot;&gt;Update Wireguard&#x27;s config to not use Masquerade&lt;&#x2F;h3&gt;
&lt;p&gt;The default &lt;code&gt;wg0.conf&lt;&#x2F;code&gt; needs to be updated to look like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;ini&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;[Interface]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;Address&lt;&#x2F;span&gt;&lt;span&gt; = 192.168.30.1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;ListenPort&lt;&#x2F;span&gt;&lt;span&gt; = 51820&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;PrivateKey&lt;&#x2F;span&gt;&lt;span&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt; ##############################################&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;[Peer]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# peer_iPhoneKristof&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;PublicKey&lt;&#x2F;span&gt;&lt;span&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt; ##############################################&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;AllowedIPs&lt;&#x2F;span&gt;&lt;span&gt; = 192.168.30.10&#x2F;32&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So what did we change?&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;We removed &lt;code&gt;PostUp&lt;&#x2F;code&gt; and &lt;code&gt;PostDown&lt;&#x2F;code&gt;, as these are only needed when we do NAT (i.e. Masquarade)&lt;&#x2F;li&gt;
&lt;li&gt;We set up the &lt;code&gt;Interface&lt;&#x2F;code&gt;&#x27;s address to be &lt;code&gt;.1&lt;&#x2F;code&gt; in our new range.&lt;&#x2F;li&gt;
&lt;li&gt;We set our client to be &lt;code&gt;.10&lt;&#x2F;code&gt; in our new range.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h3 id=&quot;ensure-the-server-forwards-ipv4-packets-and-enable-proxy-arp&quot;&gt;Ensure the server forwards IPv4 packets AND enable proxy ARP&lt;&#x2F;h3&gt;
&lt;p&gt;Lastly, the underlying server needs 2 changes:&lt;&#x2F;p&gt;
&lt;p&gt;In &lt;code&gt;&#x2F;etc&#x2F;sysctl.conf&lt;&#x2F;code&gt; (or wherever it is on your flavor of Linux):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;ini&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# Enable IPv4 packet forwarding&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;net.ipv4.ip_forward&lt;&#x2F;span&gt;&lt;span&gt;=1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# Enable Proxy ARP (https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Proxy_ARP)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;net.ipv4.conf.all.proxy_arp&lt;&#x2F;span&gt;&lt;span&gt;=1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;update-the-client-s-config&quot;&gt;Update the client&#x27;s config&lt;&#x2F;h3&gt;
&lt;p&gt;We need to make sure that the client now connects with the right IP, and that the client&#x27;s &lt;code&gt;AllowedIPs&lt;&#x2F;code&gt; are set up to target our ORIGINAL range:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;ini&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;[Interface]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;PrivateKey&lt;&#x2F;span&gt;&lt;span&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt; ##############################################&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;ListenPort&lt;&#x2F;span&gt;&lt;span&gt; = 51820&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;Address&lt;&#x2F;span&gt;&lt;span&gt; = 192.168.30.10&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;DNS&lt;&#x2F;span&gt;&lt;span&gt; = 192.168.25.5 &lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# adguard sits here&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;[Peer]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;PublicKey&lt;&#x2F;span&gt;&lt;span&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt; ##############################################&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;AllowedIPs&lt;&#x2F;span&gt;&lt;span&gt; = 192.168.25.0&#x2F;24&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;Endpoint&lt;&#x2F;span&gt;&lt;span&gt; = my.endpoint.com:51820&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h3&gt;
&lt;p&gt;We now can track individual Wireguard clients on our network.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>.NET: Disabling SSL Certificate validation</title>
          <pubDate>Thu, 09 Jun 2016 14:17:39 +0000</pubDate>
          <author>Kristof</author>
          <link>https://mattei.io/net-disabling-ssl-certificate-validation/</link>
          <guid>https://mattei.io/net-disabling-ssl-certificate-validation/</guid>
          <description xml:base="https://mattei.io/net-disabling-ssl-certificate-validation/">&lt;p&gt;Yesterday I wanted to download some content off a website with F#, however unfortunately the certificate of the website was expired.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;fsharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;let&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; result&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    try&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; request&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;            &amp;quot;https:&#x2F;&#x2F;somewebsite&#x2F;with&#x2F;expired&#x2F;ssl&#x2F;certificate&#x2F;data.json?paramx=1&amp;amp;paramy=2&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;            |&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; WebRequest.Create&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; response&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            request.GetResponse &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;        &#x2F;&#x2F; parse data&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; parsed&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;...&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        Ok parsed&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    with&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    |&lt;&#x2F;span&gt;&lt;span&gt; ex &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        Error ex&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we execute this, then result would be of Error with the following exception:&lt;&#x2F;p&gt;

&lt;figure&gt;
    &lt;img
    src=&quot;&amp;#x2F;net-disabling-ssl-certificate-validation&amp;#x2F;&#x2F;remote_certificate_invalid.png&quot;
    &#x2F;&gt;
    &lt;figcaption&gt;SSL certificate validation exception&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;

&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;fsharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ex.Message&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;quot;The underlying connection was closed: Could not establish trust relationship for the SSL&#x2F;TLS secure channel.&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ex.InnerException.Message&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;quot;The remote certificate is invalid according to the validation procedure.&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So how do we fix this?&lt;&#x2F;p&gt;
&lt;p&gt;The solution is to set the following code at startup of the application (or at least before the first call):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ServicePointManager.ServerCertificateValidationCallback &amp;lt;-&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    new RemoteCertificateValidationCallback(fun _ _ _ _ -&amp;gt; true)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Notice that you should not do this, because this does not validate the certificate at ALL!
Also, this is for ALL calls, if you want to do it on a specific call you need to do make some changes.&lt;&#x2F;p&gt;
&lt;p&gt;First of all, it doesn&#x27;t work with &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;bw00b1dc(v=vs.110).aspx&quot;&gt;&lt;code&gt;WebRequest.Create&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, you need to use &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;ff382788(v=vs.110).aspx&quot;&gt;&lt;code&gt;WebRequest.CreateHttp&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, or cast the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;system.net.webrequest(v=vs.110).aspx&quot;&gt;&lt;code&gt;WebRequest&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; to &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;system.net.httpwebrequest(v=vs.110).aspx&quot;&gt;&lt;code&gt;HttpWebRequest&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, as the property we need, &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;system.net.httpwebrequest.servercertificatevalidationcallback(v=vs.110).aspx&quot;&gt;&lt;code&gt;ServerCertificateValidationCallback&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; is not available on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;system.net.webrequest(v=vs.110).aspx&quot;&gt;&lt;code&gt;WebRequest&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, only on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;system.net.httpwebrequest(v=vs.110).aspx&quot;&gt;&lt;code&gt;HttpWebRequest&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. The resulting code looks like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;fsharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;let&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; request&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;    &amp;quot;https:&#x2F;&#x2F;somewebsite&#x2F;with&#x2F;expired&#x2F;ssl&#x2F;certificate&#x2F;data.json?paramx=1&amp;amp;paramy=2&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    |&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; WebRequest.CreateHttp&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;request.ServerCertificateValidationCallback &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;lt;- new&lt;&#x2F;span&gt;&lt;span&gt; RemoteCertificateValidationCallback&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;(fun&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; _ _ _ _ &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; true&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;let&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; response&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    request.GetResponse &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Again, don&#x27;t do this in production!&lt;&#x2F;p&gt;
&lt;p&gt;If need be, do it on a single &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;system.net.httpwebrequest(v=vs.110).aspx&quot;&gt;&lt;code&gt;HttpWebRequest&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, like the last example, and write some code so that you ignore the expiration part, but leave in place the validation part.&lt;&#x2F;p&gt;
&lt;p&gt;Code on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;kristof-mattei&#x2F;dont-validate-ssl&quot;&gt;Github&lt;&#x2F;a&gt;!&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Be careful with npm shrinkwrap</title>
          <pubDate>Tue, 17 Nov 2015 08:32:26 +0000</pubDate>
          <author>Kristof</author>
          <link>https://mattei.io/be-careful-with-npm-shrinkwrap/</link>
          <guid>https://mattei.io/be-careful-with-npm-shrinkwrap/</guid>
          <description xml:base="https://mattei.io/be-careful-with-npm-shrinkwrap/">&lt;p&gt;Recently we had the issue that we used version x of an npm package. However in the course of time this package was updated by the author and contained a critical bug, which broke our AWS deployments. Locally this was no issue because we had a version installed that satisfied the version requirements.&lt;&#x2F;p&gt;
&lt;p&gt;In order to prevent issues like this we looked at &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.npmjs.com&#x2F;cli&#x2F;shrinkwrap&quot;&gt;npm shrinkwrap&lt;&#x2F;a&gt;. This writes a file called &lt;code&gt;npm-shrinkwrap.json&lt;&#x2F;code&gt; which &#x27;freezes&#x27; all of the package-versions that are installed in current project.&lt;&#x2F;p&gt;
&lt;p&gt;Now this is dangerous, as we just found out.&lt;&#x2F;p&gt;
&lt;p&gt;The issue is that when an author decides to delete a package from the feed. Delete you ask? Yes, delete, gone, no trace, nothing.&lt;&#x2F;p&gt;
&lt;p&gt;What&#x27;s the issue you might ask? You&#x27;ll find it next time?&lt;&#x2F;p&gt;
&lt;p&gt;Not really.&lt;&#x2F;p&gt;
&lt;p&gt;Imagine you&#x27;re using Elastic Beanstalk, which, based on certain triggers, can spawn a new instance of a server, or delete one.&lt;&#x2F;p&gt;
&lt;p&gt;Now today you release your application to your servers, and you &lt;code&gt;shrinkwrap&lt;&#x2F;code&gt; your packages.&lt;&#x2F;p&gt;
&lt;p&gt;Before you do that, you obviously clear your local npm cache (located in &lt;code&gt;%appdata%\npm-cache&lt;&#x2F;code&gt;), and your local &lt;code&gt;node_modules&lt;&#x2F;code&gt;. Then you do an &lt;code&gt;npm install&lt;&#x2F;code&gt; to verify every package is correctly installed, you do a few test runs, maybe on a local server. Then you package and send it of to AWS.&lt;&#x2F;p&gt;
&lt;p&gt;All runs well, you&#x27;re happy, and your boss is happy.&lt;&#x2F;p&gt;
&lt;p&gt;Next week, for whatever reason, you get a high load on your servers. Elastic Beanstalk decides to add one more instance.&lt;&#x2F;p&gt;
&lt;p&gt;And then stuff starts to break. You get emails that its health is degraded. Then you get emails that its health severe.&lt;&#x2F;p&gt;
&lt;p&gt;At 2a.m. you open your laptop, and you start looking at the logs. There you find something in the lines of:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;npm ERR! Linux 3.14.48-33.39.amzn1.x86_64&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;npm ERR! argv &amp;quot;&#x2F;usr&#x2F;bin&#x2F;iojs&amp;quot; &amp;quot;&#x2F;usr&#x2F;bin&#x2F;npm&amp;quot; &amp;quot;install&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;npm ERR! node v2.4.0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;npm ERR! npm  v2.13.0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;npm ERR! version not found: node-uuid@1.4.4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;npm ERR!&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;npm ERR! If you need help, you may report this error at:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;npm ERR!     &amp;lt;https:&#x2F;&#x2F;github.com&#x2F;npm&#x2F;npm&#x2F;issues&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;npm ERR! Please include the following file with any support request:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;npm ERR!     &#x2F;app&#x2F;npm-debug.log&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What? You tested locally? What happened?&lt;&#x2F;p&gt;
&lt;p&gt;Okay, you fire up a console window. You make a test dir. You run &lt;code&gt;npm installnode-uuid@1.4.4&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;All goes well. Or does it?&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s look at the output:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;C:\__SOURCES&amp;gt;mkdir test&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;C:\__SOURCES&amp;gt;cd test&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;C:\__SOURCES\test&amp;gt;npm install node-uuid@1.4.4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;npm http GET https:&#x2F;&#x2F;registry.npmjs.org&#x2F;node-uuid&#x2F;1.4.4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;npm http 404 https:&#x2F;&#x2F;registry.npmjs.org&#x2F;node-uuid&#x2F;1.4.4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;node-uuid@1.4.4 node_modules\node-uuid&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Notice the &lt;code&gt;404&lt;&#x2F;code&gt;? I didn&#x27;t... But it&#x27;s important!&lt;&#x2F;p&gt;
&lt;p&gt;Now here&#x27;s what happened: locally I had &lt;code&gt;node-uuid@1.4.4&lt;&#x2F;code&gt; in my cache, so he took that one, even though the package disappeared from the registry.&lt;&#x2F;p&gt;
&lt;p&gt;However: my new instance on Elastic Beanstalk didn&#x27;t. That&#x27;s why it failed.&lt;&#x2F;p&gt;
&lt;p&gt;So, solutions:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Be careful when you &lt;code&gt;shrinkwrap&lt;&#x2F;code&gt;. Stuff might break in the future, as authors delete packages&lt;&#x2F;li&gt;
&lt;li&gt;Create a private feed, that you curate&lt;&#x2F;li&gt;
&lt;li&gt;As a package author, don&#x27;t delete packages. Just don&#x27;t. Other people might depend on you.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</description>
      </item>
      <item>
          <title>DynamoDb &amp; updating objects: it doesn&#x27;t react like SQL!</title>
          <pubDate>Wed, 05 Aug 2015 19:34:32 +0000</pubDate>
          <author>Kristof</author>
          <link>https://mattei.io/dynamodb-and-updating-objects-it-doesnt-react-like-sql/</link>
          <guid>https://mattei.io/dynamodb-and-updating-objects-it-doesnt-react-like-sql/</guid>
          <description xml:base="https://mattei.io/dynamodb-and-updating-objects-it-doesnt-react-like-sql/">&lt;p&gt;Today I stumbled upon the following bug:&lt;&#x2F;p&gt;
&lt;p&gt;We had an object with some properties that we wanted to update, but only if a certain property of that object is not set, i.e. it should be null.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;    &amp;quot;Id&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt; &#x2F;&#x2F; Id is the HashKey&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In this case we wanted to update the object with Id 1, and set an attribute called &lt;code&gt;Foo&lt;&#x2F;code&gt; to &lt;code&gt;&quot;Bar&quot;&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;To do this I wrote the following Javascript, using the &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;docs.aws.amazon.com&#x2F;AWSJavaScriptSDK&#x2F;latest&#x2F;index.html&quot;&gt;aws-sdk&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;function&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; updateObject&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt;id&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;    var&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; dynamodb&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; = new&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other z-constant&quot;&gt; AWS&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;DynamoDB&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;    dynamodb.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;updateItem&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;            Id: id,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        },&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            UpdateExpression:&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;SET Foo = :value&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            ExpressionAttributeValues: {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;                &amp;quot;:value&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;Bar&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            },&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            ConditionExpression:&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;attribute_not_exists(Foo)&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        },&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;        function&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt;error&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; data&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;            if&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; (error) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;                &#x2F;&#x2F; TODO check that the error is a ConditionalCheckFailedException, in&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;                &#x2F;&#x2F; which case the Condition failed, otherwise something else might be off.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;                console.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;quot;Error&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;                console.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;quot;All good, we&amp;#39;ve updated the object&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        },&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    );&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Perfect!&lt;&#x2F;p&gt;
&lt;p&gt;Now assume we have have a range of 1 -&amp;gt; 12 in our table, where half of them already have the &lt;code&gt;Foo&lt;&#x2F;code&gt; attribute, so we should get 50% &lt;code&gt;Error&lt;&#x2F;code&gt;, and 50% &lt;code&gt;All good, ...&lt;&#x2F;code&gt; (which is the case).&lt;&#x2F;p&gt;
&lt;p&gt;However, what do we expect when we update an item with Id 13?&lt;&#x2F;p&gt;
&lt;p&gt;When I, in my mind, which talks (used to) talk SQL when thinging about a database, updating something that is not there, doesn&#x27;t do anything.&lt;&#x2F;p&gt;
&lt;p&gt;Consider the following table:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;sql&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;CREATE TABLE&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Test&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Id &lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type z-keyword&quot;&gt;INT NOT NULL&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Foo &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;NVARCHAR&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;255&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With the following query:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;sql&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;INSERT INTO&lt;&#x2F;span&gt;&lt;span&gt; Test (Id, Foo) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;VALUES&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;), (&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span class=&quot;z-string z-punctuation z-definition z-string&quot;&gt;N&amp;#39;Bar&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;), (&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;GO&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;--SELECT * FROM Test&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;--GO&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;UPDATE&lt;&#x2F;span&gt;&lt;span&gt; Test &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;SET&lt;&#x2F;span&gt;&lt;span&gt; Foo &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;#39;Bar&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; WHERE&lt;&#x2F;span&gt;&lt;span&gt; Id &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; AND&lt;&#x2F;span&gt;&lt;span&gt; Foo &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;IS NULL&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;IF&lt;&#x2F;span&gt;&lt;span&gt; @@ROWCOUNT &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;BEGIN&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    SELECT&lt;&#x2F;span&gt;&lt;span class=&quot;z-string z-punctuation z-definition z-string&quot;&gt; N&amp;#39;1 updated, set Foo to Bar&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;END&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;ELSE&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;BEGIN&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    SELECT&lt;&#x2F;span&gt;&lt;span class=&quot;z-string z-punctuation z-definition z-string&quot;&gt; N&amp;#39;1 not updated, Foo was already set&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;END&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;GO&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;--SELECT * FROM Test&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;--GO&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;UPDATE&lt;&#x2F;span&gt;&lt;span&gt; Test &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;SET&lt;&#x2F;span&gt;&lt;span&gt; Foo &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;#39;Bar&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; WHERE&lt;&#x2F;span&gt;&lt;span&gt; Id &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; AND&lt;&#x2F;span&gt;&lt;span&gt; Foo &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;IS NULL&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;IF&lt;&#x2F;span&gt;&lt;span&gt; @@ROWCOUNT &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;BEGIN&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    SELECT&lt;&#x2F;span&gt;&lt;span class=&quot;z-string z-punctuation z-definition z-string&quot;&gt; N&amp;#39;2 updated, set Foo to Bar&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;END&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;ELSE&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;BEGIN&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    SELECT&lt;&#x2F;span&gt;&lt;span class=&quot;z-string z-punctuation z-definition z-string&quot;&gt; N&amp;#39;2 not updated, Foo was already set&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;END&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;--SELECT * FROM Test&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;--GO&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;UPDATE&lt;&#x2F;span&gt;&lt;span&gt; Test &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;SET&lt;&#x2F;span&gt;&lt;span&gt; Foo &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;#39;Bar&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; WHERE&lt;&#x2F;span&gt;&lt;span&gt; Id &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; AND&lt;&#x2F;span&gt;&lt;span&gt; Foo &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;IS NULL&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt; -- 7 Doesn&amp;#39;t exist!&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;IF&lt;&#x2F;span&gt;&lt;span&gt; @@ROWCOUNT &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;BEGIN&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    SELECT&lt;&#x2F;span&gt;&lt;span class=&quot;z-string z-punctuation z-definition z-string&quot;&gt; N&amp;#39;7 updated, set Foo to Bar&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;END&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;ELSE&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;BEGIN&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    SELECT&lt;&#x2F;span&gt;&lt;span class=&quot;z-string z-punctuation z-definition z-string&quot;&gt; N&amp;#39;7 not updated, because 7 doesn&amp;#39;&amp;#39;t exist!&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;END&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will print, along with some empty result sets, the following:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1 updated, set Foo to Bar&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;2 not updated, Foo was already set&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;7 not updated, because 7 doesn&amp;#39;t exist!&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, that knowledge in SQL doesn&#x27;t apply to DynamoDb.&lt;&#x2F;p&gt;
&lt;p&gt;While testing on some non-existing values we saw that our code passed the testcases perfectly. That&#x27;s not how it should be.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s take a look again at the &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;docs.aws.amazon.com&#x2F;AWSJavaScriptSDK&#x2F;latest&#x2F;AWS&#x2F;DynamoDB.html#updateItem-property&quot;&gt;documentation&lt;&#x2F;a&gt;, this time do actually read the first line:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Edits an existing item&#x27;s attributes, &lt;strong&gt;or adds a new item to the table if it does not already exist&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;(emphasis mine).&lt;&#x2F;p&gt;

&lt;img
    src=&quot;&amp;#x2F;dynamodb-and-updating-objects-it-doesnt-react-like-sql&amp;#x2F;&#x2F;homer-computer-doh.jpg&quot;
    &#x2F;&gt;

&lt;p&gt;So we need to guard ourselves against updates on non-existing items? How do we do that? Let&#x27;s extend our &lt;code&gt;ConditionExpression&lt;&#x2F;code&gt;. Start by taking the original code, and change the &lt;code&gt;ConditionExpression&lt;&#x2F;code&gt; as highlighted:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;function&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; updateObject&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt;id&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;    var&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; dynamodb&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; = new&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other z-constant&quot;&gt; AWS&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;DynamoDB&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;    dynamodb.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;updateItem&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;            Id: id,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        },&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            UpdateExpression:&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;SET Foo = :value&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            ExpressionAttributeValues: {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;                &amp;quot;:id&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;: id,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;                &amp;quot;:value&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;Bar&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            },&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;            &#x2F;&#x2F; make sure the object we&amp;#39;re updating actually has&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;            &#x2F;&#x2F; :id as Id, the side-effect of this is that if none of those&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;            &#x2F;&#x2F; is found, it will throw a ConditionalCheckFailedException&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;            &#x2F;&#x2F; which is what we want&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            ConditionExpression:&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;Id = :id AND attribute_not_exists(Foo)&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        },&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;        function&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt;error&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; data&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;            if&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; (error) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;                &#x2F;&#x2F; TODO check that the error is a ConditionalCheckFailedException, in&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;                &#x2F;&#x2F; which case the Condition failed, otherwise something else might be off.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;                console.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;quot;Error&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;                console.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;quot;All good, we&amp;#39;ve updated the object&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        },&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    );&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;</description>
      </item>
      <item>
          <title>AWS &amp; Encryption keys: Revert manually edited policy</title>
          <pubDate>Sun, 14 Jun 2015 14:04:58 +0000</pubDate>
          <author>Kristof</author>
          <link>https://mattei.io/aws-and-encryption-keys-revert-manually-edited-policy/</link>
          <guid>https://mattei.io/aws-and-encryption-keys-revert-manually-edited-policy/</guid>
          <description xml:base="https://mattei.io/aws-and-encryption-keys-revert-manually-edited-policy/">&lt;p&gt;Since we&#x27;ve been working with AWS we sometimes did stuff that, after looking back on it, wasn&#x27;t the best approach.&lt;&#x2F;p&gt;
&lt;p&gt;One of those things was manually applying Key Policies on Encryption Keys.&lt;&#x2F;p&gt;
&lt;p&gt;It currently looked like this:&lt;&#x2F;p&gt;

&lt;img
    src=&quot;&amp;#x2F;aws-and-encryption-keys-revert-manually-edited-policy&amp;#x2F;&#x2F;manually-edited-key-policy.png&quot;
    &#x2F;&gt;

&lt;p&gt;Notice the sentence:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;We&#x27;ve detected that the policy document for this key has been manually edited. You may now edit the document directly to make changes to permissions.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;This gives a lot of issues, for example, you cannot view grants anymore through the UI, nor can you easily add &amp;amp; remove Key Administrators. While the API allows you to modify the grants, that wasn&#x27;t enough for simple changes we&#x27;d like to make when testing &#x2F; operating our products.&lt;&#x2F;p&gt;
&lt;p&gt;Because of the fact that you cannot delete nor reset keys in AWS, you have to find another way.&lt;&#x2F;p&gt;
&lt;p&gt;So I do have another key that shows me the UI I want, where I can modify Key Administrators and Key Usage.&lt;&#x2F;p&gt;
&lt;p&gt;So, what do we do then? We fetch the correct policy from a key that shows the correct UI and see whether we can apply it to our &#x27;broken&#x27; key, and see if it works. (Spoiler, it does).&lt;&#x2F;p&gt;
&lt;p&gt;_Should you not have a &#x27;working&#x27; key (as described next), and don&#x27;t want to create a new one for the sake of doing this (you can&#x27;t delete a key, so I completely understand), click here to scroll down to the correct policy. _&lt;&#x2F;p&gt;
&lt;p&gt;First, let&#x27;s get the ARN of a working key, just navigate to the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;console.aws.amazon.com&#x2F;iam&#x2F;home?region=eu-west-1#encryptionKeys&quot;&gt;Encryption Key section in the IAM Management console&lt;&#x2F;a&gt;, set your region and select your key, and copy the ARN:&lt;&#x2F;p&gt;

&lt;figure&gt;
    &lt;img
    src=&quot;&amp;#x2F;aws-and-encryption-keys-revert-manually-edited-policy&amp;#x2F;&#x2F;find-the-ARN.png&quot;
    &#x2F;&gt;
    &lt;figcaption&gt;Find the ARN&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;

&lt;p&gt;So, how do we get that correct policy? Let&#x27;s use Python with boto3.&lt;&#x2F;p&gt;
&lt;p&gt;First of all we make sure we have an account in&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;%userprofile%\.aws\credentials&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you don&#x27;t please follow the steps &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;docs.aws.amazon.com&#x2F;cli&#x2F;latest&#x2F;userguide&#x2F;cli-chap-getting-started.html#cli-config-files&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Next up is ensuring we have boto3 installed. Fire up a cmd window and execute the following:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;pip install boto3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When that&#x27;s done, we can open Python, and that key for its policy.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; boto3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;kms&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; boto3.client(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;quot;iam&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;policy&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; kms.get_key_policy(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt;KeyId&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;quot;THE ARN YOU JUST GOT FROM A WORKING KEY&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; PolicyName&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;quot;default&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)[&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;quot;Policy&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt; policy&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;2 things here:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Do paste in the correct ARN!&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Why &lt;code&gt;default&lt;&#x2F;code&gt; as policy name? That&#x27;s the &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;docs.aws.amazon.com&#x2F;kms&#x2F;latest&#x2F;APIReference&#x2F;API_GetKeyPolicy.html#API_GetKeyPolicy_RequestSyntax&quot;&gt;only one they support&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;That policy is a JSON string. It&#x27;s full of &lt;code&gt;\n&lt;&#x2F;code&gt; gibberish, so let&#x27;s trim that out (in the same window, we reuse that &lt;code&gt;policy&lt;&#x2F;code&gt; variable):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; json&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;json.dumps(json.loads(policy))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which should give you this beautiful JSON document:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;#39;{&amp;quot;Version&amp;quot;: &amp;quot;2012-10-17&amp;quot;, &amp;quot;Id&amp;quot;: &amp;quot;key-consolepolicy-2&amp;quot;, &amp;quot;Statement&amp;quot;: [{&amp;quot;Action&amp;quot;: &amp;quot;kms:*&amp;quot;, &amp;quot;Principal&amp;quot;: {&amp;quot;AWS&amp;quot;: &amp;quot;arn:aws:iam::************:root&amp;quot;}, &amp;quot;Resource&amp;quot;: &amp;quot;*&amp;quot;, &amp;quot;Effect&amp;quot;: &amp;quot;Allow&amp;quot;, &amp;quot;Sid&amp;quot;: &amp;quot;Enable IAM User Permissions&amp;quot;}, {&amp;quot;Action&amp;quot;: [&amp;quot;kms:Describe*&amp;quot;, &amp;quot;kms:Put*&amp;quot;, &amp;quot;kms:Create*&amp;quot;, &amp;quot;kms:Update*&amp;quot;, &amp;quot;kms:Enable*&amp;quot;, &amp;quot;kms:Revoke*&amp;quot;, &amp;quot;kms:List*&amp;quot;, &amp;quot;kms:Get*&amp;quot;, &amp;quot;kms:Disable*&amp;quot;, &amp;quot;kms:Delete*&amp;quot;], &amp;quot;Resource&amp;quot;: &amp;quot;*&amp;quot;, &amp;quot;Effect&amp;quot;: &amp;quot;Allow&amp;quot;, &amp;quot;Sid&amp;quot;: &amp;quot;Allow access for Key Administrators&amp;quot;}, {&amp;quot;Action&amp;quot;: [&amp;quot;kms:DescribeKey&amp;quot;, &amp;quot;kms:GenerateDataKey*&amp;quot;, &amp;quot;kms:Encrypt&amp;quot;, &amp;quot;kms:ReEncrypt*&amp;quot;, &amp;quot;kms:Decrypt&amp;quot;], &amp;quot;Resource&amp;quot;: &amp;quot;*&amp;quot;, &amp;quot;Effect&amp;quot;: &amp;quot;Allow&amp;quot;, &amp;quot;Sid&amp;quot;: &amp;quot;Allow use of the key&amp;quot;}, {&amp;quot;Action&amp;quot;: [&amp;quot;kms:ListGrants&amp;quot;, &amp;quot;kms:CreateGrant&amp;quot;, &amp;quot;kms:RevokeGrant&amp;quot;], &amp;quot;Resource&amp;quot;: &amp;quot;*&amp;quot;, &amp;quot;Effect&amp;quot;: &amp;quot;Allow&amp;quot;, &amp;quot;Condition&amp;quot;: {&amp;quot;Bool&amp;quot;: {&amp;quot;kms:GrantIsForAWSResource&amp;quot;: true}}, &amp;quot;Sid&amp;quot;: &amp;quot;Allow attachment of persistent resources&amp;quot;}]}&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;(!) Notice the single quotes in the beginning and the end. You DON&#x27;T want those. Also notice that I&#x27;ve removed my Account Id (replaced by asterisks), so if you&#x27;re just copy pasting, make sure you replace them by your own Account Id, which you can find &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;console.aws.amazon.com&#x2F;billing&#x2F;home?#&#x2F;account&quot;&gt;here&lt;&#x2F;a&gt; (middle, Account Id, 12 digit number).&lt;&#x2F;p&gt;
&lt;p&gt;Now let&#x27;s go to our broken key again, and in the policy field we paste in our just-retrieved working policy.&lt;&#x2F;p&gt;
&lt;p&gt;Hit the save button, and lo and behold, we revert back to the original UI.&lt;&#x2F;p&gt;
&lt;p&gt;Succes!&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Topshelf install, PowerShell and Get-Credentials</title>
          <pubDate>Thu, 15 Jan 2015 15:10:27 +0000</pubDate>
          <author>Kristof</author>
          <link>https://mattei.io/topshelf-install-powershell-and-get-credentials/</link>
          <guid>https://mattei.io/topshelf-install-powershell-and-get-credentials/</guid>
          <description xml:base="https://mattei.io/topshelf-install-powershell-and-get-credentials/">&lt;p&gt;In the project I&#x27;m currently working at we use PowerShell script for configuration and build execution.&lt;&#x2F;p&gt;
&lt;p&gt;This means that if you get a new laptop, or a new member joins the team, or even when you need to change your Windows password, you just need to run the script again and it will set up everything in the correct locations &amp;amp; with the correct credentials.&lt;&#x2F;p&gt;
&lt;p&gt;The credentials were a problem though.&lt;&#x2F;p&gt;
&lt;p&gt;When installing a Topshelf service with the &lt;code&gt;--interactive&lt;&#x2F;code&gt; parameter (we need to install under the current user, not &lt;code&gt;System&lt;&#x2F;code&gt;) it will prompt you for your credentials for each service you want to install. For one, it&#x27;s fine, for 2, it&#x27;s already boring, for 3, ... You get the point.&lt;&#x2F;p&gt;
&lt;p&gt;We initially used the following command line to install the services:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;powershell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; $pathToServiceExe&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; --&lt;&#x2F;span&gt;&lt;span&gt;install &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;--&lt;&#x2F;span&gt;&lt;span&gt;interactive &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;--&lt;&#x2F;span&gt;&lt;span&gt;autostart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To fix this we will give the &lt;code&gt;$pathToServiceExe&lt;&#x2F;code&gt; the username and password ourselves with the &lt;code&gt;-username&lt;&#x2F;code&gt; and &lt;code&gt;-password&lt;&#x2F;code&gt;. We should also omit the &lt;code&gt;--interactive&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;First gotcha here: When reading the &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;topshelf.readthedocs.org&#x2F;en&#x2F;latest&#x2F;overview&#x2F;commandline.html&quot;&gt;documentation&lt;&#x2F;a&gt;, it says one must specify the commands in this format:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;powershell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; $pathToServiceExe&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; --&lt;&#x2F;span&gt;&lt;span&gt;install &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;--&lt;&#x2F;span&gt;&lt;span&gt;autostart &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;username:username &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;password:password&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, this is not the case. You mustn&#x27;t separate the command line argument and the value with a &lt;code&gt;:&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Now, we don&#x27;t want to hardcode the username &amp;amp; password file in the setup script.&lt;&#x2F;p&gt;
&lt;p&gt;So let&#x27;s get the credentials of the current user:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;powershell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;$credentialsOfCurrentUser&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; Get-Credential&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt;Message &lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;quot;Please enter your username &amp;amp; password for the service installs&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next up we should extract the username &amp;amp; password of the &lt;code&gt;$credentialsOfCurrentUser&lt;&#x2F;code&gt; variable, as we need it in clear-text (potential security risk!).&lt;&#x2F;p&gt;
&lt;p&gt;One can do this in 2 ways, either by getting the &lt;code&gt;NetworkCredential&lt;&#x2F;code&gt; from the &lt;code&gt;PSCredential&lt;&#x2F;code&gt; with &lt;code&gt;GetNetworkCredential()&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;powershell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;$networkCredentials&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; $credentialsOfCurrentUser.GetNetworkCredential();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;$username&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;quot;{0}\{1}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; -f&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; $networkCredentials.Domain&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; $networkCredentials.UserName&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt; # change this if you want the user@domain syntax, it will then have an empty Domain and everything will be in UserName.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;$password&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; $networkCredentials.Password&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Notice the &lt;code&gt;$username&lt;&#x2F;code&gt; caveat.&lt;&#x2F;p&gt;
&lt;p&gt;Or, by not converting it to a &lt;code&gt;NetworkCredential&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;powershell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# notice the UserName contains the Domain AND the UserName, no need to extract it separately&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;$username&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; $credentialsOfCurrentUser.UserName&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# little more for the password&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;$BSTR&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;System.Runtime.InteropServices.Marshal&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;]::SecureStringToBSTR($credentialsOfCurrentUser.Password)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;$password&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;System.Runtime.InteropServices.Marshal&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;]::PtrToStringAuto($BSTR)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Notice the extra code to retrieve the &lt;code&gt;$password&lt;&#x2F;code&gt; in plain-text.&lt;&#x2F;p&gt;
&lt;p&gt;I would recommend combining both, using the &lt;code&gt;NetworkCredential&lt;&#x2F;code&gt; for the &lt;code&gt;$password&lt;&#x2F;code&gt;, but the regular &lt;code&gt;PSCredential&lt;&#x2F;code&gt; for the &lt;code&gt;$username&lt;&#x2F;code&gt; as then you&#x27;re not dependent on how your user enters his username.&lt;&#x2F;p&gt;
&lt;p&gt;So the best version is:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;powershell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;$credentialsOfCurrentUser&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; Get-Credential&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt;Message &lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;quot;Please enter your username &amp;amp; password for the service installs&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;$networkCredentials&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; $credentialsOfCurrentUser.GetNetworkCredential();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;$username&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; $credentialsOfCurrentUser.UserName&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;$password&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; $networkCredentials.Password&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that we have those variables we can pass them on to the install of the Topshelf exe:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;powershell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; $pathToServiceExe install &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;username &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;`&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;$username&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;`&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt;password &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;`&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;$password&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;`&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; --&lt;&#x2F;span&gt;&lt;span&gt;autostart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Notice the backticks (`) to ensure the double quotes are escaped.&lt;&#x2F;p&gt;
&lt;p&gt;In this way you can install all your services and only prompt your user for his credentials once!&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>When frameworks try to be smart, AngularJS &amp; Expressions</title>
          <pubDate>Fri, 28 Nov 2014 09:40:41 +0000</pubDate>
          <author>Kristof</author>
          <link>https://mattei.io/when-frameworks-try-to-be-smart-angularjs-and-expressions/</link>
          <guid>https://mattei.io/when-frameworks-try-to-be-smart-angularjs-and-expressions/</guid>
          <description xml:base="https://mattei.io/when-frameworks-try-to-be-smart-angularjs-and-expressions/">&lt;p&gt;One of my colleagues just discovered this bug&#x2F;feature in AngularJS. Using an &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.angularjs.org&#x2F;api&#x2F;ng&#x2F;directive&#x2F;ngIf&quot;&gt;ngIf&lt;&#x2F;a&gt; on a string &lt;code&gt;&quot;no&quot;&lt;&#x2F;code&gt; will result in &lt;code&gt;false&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;HTML:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;html&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-tag&quot;&gt;div&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity&quot;&gt; ng-app&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-tag&quot;&gt;div&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity&quot;&gt; ng-controller&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;quot;yesNoController&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-tag&quot;&gt;div&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity&quot;&gt; ng-if&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;quot;yes&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;Yes is defined, will display&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-tag&quot;&gt;div&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-tag&quot;&gt;div&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity&quot;&gt; ng-if&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;quot;no&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;No is defined, but will not display on Angular 1.2.1&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-tag&quot;&gt;div&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        &amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-tag&quot;&gt;div&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity&quot;&gt; ng-if&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;quot;notDefined&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;Not defined, will not display&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-tag&quot;&gt;div&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-tag&quot;&gt;div&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-tag&quot;&gt;div&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;JavaScript:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;function&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; yesNoController&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt;$scope&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;    $scope.yes&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;yes&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;    $scope.no&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;no&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;    $scope.notDefined&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; undefined&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Will print:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Yes is defined, will display&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s read the documentation on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.angularjs.org&#x2F;guide&#x2F;expression&quot;&gt;expression&lt;&#x2F;a&gt;, to see where this case is covered.&lt;&#x2F;p&gt;
&lt;p&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Can you find it?&lt;&#x2F;p&gt;
&lt;p&gt;Neither can I.&lt;&#x2F;p&gt;
&lt;p&gt;Fix?&lt;&#x2F;p&gt;
&lt;p&gt;Use the double bang:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&#x2F;&#x2F; ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;div ng-if=&amp;quot;!!no&amp;quot;&amp;gt;No is defined, but we need to add a double bang for it to parse correctly&amp;lt;&#x2F;div&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&#x2F;&#x2F; ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;JSFiddle can be found &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;jsfiddle.net&#x2F;t56onm8o&#x2F;5&#x2F;&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;For those who care, it&#x27;s not a JS thing:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&#x2F;&#x2F; execute this line in a console&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;alert(&amp;quot;no&amp;quot; ? &amp;quot;no evaluated as true&amp;quot; : &amp;quot;no evaluated as false&amp;quot;); &#x2F;&#x2F; will alert &amp;quot;no evaluated as true&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;</description>
      </item>
      <item>
          <title>Enabling dynamic compression (gzip) for WebAPI and IIS</title>
          <pubDate>Mon, 27 Oct 2014 18:43:08 +0000</pubDate>
          <author>Kristof</author>
          <link>https://mattei.io/enabling-dynamic-compression-gzip-for-webapi-and-iis/</link>
          <guid>https://mattei.io/enabling-dynamic-compression-gzip-for-webapi-and-iis/</guid>
          <description xml:base="https://mattei.io/enabling-dynamic-compression-gzip-for-webapi-and-iis/">&lt;p&gt;A lot of code on the internet refers to writing custom ActionFilters, or even HttpHandlers that will compress your return payload for you.&lt;&#x2F;p&gt;
&lt;p&gt;For example, see this &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;azzlack&#x2F;Microsoft.AspNet.WebApi.MessageHandlers.Compression&#x2F;&quot;&gt;package&lt;&#x2F;a&gt; (which with its name implies that it is Microsoft, but then says it&#x27;s not Microsoft).&lt;&#x2F;p&gt;
&lt;p&gt;At the moment of writing the above-linked package even throws an error when you return a 200 OK without a body...&lt;&#x2F;p&gt;
&lt;p&gt;But in the end, it&#x27;s very simple to enable compression on your IIS server without writing a single line of code:&lt;&#x2F;p&gt;
&lt;p&gt;You first need to install the IIS Dynamic Content Compression module:&lt;&#x2F;p&gt;

&lt;img
    src=&quot;&amp;#x2F;enabling-dynamic-compression-gzip-for-webapi-and-iis&amp;#x2F;&#x2F;dynamic_content_compression.png&quot;
    &#x2F;&gt;

&lt;p&gt;Or, if you&#x27;re a command line guy, execute the following command in an elevated CMD:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;dism &#x2F;online &#x2F;Enable-Feature &#x2F;FeatureName:IIS-HttpCompressionDynamic&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next up you need to enable the Dynamic Content Compression to compress&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;application&#x2F;json&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;and&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;application&#x2F;json; charset=utf-8&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To do this, execute the following commands in an elevated CMD:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cd c:\Windows\System32\inetsrv&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;appcmd.exe set config -section:system.webServer&#x2F;httpCompression &#x2F;+&amp;quot;dynamicTypes.[mimeType=&amp;#39;application&#x2F;json&amp;#39;,enabled=&amp;#39;True&amp;#39;]&amp;quot; &#x2F;commit:apphost&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;appcmd.exe set config -section:system.webServer&#x2F;httpCompression &#x2F;+&amp;quot;dynamicTypes.[mimeType=&amp;#39;application&#x2F;json; charset=utf-8&amp;#39;,enabled=&amp;#39;True&amp;#39;]&amp;quot; &#x2F;commit:apphost&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;This adds the 2 mimetypes to the list of types the module is allowed to compress. Validate that they are added with this command:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;appcmd.exe list config -section:system.webServer&#x2F;httpCompression&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Validate that the 2 mimetypes are there and enabled:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;img&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    src=&amp;quot;&amp;amp;#x2F;enabling-dynamic-compression-gzip-for-webapi-and-iis&amp;amp;#x2F;&#x2F;cmd.png&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &#x2F;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;And lastly, you&amp;#39;ll probably need to restart the Windows Process Activation Service.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Best is to do this through the UI because I have yet to find a way in CMD to restart a service (can&amp;#39;t seem to start services that are dependent on the one we just started).&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;In services.msc you&amp;#39;ll need to search for Windows Process Activation Service. Restart it.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;img&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    src=&amp;quot;&amp;amp;#x2F;enabling-dynamic-compression-gzip-for-webapi-and-iis&amp;amp;#x2F;&#x2F;wpas.png&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &#x2F;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Obviously there are more settings available, take a look at the [httpCompression Element](http:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;ms690689.aspx) settings page.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;I recommend reading about 2 at least:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;* dynamicCompressionDisableCpuUsage&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;* noCompressionForProxies&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Good luck,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-Kristof&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;</description>
      </item>
      <item>
          <title>Update on handedness (menu location)</title>
          <pubDate>Sat, 02 Aug 2014 14:30:20 +0000</pubDate>
          <author>Kristof</author>
          <link>https://mattei.io/update-on-handedness-menu-location/</link>
          <guid>https://mattei.io/update-on-handedness-menu-location/</guid>
          <description xml:base="https://mattei.io/update-on-handedness-menu-location/">&lt;p&gt;A while back I wrote how to change the &lt;a href=&quot;..&#x2F;2014-05-15-menu-on-the-wrong-side-with-a-touch-screen&#x2F;index.md&quot;&gt;handedness&lt;&#x2F;a&gt; (which seems to be the correct term, instead of the dreadful &#x27;Menu on the wrong side with a touch screen&#x27;).&lt;&#x2F;p&gt;
&lt;p&gt;I got a machine in my hands which exhibited the previously mentioned problem. However Tablet PC Settings weren&#x27;t installed, so we couldn&#x27;t open the tab.&lt;&#x2F;p&gt;
&lt;p&gt;After searching the bowels of the internet I found the following shell shortcut:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;shell:::{80F3F1D5-FECA-45F3-BC32-752C152E456E}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Putting this in Winkey+R, or in the Windows 7&#x2F;8(.1) search box will open the Tablet PC Settings, and if you don&#x27;t have a touch screen, will default to the Other tab, where you can change the handedness of your menus!&lt;&#x2F;p&gt;

&lt;img
    src=&quot;&amp;#x2F;update-on-handedness-menu-location&amp;#x2F;&#x2F;left_handed.png&quot;
    &#x2F;&gt;

&lt;p&gt;Have a good one,&lt;&#x2F;p&gt;
&lt;p&gt;-Kristof&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>The impact of SqlDataReader.GetOrdinal on performance</title>
          <pubDate>Sun, 25 May 2014 11:56:53 +0000</pubDate>
          <author>Kristof</author>
          <link>https://mattei.io/the-impact-of-sqldatareader-getordinal-on-performance/</link>
          <guid>https://mattei.io/the-impact-of-sqldatareader-getordinal-on-performance/</guid>
          <description xml:base="https://mattei.io/the-impact-of-sqldatareader-getordinal-on-performance/">&lt;p&gt;I recently had a discussion about the impact of &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;system.data.sqlclient.sqldatareader.getordinal.aspx&quot;&gt;&lt;code&gt;SqlDataReader.GetOrdinal&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; on execution of a &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;system.data.sqlclient.sqlcommand.aspx&quot;&gt;&lt;code&gt;SqlClient.SqlCommand&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. I then decided to run some code to measure the difference, because I think that&#x27;s the only way to get a decent opinion. This is the code that I&#x27;ve used to run a certain query 1000 times:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-keyword&quot;&gt;private void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; InvokeQuery&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Action mapObject&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Stopwatch stopwatch&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; Stopwatch.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;StartNew&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;int&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; i&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;; i&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; Iterations; i&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;++&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        using&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;var&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; sqlCommand&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; = new&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; SqlCommand&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;this&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;._query,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; this&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;._sqlConnection))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;            using&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;SqlDataReader sqlDataReader&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; sqlCommand.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;ExecuteReader&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;                while&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; (sqlDataReader.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;NextResult&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;                    mapObject&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;(sqlDataReader);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;    stopwatch.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Stop&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;    Debug.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;WriteLine&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;quot;Running {0} queries took {1} milliseconds!&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;, Iterations, stopwatch.ElapsedMilliseconds);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;mapObject&lt;&#x2F;code&gt; uses either directly the ordinal, or fetches the ordinal based on the column name. Also, I moved everything inside of the &lt;code&gt;for&lt;&#x2F;code&gt; loop to ensure nothing could be reused between queries. Here are the &lt;code&gt;mapObject&lt;&#x2F;code&gt; &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;system.action.aspx&quot;&gt;&lt;code&gt;Action&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;s, with &lt;code&gt;GetOrdinal&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Action&amp;lt;SqlDataReader&amp;gt; = sqlDataReader =&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int salesOrderID = sqlDataReader.GetOrdinal(&amp;quot;SalesOrderID&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int revisionNumber = sqlDataReader.GetOrdinal(&amp;quot;RevisionNumber&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int orderDate = sqlDataReader.GetOrdinal(&amp;quot;OrderDate&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int dueDate = sqlDataReader.GetOrdinal(&amp;quot;DueDate&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int shipDate = sqlDataReader.GetOrdinal(&amp;quot;ShipDate&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int status = sqlDataReader.GetOrdinal(&amp;quot;Status&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int onlineOrderFlag = sqlDataReader.GetOrdinal(&amp;quot;OnlineOrderFlag&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int salesOrderNumber = sqlDataReader.GetOrdinal(&amp;quot;SalesOrderNumber&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int purchaseOrderNumber = sqlDataReader.GetOrdinal(&amp;quot;PurchaseOrderNumber&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int accountNumber = sqlDataReader.GetOrdinal(&amp;quot;AccountNumber&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int customerID = sqlDataReader.GetOrdinal(&amp;quot;CustomerID&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int salesPersonID = sqlDataReader.GetOrdinal(&amp;quot;SalesPersonID&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int territoryID = sqlDataReader.GetOrdinal(&amp;quot;TerritoryID&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int billToAddressID = sqlDataReader.GetOrdinal(&amp;quot;BillToAddressID&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int shipToAddressID = sqlDataReader.GetOrdinal(&amp;quot;ShipToAddressID&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int shipMethodID = sqlDataReader.GetOrdinal(&amp;quot;ShipMethodID&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int creditCardID = sqlDataReader.GetOrdinal(&amp;quot;CreditCardID&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int creditCardApprovalCode = sqlDataReader.GetOrdinal(&amp;quot;CreditCardApprovalCode&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int currencyRateID = sqlDataReader.GetOrdinal(&amp;quot;CurrencyRateID&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int subTotal = sqlDataReader.GetOrdinal(&amp;quot;SubTotal&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int taxAmt = sqlDataReader.GetOrdinal(&amp;quot;TaxAmt&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int freight = sqlDataReader.GetOrdinal(&amp;quot;Freight&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int totalDue = sqlDataReader.GetOrdinal(&amp;quot;TotalDue&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int comment = sqlDataReader.GetOrdinal(&amp;quot;Comment&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int rowguid = sqlDataReader.GetOrdinal(&amp;quot;rowguid&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    int modifiedDate = sqlDataReader.GetOrdinal(&amp;quot;ModifiedDate&amp;quot;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    var temp = new SalesOrderHeader(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        salesOrderID: sqlDataReader.GetInt32(salesOrderID),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        revisionNumber: sqlDataReader.GetInt16(revisionNumber),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        orderDate: sqlDataReader.GetDateTime(orderDate),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        dueDate: sqlDataReader.GetDateTime(dueDate),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        shipDate: sqlDataReader.GetDateTime(shipDate),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        status: sqlDataReader.GetInt16(status),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        onlineOrderFlag: sqlDataReader.GetBoolean(onlineOrderFlag),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        salesOrderNumber: sqlDataReader.GetString(salesOrderNumber),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        purchaseOrderNumber: sqlDataReader.GetString(purchaseOrderNumber),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        accountNumber: sqlDataReader.GetString(accountNumber),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        customerID: sqlDataReader.GetInt32(customerID),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        salesPersonID: sqlDataReader.GetInt32(salesPersonID),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        territoryID: sqlDataReader.GetInt32(territoryID),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        billToAddressID: sqlDataReader.GetInt32(billToAddressID),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        shipToAddressID: sqlDataReader.GetInt32(shipToAddressID),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        shipMethodID: sqlDataReader.GetInt32(shipMethodID),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        creditCardID: sqlDataReader.GetInt32(creditCardID),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        creditCardApprovalCode: sqlDataReader.GetString(creditCardApprovalCode),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        currencyRateID: sqlDataReader.GetInt32(currencyRateID),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        subTotal: sqlDataReader.GetDecimal(subTotal),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        taxAmt: sqlDataReader.GetDecimal(taxAmt),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        freight: sqlDataReader.GetDecimal(freight),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        totalDue: sqlDataReader.GetDecimal(totalDue),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        comment: sqlDataReader.GetString(comment),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        rowguid: sqlDataReader.GetGuid(rowguid),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        modifiedDate: sqlDataReader.GetDateTime(modifiedDate)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        );&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And without &lt;code&gt;GetOrdinal&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Action&amp;lt;SqlDataReader&amp;gt; mapSalesOrderHeader = sqlDataReader =&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    new SalesOrderHeader(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        salesOrderID: sqlDataReader.GetInt32(0),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        revisionNumber: sqlDataReader.GetInt16(1),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        orderDate: sqlDataReader.GetDateTime(2),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        dueDate: sqlDataReader.GetDateTime(3),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        shipDate: sqlDataReader.GetDateTime(4),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        status: sqlDataReader.GetInt16(5),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        onlineOrderFlag: sqlDataReader.GetBoolean(6),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        salesOrderNumber: sqlDataReader.GetString(7),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        purchaseOrderNumber: sqlDataReader.GetString(8),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        accountNumber: sqlDataReader.GetString(9),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        customerID: sqlDataReader.GetInt32(10),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        salesPersonID: sqlDataReader.GetInt32(11),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        territoryID: sqlDataReader.GetInt32(12),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        billToAddressID: sqlDataReader.GetInt32(13),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        shipToAddressID: sqlDataReader.GetInt32(14),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        shipMethodID: sqlDataReader.GetInt32(15),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        creditCardID: sqlDataReader.GetInt32(16),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        creditCardApprovalCode: sqlDataReader.GetString(17),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        currencyRateID: sqlDataReader.GetInt32(18),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        subTotal: sqlDataReader.GetDecimal(19),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        taxAmt: sqlDataReader.GetDecimal(20),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        freight: sqlDataReader.GetDecimal(21),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        totalDue: sqlDataReader.GetDecimal(22),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        comment: sqlDataReader.GetString(23),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        rowguid: sqlDataReader.GetGuid(24),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        modifiedDate: sqlDataReader.GetDateTime(25));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With &lt;code&gt;GetOrdinal&lt;&#x2F;code&gt; the results are:&lt;&#x2F;p&gt;

&lt;img
    src=&quot;&amp;#x2F;the-impact-of-sqldatareader-getordinal-on-performance&amp;#x2F;&#x2F;CreateWithGetOrdinal.png&quot;
    &#x2F;&gt;

&lt;p&gt;And without:&lt;&#x2F;p&gt;

&lt;img
    src=&quot;&amp;#x2F;the-impact-of-sqldatareader-getordinal-on-performance&amp;#x2F;&#x2F;CreateWithoutGetOrdinal.png&quot;
    &#x2F;&gt;

&lt;p&gt;As you can see the performance difference is so low that I honestly don&#x27;t think you should sacrifice the readability and maintainability of your code vs a mere 82 milliseconds on a 1000 queries. Readability speaks for itself, you don&#x27;t talk with &lt;code&gt;int&lt;&#x2F;code&gt;s anymore, and for maintainability, consider the following: If your query column(s) change and you forget to update your code, &lt;code&gt;GetOrdinal&lt;&#x2F;code&gt; will throw an &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;system.indexoutofrangeexception.aspx&quot;&gt;IndexOutOfRangeException&lt;&#x2F;a&gt;, instead of &lt;strong&gt;maybe&lt;&#x2F;strong&gt; get an &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;system.invalidcastexception.aspx&quot;&gt;InvalidCastException&lt;&#x2F;a&gt; or, if you&#x27;re really unlucky, another column and then broken code behavior... One &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;system.data.sqlclient.sqldatareader.getordinal.aspx&quot;&gt;sidenote&lt;&#x2F;a&gt; to add:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;GetOrdinal&lt;&#x2F;code&gt; performs a case-sensitive lookup first. If it fails, a second, case-insensitive search occurs (a case-insensitive comparison is done using the database collation). Unexpected results can occur when comparisons are affected by culture-specific casing rules. For example, in Turkish, the following example yields the wrong results because the file system in Turkish does not use linguistic casing rules for the letter &#x27;i&#x27; in &quot;file&quot;. The method throws an &lt;code&gt;IndexOutOfRange&lt;&#x2F;code&gt; exception if the zero-based column ordinal is not found.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;So do watch out that case.&lt;&#x2F;p&gt;
&lt;p&gt;PS: the project itself is hosted on GitHub, you can find it &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;kristof-mattei&#x2F;get-ordinal-or-not&quot;&gt;here&lt;&#x2F;a&gt;!&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Menu on the wrong side with a touch screen?</title>
          <pubDate>Thu, 15 May 2014 19:21:52 +0000</pubDate>
          <author>Kristof</author>
          <link>https://mattei.io/menu-on-the-wrong-side-with-a-touch-screen/</link>
          <guid>https://mattei.io/menu-on-the-wrong-side-with-a-touch-screen/</guid>
          <description xml:base="https://mattei.io/menu-on-the-wrong-side-with-a-touch-screen/">&lt;p&gt;When you&#x27;re reading this you probably have a touch screen.&lt;&#x2F;p&gt;
&lt;p&gt;So, I never use my touch screen. Almost never. But I did notice that by default my menus in Windows (from a menu bar, not a ribbon) appear (when possible) on the right side of the clicked menu item.&lt;&#x2F;p&gt;
&lt;p&gt;Like this:&lt;&#x2F;p&gt;

&lt;img
    src=&quot;&amp;#x2F;menu-on-the-wrong-side-with-a-touch-screen&amp;#x2F;&#x2F;menu_left.png&quot;
    &#x2F;&gt;

&lt;p&gt;Goosebumps. Something is off. It took me a while to realize this,&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;The menu expanded to the left!&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;So, what is this. I can&#x27;t remember what exactly I searched for, but the change you need to make is in &lt;strong&gt;Tablet PC Settings&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;When your menus are expanded to the left you&#x27;ll see something like this:&lt;&#x2F;p&gt;

&lt;img
    src=&quot;&amp;#x2F;menu-on-the-wrong-side-with-a-touch-screen&amp;#x2F;&#x2F;right_handed.png&quot;
    &#x2F;&gt;

&lt;p&gt;This is different from the default that I&#x27;ve been used to since I&#x27;ve been using Windows 95.&lt;&#x2F;p&gt;
&lt;p&gt;Change it to &#x27;Left-handed&#x27;:&lt;&#x2F;p&gt;

&lt;img
    src=&quot;&amp;#x2F;menu-on-the-wrong-side-with-a-touch-screen&amp;#x2F;&#x2F;left_handed.png&quot;
    &#x2F;&gt;

&lt;p&gt;Hit apply, and restart any offending programs, open a menu and enjoy:&lt;&#x2F;p&gt;

&lt;img
    src=&quot;&amp;#x2F;menu-on-the-wrong-side-with-a-touch-screen&amp;#x2F;&#x2F;menu_right.png&quot;
    &#x2F;&gt;

&lt;p&gt;I can rest easy again...&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>TransactionScope &amp; SqlConnection not rolling back? Here&#x27;s why...</title>
          <pubDate>Tue, 22 Apr 2014 16:39:29 +0000</pubDate>
          <author>Kristof</author>
          <link>https://mattei.io/transactionscope-and-sqlconnection-not-rolling-back-heres-why/</link>
          <guid>https://mattei.io/transactionscope-and-sqlconnection-not-rolling-back-heres-why/</guid>
          <description xml:base="https://mattei.io/transactionscope-and-sqlconnection-not-rolling-back-heres-why/">&lt;p&gt;A while back we ran into an issue with one of our projects where we executed a erroneous query (missing DELETE statement), and then left the database in an inconsistent state.&lt;&#x2F;p&gt;
&lt;p&gt;Which is weird, considering the fact that we use a &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;system.transactions.transactionscope.aspx&quot;&gt;&lt;code&gt;TransactionScope&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;After some digging around I found the behavior I wanted, and how to write it in correct C#.&lt;&#x2F;p&gt;
&lt;p&gt;Allow me to elaborate.&lt;&#x2F;p&gt;
&lt;p&gt;Consider a database with 3 tables:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;T2 --&amp;gt; T1 &amp;lt;-- T3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Where both &lt;code&gt;T2&lt;&#x2F;code&gt; and &lt;code&gt;T3&lt;&#x2F;code&gt; link to an entity in &lt;code&gt;T1&lt;&#x2F;code&gt;, thus we cannot delete lines from &lt;code&gt;T1&lt;&#x2F;code&gt; that are still referenced in &lt;code&gt;T2&lt;&#x2F;code&gt; or &lt;code&gt;T3&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;I jumped to C# and started playing with some code, and discovered the following (mind you, each piece of code is actually supposed to throw an exception and abort):&lt;&#x2F;p&gt;
&lt;p&gt;This doesn&#x27;t use a &lt;code&gt;TransactionScope&lt;&#x2F;code&gt;, thus leaving the database in an inconsistent state:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;using&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;var&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; sqlConnection&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; = new&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; SqlConnection&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;(ConnectionString))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;    sqlConnection.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Open&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    using&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;SqlCommand sqlCommand&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; sqlConnection.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;CreateCommand&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;        sqlCommand.CommandText&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;USE [TransactionScopeTests]; DELETE FROM T3; DELETE FROM T1;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;        &#x2F;&#x2F; DELETE FROM T1 will cause violation of integrity, because rows from T2 are still using rows from T1.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;        sqlCommand.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;ExecuteNonQuery&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now I wanted to wrap this in a TransactionScope, so I tried this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;using&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;var&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; sqlConnection&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; = new&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; SqlConnection&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;(ConnectionString))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;    sqlConnection.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Open&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    using&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;var&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; transactionScope&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; = new&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; TransactionScope&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        using&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;SqlCommand sqlCommand&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; sqlConnection.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;CreateCommand&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;            sqlCommand.CommandText&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;USE [TransactionScopeTests]; DELETE FROM T3; DELETE FROM T1;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;            sqlCommand.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;ExecuteNonQuery&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;        transactionScope.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Complete&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Well guess what, this essentially fixes nothing. The database, upon completion of the &lt;code&gt;ExecuteNonQuery()&lt;&#x2F;code&gt; is left in the same inconsistent state. &lt;code&gt;T3&lt;&#x2F;code&gt; was empty, which shouldn&#x27;t happen since the delete from &lt;code&gt;T1&lt;&#x2F;code&gt; failed.&lt;&#x2F;p&gt;
&lt;p&gt;So what is the correct behavior?&lt;&#x2F;p&gt;
&lt;p&gt;Well, it doesn&#x27;t matter whether you create the &lt;code&gt;TransactionScope&lt;&#x2F;code&gt; or the &lt;code&gt;SqlConnection&lt;&#x2F;code&gt; first, &lt;strong&gt;as long as you &lt;code&gt;Open()&lt;&#x2F;code&gt; the &lt;code&gt;SqlConnection&lt;&#x2F;code&gt; inside of the &lt;code&gt;TransactionScope&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;using&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;var&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; transactionScope&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; = new&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; TransactionScope&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    using&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;var&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; sqlConnection&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; = new&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; SqlConnection&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;(ConnectionString))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;        sqlConnection.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Open&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        using&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;SqlCommand sqlCommand&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; sqlConnection.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;CreateCommand&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;            sqlCommand.CommandText&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;USE [TransactionScopeTests]; DELETE FROM T3; DELETE FROM T1;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;            sqlCommand.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;ExecuteNonQuery&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;        transactionScope.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Complete&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or the inverse (swapping the declaration of the &lt;code&gt;TransactionScope&lt;&#x2F;code&gt; and &lt;code&gt;SqlConnection&lt;&#x2F;code&gt;):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;using&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;var&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; sqlConnection&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; = new&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; SqlConnection&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;(ConnectionString))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    using&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;var&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; transactionScope&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; = new&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; TransactionScope&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;        sqlConnection.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Open&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        using&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;SqlCommand sqlCommand&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; sqlConnection.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;CreateCommand&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;            sqlCommand.CommandText&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;USE [TransactionScopeTests]; DELETE FROM T3; DELETE FROM T1;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;            sqlCommand.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;ExecuteNonQuery&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;        transactionScope.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Complete&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I wrote the test cases on a project on GitHub which you can download, compile and run as Tests for yourself!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;kristof-mattei&#x2F;transaction-scope&quot;&gt;https:&#x2F;&#x2F;github.com&#x2F;kristof-mattei&#x2F;transaction-scope&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Have a good one,&lt;&#x2F;p&gt;
&lt;p&gt;-Kristof&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>About a dictionary, removing and adding items, and their order.</title>
          <pubDate>Tue, 04 Mar 2014 20:52:00 +0000</pubDate>
          <author>Kristof</author>
          <link>https://mattei.io/about-a-dictionary-removing-and-adding-items-and-their-order/</link>
          <guid>https://mattei.io/about-a-dictionary-removing-and-adding-items-and-their-order/</guid>
          <description xml:base="https://mattei.io/about-a-dictionary-removing-and-adding-items-and-their-order/">&lt;p&gt;I had a weird problem today using a &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;xfhwa508(v=vs.110).aspx&quot;&gt;Dictionary&lt;&#x2F;a&gt;. The process involved removing and adding data, and then printing the data. I assumed that it was ordered. I was wrong! Let me show you:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;var&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; dictionary&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; = new&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Dictionary&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; string&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;dictionary.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Add&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;The&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;dictionary.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Add&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;quick&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;dictionary.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Add&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;31&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;brown&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;dictionary.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Add&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;145&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;fox&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;dictionary.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Remove&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt; &#x2F;&#x2F; remove the &amp;quot;quick&amp;quot; entry&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;After a while I added another line to the dictionary:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;dictionary.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Add&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;423&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;jumps&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;While printing this data I discovered an oddity.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;dictionary&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;ToList&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;ForEach&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;e&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; Console.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;WriteLine&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;quot;{0} =&amp;gt; {1}&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;, e.Key, e.Value));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What do you expect the output of this to be?&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; The&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;31&lt;&#x2F;span&gt;&lt;span&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; brown&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;145&lt;&#x2F;span&gt;&lt;span&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; fox&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;423&lt;&#x2F;span&gt;&lt;span&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; jumps&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However the actual result was this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; The&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;423&lt;&#x2F;span&gt;&lt;span&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; jumps&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;31&lt;&#x2F;span&gt;&lt;span&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; brown&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;145&lt;&#x2F;span&gt;&lt;span&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; fox&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;xfhwa508(v=vs.110).aspx&quot;&gt;documentation&lt;&#x2F;a&gt; tells us the following:&lt;&#x2F;p&gt;
&lt;blockquote&gt;For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair&lt;TKey, TValue&gt; structure representing a value and its key. The order in which the items are returned is undefined.&lt;&#x2F;blockquote&gt;
&lt;p&gt;Interested in the actual behavior I looked at the source code of Dictionary &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;referencesource-beta.microsoft.com&#x2F;#mscorlib&#x2F;system&#x2F;collections&#x2F;generic&#x2F;dictionary.cs#d3599058f8d79be0&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If you look closely, first at &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;referencesource-beta.microsoft.com&#x2F;#mscorlib&#x2F;system&#x2F;collections&#x2F;generic&#x2F;dictionary.cs#a6db5ffdec557169&quot;&gt;&lt;code&gt;Remove&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; and then to &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;referencesource-beta.microsoft.com&#x2F;#mscorlib&#x2F;system&#x2F;collections&#x2F;generic&#x2F;dictionary.cs#a7861da7aaa500fe&quot;&gt;&lt;code&gt;Add&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; (and subsequently &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;referencesource-beta.microsoft.com&#x2F;#mscorlib&#x2F;system&#x2F;collections&#x2F;generic&#x2F;dictionary.cs#fd1acf96113fbda9&quot;&gt;&lt;code&gt;Insert&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;) you can see that when you remove an item it holds a reference (in &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;referencesource-beta.microsoft.com&#x2F;#mscorlib&#x2F;system&#x2F;collections&#x2F;generic&#x2F;dictionary.cs#998e5f475d87f454&quot;&gt;&lt;code&gt;freelist&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;) to the free &#x27;entry&#x27;.&lt;&#x2F;p&gt;
&lt;p&gt;What&#x27;s more weird is the behavior when you delete 2 entries, and then add 2 others:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;var&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; dictionary&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; = new&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Dictionary&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; string&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;dictionary.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Add&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;The&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;dictionary.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Add&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;quick&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;dictionary.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Add&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;31&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;brown&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;dictionary.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Add&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;145&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;fox&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;dictionary.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Remove&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt; &#x2F;&#x2F; remove the &amp;quot;quick&amp;quot; entry&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;dictionary.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Remove&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;31&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt; &#x2F;&#x2F; also remove the &amp;quot;brown&amp;quot; entry&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;dictionary.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Add&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;423&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;jumps&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;dictionary.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Add&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;534&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;quot;high&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;dictionary&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;ToList&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;ForEach&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;e&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; Console.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;WriteLine&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt;&amp;quot;{0} =&amp;gt; {1}&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;, e.Key, e.Value));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which yields:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; The&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;534&lt;&#x2F;span&gt;&lt;span&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; high&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;423&lt;&#x2F;span&gt;&lt;span&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; jumps&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;145&lt;&#x2F;span&gt;&lt;span&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; fox&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But for that you&#x27;ll need to look at &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;referencesource-beta.microsoft.com&#x2F;#mscorlib&#x2F;system&#x2F;collections&#x2F;generic&#x2F;dictionary.cs#340&quot;&gt;line 340&lt;&#x2F;a&gt; and further!&lt;&#x2F;p&gt;
&lt;p&gt;So what have we learned? It&#x27;s not ordered until MSDN tells you!&lt;&#x2F;p&gt;
&lt;p&gt;Have a good one!&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Default values and overloads are not the same!</title>
          <pubDate>Mon, 13 Jan 2014 20:39:02 +0000</pubDate>
          <author>Kristof</author>
          <link>https://mattei.io/default-values-and-overloads-are-not-the-same/</link>
          <guid>https://mattei.io/default-values-and-overloads-are-not-the-same/</guid>
          <description xml:base="https://mattei.io/default-values-and-overloads-are-not-the-same/">&lt;p&gt;Consider the following class of the Awesome(r) library, using default parameters.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-storage z-type&quot;&gt;public class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Foo&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-keyword&quot;&gt;    public void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; DoCall&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;int&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; timeout&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 10&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;        &#x2F;* awesome implementation goes here *&#x2F;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You get the dll and that class &amp;amp; function in your code, like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;Foo foo&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; = new&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Foo&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;foo.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;DoCall&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Can&#x27;t get much easier than this right?&lt;&#x2F;p&gt;
&lt;p&gt;Then the Awesome(r) library gets updated:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-storage z-type&quot;&gt;public class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Foo&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-keyword&quot;&gt;    public void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; DoCall&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;int&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; timeout&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 20&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;        &#x2F;* awesome implementation goes here *&#x2F;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Notice that the default value has changed. You assume that when you just overwrite the dll in production, you will adopt the new behavior.&lt;&#x2F;p&gt;
&lt;p&gt;Nop. You need to recompile. Let me show you: the problem with default values is that the developer of Awesome(r) library is no longer in control of it.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s take a look at an excerpt of the IL where we create a new &lt;code&gt;Foo&lt;&#x2F;code&gt; and &lt;strong&gt;call&lt;&#x2F;strong&gt; &lt;code&gt;DoCall&lt;&#x2F;code&gt; without specifying &lt;code&gt;timeout&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;IL_0000&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;:  newobj     instance&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; void&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; AwesomeLibrary.Foo&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;ctor&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;IL_0005&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;:  stloc.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;IL_0006&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;:  ldloc.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;IL_0007&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;:  ldc.i4.s&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;   10&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;IL_0009&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;:  callvirt   instance&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; void&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; AwesomeLibrary.Foo&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;::&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;DoCall&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;(int32)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;IL_000e&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;:  ret&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is a release build.&lt;&#x2F;p&gt;
&lt;p&gt;Notice how on line 4 value 10 gets pushed on the the stack, and the next line calls the &lt;code&gt;DoCall&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This is a big danger in public APIs, and this is why the developer of Awesome(r) library should have used an overload instead of a default parameter:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-storage z-type&quot;&gt;public class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Foo&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-keyword&quot;&gt;    public void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; DoCall&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;        this&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;DoCall&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;20&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-keyword&quot;&gt;    public void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; DoCall&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;int&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; timeout&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;        &#x2F;* awesome implementation goes here *&#x2F;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This ensures that when a new version of Awesome(r) library is released AND that if that release is backwards API compatible, it can just be dropped in, without you having to recompile your whole codebase (but you should still test it :P )&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Make sure unattended.xml is not encrypted!</title>
          <pubDate>Thu, 12 Sep 2013 16:16:20 +0000</pubDate>
          <author>Kristof</author>
          <link>https://mattei.io/make-sure-unattended-xml-is-not-encrypted/</link>
          <guid>https://mattei.io/make-sure-unattended-xml-is-not-encrypted/</guid>
          <description xml:base="https://mattei.io/make-sure-unattended-xml-is-not-encrypted/">&lt;p&gt;I was playing around with Sysprep using &lt;code&gt;unattended.xml&lt;&#x2F;code&gt; when I hit a weird issue with VirtualBox and Encrypted folders on the host.&lt;&#x2F;p&gt;
&lt;p&gt;Setup:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;unattended.xml&lt;&#x2F;code&gt; on the host, encrypted (with Windows EFS).&lt;&#x2F;li&gt;
&lt;li&gt;Virtual Machine, hosted in VirtualBox&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;I mounted the folder with &lt;code&gt;unattended.xml&lt;&#x2F;code&gt; (and other files) inside the VirtualBox and started sysprep (&lt;code&gt;sysprep+shutdown.cmd&lt;&#x2F;code&gt; just executes the sysprep with the &lt;code&gt;unattended.xml&lt;&#x2F;code&gt; from the location and copies a &lt;code&gt;SetupComplete.cmd&lt;&#x2F;code&gt; to &lt;code&gt;c:\Windows\Scripts&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;

&lt;figure&gt;
    &lt;img
    src=&quot;&amp;#x2F;make-sure-unattended-xml-is-not-encrypted&amp;#x2F;&#x2F;Windows-Setup-encountered.png&quot;
    &#x2F;&gt;
    &lt;figcaption&gt;Windows Setup encountered an internal error while loading or searching for an unattended answer file.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;

&lt;p&gt;When booting the VM I got the following error:&lt;&#x2F;p&gt;

&lt;img
    src=&quot;&amp;#x2F;make-sure-unattended-xml-is-not-encrypted&amp;#x2F;&#x2F;sysprep-files.png&quot;
    &#x2F;&gt;

&lt;p&gt;To investigate the error I hit up Shift+F10 and checked &lt;code&gt;c:\Windows\Panther\setuperr.log&lt;&#x2F;code&gt;, which had the following error:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;[setup.exe] UnattendSearchExplicitPath: Found unattend file at [C:\Windows\Panther\unattend.xml] but unable to deserialize it; status = 0x80070005, hrResult = 0x0.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Googling for the error string didn&#x27;t help. Googling for the error code did help. It meant &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;support.microsoft.com&#x2F;kb&#x2F;816731&quot;&gt;Access Denied&lt;&#x2F;a&gt;. Now what could it be. I had a suspicion that it was the encryption. Let&#x27;s find out:&lt;&#x2F;p&gt;
&lt;p&gt;By using the command&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cipher &#x2F;s:c:\Windows\Panther&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I saw this:&lt;&#x2F;p&gt;

&lt;img
    src=&quot;&amp;#x2F;make-sure-unattended-xml-is-not-encrypted&amp;#x2F;&#x2F;cipher.png&quot;
    &#x2F;&gt;

&lt;p&gt;Notice the &lt;code&gt;E&lt;&#x2F;code&gt;, which means, Encrypted.&lt;&#x2F;p&gt;
&lt;p&gt;Executing&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;notepad c:\Windows\Panther\unattended.xml&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;confirmed my suspicion:&lt;&#x2F;p&gt;

&lt;img
    src=&quot;&amp;#x2F;make-sure-unattended-xml-is-not-encrypted&amp;#x2F;&#x2F;access-denied.png&quot;
    &#x2F;&gt;

&lt;p&gt;After removing the file with a Windows disk BEFORE the first boot (afterwards it doesn&#x27;t work it seems) the boot went fine, I sysprepped it again (with a non-encrypted unattended.xml) and all went fine.&lt;&#x2F;p&gt;
&lt;p&gt;So make sure you don&#x27;t copy &lt;code&gt;unattended.xml&lt;&#x2F;code&gt; to a machine that is encrypted. The your personal encryption keys don&#x27;t transfer to the new system.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>C# 5.0 spec is available online now!</title>
          <pubDate>Sat, 15 Jun 2013 16:25:51 +0000</pubDate>
          <author>Kristof</author>
          <link>https://mattei.io/c-5-0-spec-is-available-online-now/</link>
          <guid>https://mattei.io/c-5-0-spec-is-available-online-now/</guid>
          <description xml:base="https://mattei.io/c-5-0-spec-is-available-online-now/">&lt;p&gt;A few posts ago I blogged about &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;mattei.io&#x2F;2013&#x2F;04&#x2F;26&#x2F;foreach-now-captures-variables-access-to-modified-closure&#x2F;&quot;&gt;C# 5.0 and foreach&lt;&#x2F;a&gt;. I mentioned a part of the docs, but those weren&#x27;t online yet!&lt;&#x2F;p&gt;
&lt;p&gt;They are now!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.microsoft.com&#x2F;en-us&#x2F;download&#x2F;confirmation.aspx?id=7029&quot;&gt;http:&#x2F;&#x2F;www.microsoft.com&#x2F;en-us&#x2F;download&#x2F;confirmation.aspx?id=7029&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Enjoy &amp;amp; have a good one,&lt;&#x2F;p&gt;
&lt;p&gt;-Kristof&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>When using an enum in PowerShell, use the member&#x27;s name, not the member&#x27;s value</title>
          <pubDate>Tue, 23 Apr 2013 12:57:32 +0000</pubDate>
          <author>Kristof</author>
          <link>https://mattei.io/when-using-an-enum-in-powershell-use-the-members-name-not-the-members-value/</link>
          <guid>https://mattei.io/when-using-an-enum-in-powershell-use-the-members-name-not-the-members-value/</guid>
          <description xml:base="https://mattei.io/when-using-an-enum-in-powershell-use-the-members-name-not-the-members-value/">&lt;p&gt;Consider the following enum in C#:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;enum&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; State&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Started&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Stopped&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Unknown&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that I have not added an explicit value for the enum members. They will be generated by the compiler. As stated in the C# spec:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;... its associated value is set implicitly, as follows:&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;If the enum member is the first enum member declared in the enum type, its associated value is zero.&lt;&#x2F;li&gt;
&lt;li&gt;Otherwise, the associated value of the enum member is obtained by increasing the associated value of the textually preceding enum member by one. This increased value must be within the range of values that can be represented by the underlying type, otherwise a compile-time error occurs.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Found at &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.microsoft.com&#x2F;en-us&#x2F;download&#x2F;details.aspx?id=7029&quot;&gt;http:&#x2F;&#x2F;www.microsoft.com&#x2F;en-us&#x2F;download&#x2F;details.aspx?id=7029&lt;&#x2F;a&gt;, page 400-401 (I can&#x27;t find the version for 4.5 though...).&lt;&#x2F;p&gt;
&lt;p&gt;Now what are the consequences of this? Consider the following piece of PowerShell:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;powershell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;$result&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; $serviceController.GetServiceStatus()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;if&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;($result&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; -eq&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    MyLib.StartService()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will work, because PowerShell implicitly converts the int to the actual enum member.&lt;&#x2F;p&gt;
&lt;p&gt;However since we are assuming the value can go wrong. In the next version you add extra values, say for example to represent a starting&#x2F;stopping service:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;enum&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; State&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Starting&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Started&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Stopping&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Stopped&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Unknown&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since now all the values are shifted when you run your PowerShell again you start the service when it&#x27;s already started ;) .&lt;&#x2F;p&gt;
&lt;p&gt;Solution?&lt;&#x2F;p&gt;
&lt;p&gt;First of all (as a consumer), use the enum&#x27;s member name instead of its value:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;powershell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;$result&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt; $serviceController.GetServiceStatus()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;if&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other&quot;&gt;($result&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; -eq&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;MyLib.State&lt;&#x2F;span&gt;&lt;span&gt;]::Stopped)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    MyLib.StartService()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will ensure that you get the value for Started, not for anything else.&lt;&#x2F;p&gt;
&lt;p&gt;As a developer of a library you should ensure that you never mess up the order of an enum, by adding new values as last, or (prefered) set the value yourself:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;enum&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; State&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Started&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Stopped&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Unknown&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Becomes:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;enum&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; State&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Starting&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Started&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Stopping&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Stopped&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Unknown&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And now you can also perfectly reorder them so the numbers are sequential:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;enum&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; State&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Started&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Stopped&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Unknown&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Starting&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;    Stopping&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;</description>
      </item>
    </channel>
</rss>
