
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
 <channel>
   <title>posts on m0sa.net</title>
   <link>https://m0sa.net/posts/</link>
   <description>Recent content in posts on m0sa.net</description>
   <generator>Hugo -- gohugo.io</generator>
   <copyright>Copyright &amp;copy; 2022 - m0sa.net</copyright>
   
       <atom:link href="https://m0sa.net/posts/index.xml" rel="self" type="application/rss+xml" />
   
   
     <item>
       <title>Roslyn Analyzer Performance</title>
       <link>https://m0sa.net/posts/2019-08-roslyn-analyzer-runtime-performance/</link>
       <pubDate>Fri, 02 Aug 2019 00:00:00 +0000</pubDate>
       
       <guid>https://m0sa.net/posts/2019-08-roslyn-analyzer-runtime-performance/</guid>
       <description>&lt;h1 id=&#34;tldr&#34;&gt;TL;DR&lt;/h1&gt;&lt;p&gt;Do you write Roslyn analyzers? Ever wondered what impact they have on your compilation times? Here&amp;rsquo;s how you can get the stats!&lt;/p&gt;&lt;p&gt;To get the numbers one can use the &lt;a href=&#34;https://github.com/microsoft/msbuild/blob/vs16.0/src/Tasks/Microsoft.CSharp.CurrentVersion.targets#L286&#34;&gt;&lt;code&gt;ReportAnalyzer&lt;/code&gt;&lt;/a&gt; MSBuild property, that gets passed in to the C# / VB compilation tasks.It&amp;rsquo;s the MSBuild equivalent of the &lt;a href=&#34;https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/listed-alphabetically&#34;&gt;&lt;code&gt;-reportanalyzer&lt;/code&gt;&lt;/a&gt; C# compiler command line switch.&lt;/p&gt;&lt;p&gt;I usually use it in combination with the &lt;a href=&#34;https://github.com/Microsoft/msbuild/blob/master/documentation/wiki/Binary-Log.md&#34;&gt;&lt;code&gt;-bl&lt;/code&gt; MSBuild switch&lt;/a&gt;, so I can inspect the output with the the excellent &lt;a href=&#34;http://www.msbuildlog.com/&#34;&gt;MSBuild Binary and Structured Log Viewer&lt;/a&gt;.&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;msbuild Your/Project.csproj /p:ReportAnalyzer&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;true /bl:out.binlog&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;After the build is done, you can find the relevant bits right after the &lt;code&gt;Csc&lt;/code&gt; task&lt;/p&gt;&lt;p&gt;&lt;img src=&#34;https://i.stack.imgur.com/TN04A.png&#34; alt=&#34;roslyn analyzer performance numbers&#34;&gt;&lt;/p&gt;&lt;hr&gt;&lt;h1 id=&#34;backstory&#34;&gt;Backstory&lt;/h1&gt;&lt;p&gt;I was looking into increased build times (see image above) after introducing some new analyzers, and I couldn&amp;rsquo;t find a good reference for this kind of investigation.&lt;/p&gt;&lt;p&gt;This is another &lt;a href=&#34;https://stackoverflow.com/&#34;&gt;Stack Overflow&lt;/a&gt; .NET Core migration war story.&lt;/p&gt;&lt;p&gt;As we&amp;rsquo;re working on porting Stack Overflow to .NET Core, there will be a temporary phase, where some of the applications in our codebase will run on ASP.NET MVC, while others will already be ported to ASP.NET Core (see my previous &lt;a href=&#34;https://m0sa.net/posts/2019-02-msbuild-global-properties-defineconstants/&#34;&gt;blog post&lt;/a&gt;).For our domain logic and data models this means that they will have to be compiled against both.The most simple way to achieve this is if they don&amp;rsquo;t depend on ASP.NET at all.We&amp;rsquo;ve worked hard on this decoupling but we were still left with the occasional IHtmlString reference, etc.Some ASP.NET references had to stay, but we didn&amp;rsquo;t want them to grow unwieldy.So we created Roslyn analyzers that check for ASP.NET usage, and fail the build if they find an usage that is not explicitly opted in (e.g. by using &lt;a href=&#34;https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-pragma-warning&#34;&gt;&lt;code&gt;#pragma warning disable ASPNETUSAGE&lt;/code&gt;&lt;/a&gt; or via &lt;a href=&#34;https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.suppressmessageattribute&#34;&gt;&lt;code&gt;SuppressMessageAttribute&lt;/code&gt;&lt;/a&gt;).Developers that do feature work get a nice error at build time, and a squiggly in their editor, whenever they accidentally introduce a new ASP.NET reference into the shared library.This prevents them from stepping on the toes of the developers working on the port.&lt;/p&gt;</description>
     </item>
   
     <item>
       <title>Learning about MSBuild Global Properties - The Hard Way</title>
       <link>https://m0sa.net/posts/2019-02-msbuild-global-properties-defineconstants/</link>
       <pubDate>Mon, 25 Feb 2019 00:00:00 +0000</pubDate>
       
       <guid>https://m0sa.net/posts/2019-02-msbuild-global-properties-defineconstants/</guid>
       <description>&lt;p&gt;One of the first steps in the Stack Overflow .NET Core migration was to extract a Common library that would target &lt;code&gt;netstandard&lt;/code&gt;.This new project would be referenced in both the old ASP.NET MVC 5 as well as the new ASP.NET Core projects, so that we could start moving the old projects one-by-one.We ended up targeting &lt;code&gt;net462&lt;/code&gt; for ASP.NET MVC, and &lt;code&gt;netcoreapp2.2&lt;/code&gt; for ASP.NET CORE.The new Common project had a few ASP.NET MVC / ASP.NET Core specific regions that were conditionally compiled via &lt;code&gt;#if NET462 &amp;lt;MVC 5 CODE&amp;gt; #else &amp;lt;ASPNETCORE CODE&amp;gt;&lt;/code&gt; directives (thanks to the new &lt;code&gt;IHtmlContent&lt;/code&gt; interface which replaced the old &lt;code&gt;IHtmlString&lt;/code&gt; from &lt;code&gt;System.Web&lt;/code&gt;, which we use across many models).The target framework moniker conditional defines are a feature of the new SDK-style builds in the .NET Core ecosystem.(Note, that switching on the TFM works for &lt;em&gt;us&lt;/em&gt; because we don&amp;rsquo;t plan on running ASP.NET CORE on the full framework - it might not work for you&amp;hellip;)&lt;/p&gt;&lt;p&gt;Everything built fine and dandy locally, but the fun started once we promoted our branch to a PR and the automatic build checks we had in place in GitHub / TeamCity kicked.No matter what we did, we only ever got into one branch of the conditional compilation; the other one was seemingly disregarded.My team spent around &lt;em&gt;2 days&lt;/em&gt; throwing WTFs around, and trying to figure out what was going on.Ultimately we started digging into the MSBuild code that powers the multi-targeting builds.&lt;/p&gt;&lt;p&gt;The magic code that introduces the target framework defines lives &lt;a href=&#34;https://github.com/dotnet/sdk/blob/700964f851905dd55c75d1869129e335fd9d1e91/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.CSharp.targets#L34&#34;&gt;here&lt;/a&gt;, and at first glance everything seemed to be in order, and nobody from our team spotted what&amp;rsquo;s going on.So, I started comparing what our CI build does differently from our local build (which worked fine).Finally I managed to repro what&amp;rsquo;s going on, by including an additional &lt;code&gt;-p:DefineContants=WHATEVER&lt;/code&gt; on the &lt;code&gt;msbuild&lt;/code&gt; command-line, locally.Our CI PR verification build was passing a conditional compilation symbol in order to speed up the build a bit (we have one that enables us to localize for English only, which means a lot less IO)Next, I compared the MSBuild binlog (you can get one by running MSBuild with the &lt;a href=&#34;https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-command-line-reference&#34;&gt;&lt;code&gt;-bl&lt;/code&gt; switch&lt;/a&gt;), and used the excellent &lt;a href=&#34;http://www.msbuildlog.com/&#34;&gt;MSBuild Binary and Structured Log Viewer&lt;/a&gt; where I searched for &lt;code&gt;DefineConstants&lt;/code&gt;.The failing case turned out to short-circuit on whatever was passed in the command-line switch (e.g. &lt;code&gt;WHATEVER&lt;/code&gt;), and never re-evaluated the property.Lucky for me, the binlog contained a hint as to why this is happening; the DefineConstants property was under a &amp;ldquo;global properties&amp;rdquo; node in the tree.Once I googled for &amp;ldquo;MSBuild global properties&amp;rdquo; I got to &lt;a href=&#34;https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-properties#global-properties&#34;&gt;this piece of doc&lt;/a&gt; fairly quickly.&lt;/p&gt;&lt;p&gt;In the end the TIL was: if a property is a global property, and you pass it to MSBuild via command-line args, the value passed in is used, regardless of what your build scripts do.&lt;/p&gt;&lt;p&gt;I&amp;rsquo;ve came up with a trivial example to explain to the team, what was going on in MSBuild:&lt;/p&gt;&lt;p&gt;&lt;img src=&#34;https://i.stack.imgur.com/M9EYD.png&#34; alt=&#34;MSBuild Global Property Example&#34;&gt;&lt;/p&gt;&lt;p&gt;I&amp;rsquo;ve also opened up an &lt;a href=&#34;https://github.com/dotnet/sdk/issues/2854&#34;&gt;issue in the DotNet SDK&lt;/a&gt;, since using a global property for the multi-targeting part makes the whole thing very brittle, and I&amp;rsquo;d expect some defensive programming around that.&lt;/p&gt;</description>
     </item>
   
     <item>
       <title>Getting Stack Overflow Localization Tooling Ready for .NET Core</title>
       <link>https://m0sa.net/posts/2018-11-runtime-moonspeak/</link>
       <pubDate>Wed, 21 Nov 2018 00:00:00 +0000</pubDate>
       
       <guid>https://m0sa.net/posts/2018-11-runtime-moonspeak/</guid>
       <description>&lt;p&gt;By popular demand, this is my writeup of our latest localization tooling refactoring at &lt;a href=&#34;https://stackoverflow.com&#34;&gt;Stack Overflow&lt;/a&gt;&lt;/p&gt;&lt;blockquote class=&#34;twitter-tweet&#34;&gt;&lt;p lang=&#34;en&#34; dir=&#34;ltr&#34;&gt;We&amp;#39;re about to change all localization on Stack Overflow (and the entire network) to runtime instead of compile time. Let&amp;#39;s hope we did this right.&lt;/p&gt;&amp;mdash; Nick Craver (@Nick_Craver) &lt;a href=&#34;https://twitter.com/Nick_Craver/status/1064913213232427009?ref_src=twsrc%5Etfw&#34;&gt;November 20, 2018&lt;/a&gt;&lt;/blockquote&gt;&lt;script async src=&#34;https://platform.twitter.com/widgets.js&#34; charset=&#34;utf-8&#34;&gt;&lt;/script&gt;&lt;h1 id=&#34;history&#34;&gt;History&lt;/h1&gt;&lt;p&gt;Lets talk about localization in general a bit first, though, to give you some background on how we ended up where here.&lt;/p&gt;&lt;p&gt;It&amp;rsquo;s a story full of tears, tech debt, and regret.&lt;/p&gt;&lt;h2 id=&#34;1-careers--talent&#34;&gt;1. Careers / Talent&lt;/h2&gt;&lt;p&gt;Your usual of-the-mill localization library works by having some sort of indirection (e.g. a key lookup) to get a string to be displayed e.g:&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-csharp&#34; data-lang=&#34;csharp&#34;&gt;Translate(SomeIDForAString, parameters)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Such an approach already provides you with a place where all the original strings are stored.On the other hand, it&amp;rsquo;s much less friendly towards the person reading (or writing) the code.Ever since we started localizing products at Stack Overflow (which dates before my time), we&amp;rsquo;ve been using a different approach.One that is much more developer-friendly, and much easier to read.Let me give you an example:&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-csharp&#34; data-lang=&#34;csharp&#34;&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;_&lt;/span&gt;s(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Hello $name$&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; { name = GetCurrentUserName() })&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This means that all of the strings that we use are directly &lt;em&gt;in our code&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;As a consequence, though, we need tooling that knows how to extract all the string templates (so we know what to give to translators).Historically, this tooling has been using &lt;a href=&#34;https://github.com/dotnet/roslyn&#34;&gt;Roslyn&lt;/a&gt; (the C# compiler, written in C#), since before it&amp;rsquo;s RTM.Originally, the tooling only did what I&amp;rsquo;ve highlighted above;it extracted all string templates used to call our localization library, and verified that all the replacement tokens were passed in as arguments.&lt;/p&gt;&lt;p&gt;Since we use ASP.NET MVC and Razor, it also had to extract strings from there, and in order to do that we had to generate C# from the &lt;code&gt;.cshtml&lt;/code&gt; files.This turned out to also be the slowest step inside of &lt;code&gt;aspnet_compiler.exe&lt;/code&gt; while precompiling the views (it had to re-do the same work we already did before).&lt;/p&gt;&lt;p&gt;If you&amp;rsquo;re interested in more background, you can read &lt;a href=&#34;https://twitter.com/mjibson&#34;&gt;@mjibson&lt;/a&gt;&amp;rsquo;s Careers Localization series (&lt;a href=&#34;https://mattjibson.com/careers-localization/&#34;&gt;part 1&lt;/a&gt;, &lt;a href=&#34;https://mattjibson.com/careers-api/&#34;&gt;part 2&lt;/a&gt;, &lt;a href=&#34;https://mattjibson.com/careers-extraction/&#34;&gt;part 3&lt;/a&gt;)&lt;/p&gt;&lt;h2 id=&#34;2-stack-overflow&#34;&gt;2. Stack Overflow&lt;/h2&gt;&lt;p&gt;The real fun started when we had to localize Stack Overflow, where we care a bit more about perf, and we tried to avoid the long build times.&lt;/p&gt;&lt;p&gt;We figured out we can hook into Razor and affect what code it generates.This allowed us to attach attributes to the generated C# views.We only needed tooling to inspect attributes from all the classes from the precompiled &lt;code&gt;.dll&lt;/code&gt;.What it also allowed us to do, was to generate the localization code (a switch statement that only required the current locale at runtime, everything else was codegen-ed) directly in the C# produced by Razor.For a while, on Stack Overflow, we put everything that we wanted to have localized inside Razor / &lt;code&gt;.cshtml&lt;/code&gt; files.This is the original source of &amp;ldquo;precompile localization&amp;rdquo; idea.&lt;/p&gt;&lt;p&gt;But then, somewhere in the middle of localizing everything, we gave up.Turns out the perf hit of rendering a Razor view for every string you want to render, ever, was just to big.&lt;/p&gt;&lt;p&gt;&lt;img src=&#34;https://i.stack.imgur.com/GBKKS.png&#34; alt=&#34;Early benchmark screenshot&#34;&gt;&lt;/p&gt;&lt;p&gt;We also needed to localize &lt;code&gt;.cs&lt;/code&gt; files.Oh, and we also found a bunch of strings that needed new localization features, like multiple pluralization tokens, and the same string being used in different contexts that matter in some languages (e.g. &lt;a href=&#34;https://en.wikipedia.org/wiki/Declension&#34;&gt;Declension&lt;/a&gt;).&lt;/p&gt;&lt;p&gt;Enter Roslyn tooling, again, but with a twist!At that time, &lt;code&gt;csc.exe&lt;/code&gt; was still the default C# compiler, and &lt;code&gt;aspnet_compiler.exe&lt;/code&gt; was (and still is super slow).So we replaced &lt;em&gt;both&lt;/em&gt; of them, with the Roslyn-based &lt;a href=&#34;https://github.com/StackExchange/StackExchange.Precompilation&#34;&gt;StackExchange.Precompilation&lt;/a&gt;, and to top it of, we made it extensible so it could handle all of our localization needs (both extracting the strings, as well as precompiling localization in both &lt;code&gt;.cshtml&lt;/code&gt; and &lt;code&gt;.cs&lt;/code&gt; files).&lt;/p&gt;&lt;p&gt;You can read more about this whole project &lt;a href=&#34;https://stackoverflow.blog/2015/07/23/announcing-stackexchange-precompilation/&#34;&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;h1 id=&#34;net-core&#34;&gt;.NET Core&lt;/h1&gt;&lt;p&gt;We currently still run on ASP.NET MVC 5, on the full framework. Our ultimate goal is to be able to run on .NET Core. ASP.NET Core is the first stepping stone towards that.&lt;/p&gt;&lt;p&gt;As we were evaluating ASP.NET vNext (== 5+), we had always hoped to roll with the metaprogramming features that were available in the previews, either there, or what was worked on in &lt;a href=&#34;https://github.com/dotnet/roslyn/blob/master/docs/features/generators.md&#34;&gt;Roslyn generators&lt;/a&gt;.But the time to move to .NET Core and ASP.NET .NET Core is now, and unfortunately none of those features are around anymore&amp;hellip;yet.Luckily, though, .NET tooling introduced some high-impact perf improvements that make the CLI tools much faster (e.g. &lt;code&gt;dotnet build-servers&lt;/code&gt;).Additionally, ASP.NET Core Razor view pre-compilation is fast!&lt;/p&gt;&lt;p&gt;We evaluated the &lt;a href=&#34;https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.1&amp;amp;viewFallbackFrom=asp%E2%80%8C%E2%80%8Bnetcore-2.1&#34;&gt;new ASP.NET Core localization features&lt;/a&gt;, but they seemed very allocate-y (it allocates an array every time, things get boxed, etc), and it doesn&amp;rsquo;t support some of the syntax edge cases we have.Considering this, and the fact that we&amp;rsquo;d have to rewrite all of our precious &amp;ldquo;&lt;code&gt;_s&lt;/code&gt;&amp;amp;&lt;code&gt;_m&lt;/code&gt;&amp;rdquo; code, we made the decision to keep rolling with our own localization framework.So we had to future-proof our tooling, while still maintaining backwards compatibility with our old MVC stack.&lt;/p&gt;&lt;h2 id=&#34;present&#34;&gt;Present&lt;/h2&gt;&lt;p&gt;Our localization needs are still the same:&lt;/p&gt;&lt;h3 id=&#34;1-no-performance-regressions&#34;&gt;1. No performance regressions&lt;/h3&gt;&lt;p&gt;Our code-gen wasn&amp;rsquo;t perfect.It relied on the &lt;a href=&#34;https://github.com/dotnet/roslyn/wiki/Getting-Started-C%23-Syntax-Analysis&#34;&gt;Roslyn &lt;code&gt;SyntaxTree&lt;/code&gt;&lt;/a&gt; only, we didn&amp;rsquo;t use the semantic model much.A lot of the generated code was not optimal (e.g. we passed around all of the template properties as &lt;code&gt;object&lt;/code&gt;s), but calling it was still way faster that having the entire Razor pipeline on the stack.This gave us some wiggle room for writing an optimized runtime implementation.And we managed to make the runtime implementation as fast as the old (albeit not very optimized) compile-time code-gen-ed localization.Kudos to &lt;a href=&#34;https://twitter.com/marcgravell&#34;&gt;@marcgravell&lt;/a&gt;, &lt;a href=&#34;https://twitter.com/Nick_Craver&#34;&gt;@Nick_Craver&lt;/a&gt; for helping out here. &lt;a href=&#34;https://github.com/dotnet/BenchmarkDotNet&#34;&gt;BenchMarkDotNet&lt;/a&gt; was also insanely helpful.&lt;/p&gt;&lt;blockquote class=&#34;twitter-tweet&#34;&gt;&lt;p lang=&#34;en&#34; dir=&#34;ltr&#34;&gt;We&amp;#39;re currently testing and optimizing our runtime localization approach on Stack Overflow (getting out of compile-time if we can). Luckily, sometimes the performance winners are very, very clear: &lt;a href=&#34;https://t.co/jDNJacTzcl&#34;&gt;pic.twitter.com/jDNJacTzcl&lt;/a&gt;&lt;/p&gt;&amp;mdash; Nick Craver (@Nick_Craver) &lt;a href=&#34;https://twitter.com/Nick_Craver/status/1062440789014429697?ref_src=twsrc%5Etfw&#34;&gt;November 13, 2018&lt;/a&gt;&lt;/blockquote&gt;&lt;script async src=&#34;https://platform.twitter.com/widgets.js&#34; charset=&#34;utf-8&#34;&gt;&lt;/script&gt;&lt;h3 id=&#34;2-compile-time-verification-of-string-templates--parameters&#34;&gt;2. Compile-time verification of string templates / parameters&lt;/h3&gt;&lt;p&gt;We&amp;rsquo;ve already written Roslyn analyzers that did this.Running the whole compilation with ASP.NET MVC 5 took ~2 minutes on a beefy machine this was really a show stopper for developers. We wanted to give them instant-feedback inside VS, with squiglies and all - analyzers provide that for us.&lt;/p&gt;&lt;h3 id=&#34;3-tooling-to-extract-strings-but-aspnetcore-compatible&#34;&gt;3. Tooling to extract strings, but AspNetCore compatible&lt;/h3&gt;&lt;p&gt;The only new requirement here is that everything it has to work with the &lt;a href=&#34;https://github.com/dotnet/cli/&#34;&gt;.NET Core CLI&lt;/a&gt;Turns out the analyzer mentioned above already has all the information we need!AspNetCore already pre-compiles &lt;code&gt;.cshtml&lt;/code&gt; -&amp;gt; &lt;code&gt;.cs&lt;/code&gt; as part of the build, those &lt;code&gt;.cs&lt;/code&gt; files can be analyzer-ed.&lt;/p&gt;&lt;p&gt;The main part of the work there was to port all our builds and build tooling to something that can potentially run on .NET Core CLI in the future.The best candidate for that seemed to be an &lt;a href=&#34;https://docs.microsoft.com/en-us/visualstudio/msbuild/build-loggers?view=vs-2017&#34;&gt;MSBuild &lt;code&gt;ILogger&lt;/code&gt; implementation&lt;/a&gt;, which can intercept the diagnostic messages, pin-pointing them to specific locations within a source file, and spit out the artifacts we can send out for translation in the end.This is super application and build configuration specific, so this implementation lives in our Stack Overflow solution.&lt;/p&gt;&lt;p&gt;I&amp;rsquo;ve run into an interesting edge case with &lt;a href=&#34;https://github.com/dotnet/roslyn/issues/30637&#34;&gt;Roslyn vs MSBuild diagnostic levels&lt;/a&gt; while doing this.&lt;/p&gt;&lt;p&gt;I&amp;rsquo;ve skipped on one other important detail here, we have a separate tool that rewrites JavaScript, we just made it generate the same diagnostic messages as the C# tools.This allows us to run both that tool and the main build step in parallel, and get a unified view dump of all used strings in the end.&lt;/p&gt;&lt;h3 id=&#34;other-tidbits&#34;&gt;Other tidbits&lt;/h3&gt;&lt;p&gt;Since we care about performance and a simple deployment model, we still don&amp;rsquo;t want to hit the DB on application spin-up.We were still able to get code-gen all of our existing translations / resources into the .dll into as part of the build.We did that by exposing a &lt;code&gt;partial class&lt;/code&gt; in code, and code-gen it&amp;rsquo;s counterpart during build by executing a &lt;code&gt;.csx&lt;/code&gt; file via &lt;a href=&#34;https://github.com/filipw/dotnet-script&#34;&gt;dotnet-script&lt;/a&gt;.I did try to write a native MSBuild Task to do the same, but the fact that we have to hit a DB or HTTP endpoint as part of that made it impossible.I found this &lt;a href=&#34;https://natemcmaster.com/blog/2017/11/11/msbuild-task-with-dependencies/&#34;&gt;blog post&lt;/a&gt; by &lt;a href=&#34;https://twitter.com/natemcmaster&#34;&gt;@natemcmaster&lt;/a&gt; very helpful while digging into that.&lt;/p&gt;&lt;h2 id=&#34;future&#34;&gt;Future&lt;/h2&gt;&lt;p&gt;With all of that in place, we now finally have a super-slim MoonSpeak library, that provides:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;template syntax processing&lt;ul&gt;&lt;li&gt;compile-time safe&lt;/li&gt;&lt;li&gt;design-time feedback&lt;/li&gt;&lt;li&gt;support multiple pluralization tokens per template (e.g.&lt;code&gt;@_s(&#39;User $userName$ has asked #numQuestions# questions and #numAnswers# answers&#39;, new { numQuestions = model.QuestionCount, numAnswers = model.AnswerCount })&lt;/code&gt;&lt;/li&gt;&lt;li&gt;built-in Markdown support&lt;code&gt;@_m(&#39;hello [world](https://example.org)&#39;)&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;a super-fast runtime base implementation which can work on both MVC5, and ASP.NET Core&lt;ul&gt;&lt;li&gt;the main issue here was to distinguish between the &lt;a href=&#34;https://docs.microsoft.com/en-us/dotnet/api/system.web.ihtmlstring&#34;&gt;&lt;code&gt;System.Web.IHtmlString&lt;/code&gt;&lt;/a&gt; (MVC5) and &lt;a href=&#34;https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.html.ihtmlcontent&#34;&gt;&lt;code&gt;Microsoft.AspNetCore.Html.IHtmlContent&lt;/code&gt;&lt;/a&gt; (ASP.NET Core) interfaces, and their implementations (we have separate packages for each of them) for backing the _m implementation&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;C# string extraction via diagnostics&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;All the other tooling that generates the translator resources, and provide translated strings to the implementation at runtime, are up to the consuming app.Even internally, we have different ways of doing that (Talent has a different workflow and intermediate formats there vs. Q&amp;amp;A).&lt;/p&gt;&lt;p&gt;We might be finally getting to a point where we can open source MoonSpeak.&lt;/p&gt;</description>
     </item>
   
     <item>
       <title>So long blogger</title>
       <link>https://m0sa.net/posts/2018-07-so-blog-longger/</link>
       <pubDate>Thu, 05 Jul 2018 00:00:00 +0000</pubDate>
       
       <guid>https://m0sa.net/posts/2018-07-so-blog-longger/</guid>
       <description>&lt;p&gt;It&amp;rsquo;s been almost 10 years since my &lt;a href=&#34;http://blog.m0sa.net/2008/11/prva-objava.html&#34;&gt;first blog posts&lt;/a&gt;, and I&amp;rsquo;ve totally neglected blogging in the past year.I totally blame blogger for that.I really want to blog more, so I&amp;rsquo;ve decided to move to a simpler setup, that&amp;rsquo;ll allow me to blog more quickly, with better tools.Nowadays I use VS Code for as many things as possible, and I&amp;rsquo;d really like to use it for blogging to. So I&amp;rsquo;ve decided to use a static page generator, that works with markdown.&lt;/p&gt;&lt;ol&gt;&lt;li&gt;&lt;p&gt;Chosing a static page generator&lt;/p&gt;&lt;p&gt;In the past I&amp;rsquo;ve had experience with Jekyll, and I&amp;rsquo;ve also played around with various node-based tools, which are &lt;em&gt;not very nice&lt;/em&gt; (&lt;em&gt;cough&lt;/em&gt;node_modules&lt;em&gt;cough&lt;/em&gt;).Although I quite liked VuePress, it didn&amp;rsquo;t offer any nice blogging templates.In the end I decided to give hugo a try, and it&amp;rsquo;s was very simple, with a lot of nice themes (you wouldn&amp;rsquo;t like another designed-by-developer style, would you?).&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;Setting up HUGO and a theme&lt;/p&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;$&amp;gt; chocolatey install hugo -y$&amp;gt; hugo new site m0sa.github.io$&amp;gt; cd blog$/m0sa.github.io&amp;gt; git init$/m0sa.github.io&amp;gt; cd themes$/m0sa.github.io/themes&amp;gt; git submodule add https://github.com/avianto/hugo-kiera&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Luckily it&amp;rsquo;s pretty easy to override stuff in the themes. The one I&amp;rsquo;ve picked, doesn&amp;rsquo;t have a rss &lt;code&gt;&amp;lt;link&amp;gt;&lt;/code&gt; tag, and the blog post titles into the &lt;code&gt;&amp;lt;title&amp;gt;&lt;/code&gt; in the header.So I&amp;rsquo;ve copied the &lt;code&gt;partials/header&lt;/code&gt; from the theme into my &lt;code&gt;layouts&lt;/code&gt; folder and tweaked it.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;Get markdown content of my blogger posts&lt;/p&gt;&lt;p&gt;Luckilly I didn&amp;rsquo;t blog all to much (which is hopefully going to change now that I have this new super duper setup), so I didn&amp;rsquo;t have to batch import anything.I&amp;rsquo;ve used ATS&amp;rsquo;s &lt;a href=&#34;https://automatethatshit.com/lab/html-to-markdown&#34;&gt;html-to-markdown&lt;/a&gt; tool. I added them on the cookie blacklist in order to get more than a single post converted w/o having to register.The bulk of the work is adding markdown front matter, and making sure all the code is in there, formatted correctly, and that everything looks OK.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;Set up hosting&lt;/p&gt;&lt;p&gt;Initially I wanted to go with github pages, but I went with netlify instead. It has all the bells and whistles (can force https + hsts with an auto generated let&amp;rsquo;s encrypt cert, CDN), and is more flexible later on (can easily add formst, AWS functions, etc), if I ever need it to be.&lt;/p&gt;&lt;p&gt;I followed the guide on the &lt;a href=&#34;https://gohugo.io/hosting-and-deployment/hosting-on-netlify/&#34;&gt;hugo homepage&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;Point redirect from old blogger URLs to the new ones (not done yet&amp;hellip;)&lt;/p&gt;&lt;p&gt;Injecting &lt;code&gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;window.location = &amp;quot;&amp;lt;NEW-URL&amp;gt;&amp;quot;;&amp;lt;/script&amp;gt;&lt;/code&gt; into single posts should do the trick, as &lt;a href=&#34;https://stackoverflow.com/a/20276484/155005&#34;&gt;suggested by Bjørn Bråthen on SO&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Since I only pulled &lt;em&gt;some&lt;/em&gt; (== non-crappy) blog posts over, I didn&amp;rsquo;t have to do this for everything.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;Profit?&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;</description>
     </item>
   
     <item>
       <title>Roslyn Adventures: Metaprogramming with StackExchange.Precompilation</title>
       <link>https://m0sa.net/posts/2016-01-roslyn-stackexchange-precompilation/</link>
       <pubDate>Sun, 24 Jan 2016 00:00:00 +0000</pubDate>
       
       <guid>https://m0sa.net/posts/2016-01-roslyn-stackexchange-precompilation/</guid>
       <description>&lt;p&gt;In this article we&amp;rsquo;ll wrap the[StringBuilderInterpolationOptimizer](&lt;a href=&#34;https://gist.github.com/m0sa/f086bb0e9f62e05995c6#file-&#34;&gt;https://gist.github.com/m0sa/f086bb0e9f62e05995c6#file-&lt;/a&gt;stringbuilderinterpolationoptimizer-cs) from my &lt;a href=&#34;https://m0sa.net/posts/2015-10-roslyn-adventures-optimizing-stringbuilder-interpolation/&#34;&gt;previousarticle&lt;/a&gt; intoan[StackExchange.Precompiltion](&lt;a href=&#34;https://blog.stackoverflow.com/2015/07/announcing-&#34;&gt;https://blog.stackoverflow.com/2015/07/announcing-&lt;/a&gt;stackexchange-precompilation/) module, and use it to optimize an existing C#project.&lt;/p&gt;&lt;p&gt;First things first, we start off with an empty console project, to which weadd some sample &lt;code&gt;StringBuilder.Append&lt;/code&gt; calls, passing interpolated strings asparameters:&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://lh3.googleusercontent.com/-MEXierP8YzM/VqQb2iKFYKI/AAAAAAAAH6M/eM85aqsdVyo/s1600-h/image%25255B5%25255D.png&#34;&gt;&lt;img src=&#34;https://lh3.googleusercontent.com/-MmCCbMx5JL8/VqQb3c5rMdI/AAAAAAAAH6U/Bf97X1HhcJE/image_thumb%25255B3%25255D.png?imgmax=800&#34; alt=&#34;image&#34;&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Now, let&amp;rsquo;s add the&lt;a href=&#34;https://www.nuget.org/packages/StackExchange.Precompilation.Build&#34;&gt;StackExchange.Precompilation.Build&lt;/a&gt;package to our demo project, and make sure it still builds. This packagereplaces &lt;code&gt;csc.exe&lt;/code&gt; with a custom and hookable compiler, which is basically awrapper around the roslyn API. Unfortunately, roslyn doesn&amp;rsquo;t expose any hooksfor adding custom processing from the command line(&lt;a href=&#34;https://github.com/dotnet/roslyn/issues/3356&#34;&gt;yet&lt;/a&gt;). Luckily the API let&amp;rsquo;syou do all kinds of crazy stuff, it even comes with a parser for the &lt;code&gt;csc.exe&lt;/code&gt;command line arguments&amp;hellip;&lt;/p&gt;&lt;p&gt;After this package is successfully installed, you should see the precompilerexecutable being called instead of &lt;code&gt;csc.exe&lt;/code&gt; in the build output window:&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://lh3.googleusercontent.com/-cR1QmqlA4ok/VqQb5zoDQwI/AAAAAAAAH6c/vuEnV8Xh8pc/s1600-h/image%25255B10%25255D.png&#34;&gt;&lt;img src=&#34;https://lh3.googleusercontent.com/-MmGN28b_uQA/VqQb7YcPNpI/AAAAAAAAH6k/F_xINPY3pjY/image_thumb%25255B6%25255D.png?imgmax=800&#34; alt=&#34;image&#34;&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Next, we need to build the compilation module that can be loaded into thecompilation. So we create an empty class library, and install the&lt;a href=&#34;https://www.nuget.org/packages/StackExchange.Precompilation.Metaprogramming/&#34;&gt;StackExchange.Precompilation.Metaprogramming&lt;/a&gt;package (contains all the required base types).&lt;/p&gt;&lt;p&gt;After we&amp;rsquo;ve pulled in the &lt;code&gt;SyntaxTree&lt;/code&gt; rewriter from the last article, we canimplement the hook. The interface we need to implement is &lt;code&gt;ICompileModule&lt;/code&gt;. Wewant to implement the &lt;code&gt;BeforeCompilation&lt;/code&gt; method, which allows us to inspectand modify our compilation before any IL is emitted. Inside this method wehave full access to all syntax trees in the compilation and their semanticmodel. Note that &lt;code&gt;CSharpCompilation&lt;/code&gt; is immutable, so we have to replace theone in the compilation context with the modified one:&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://lh3.googleusercontent.com/-ukDU5Cz6lv4/VqQb79lcZHI/AAAAAAAAH6s/qMPl9UpS2_Y/s1600-h/image%25255B21%25255D.png&#34;&gt;&lt;img src=&#34;https://lh3.googleusercontent.com/-thmO6g0pfsA/VqQb88L6pZI/AAAAAAAAH60/C4RFJ_ulDtc/image_thumb%25255B13%25255D.png?imgmax=800&#34; alt=&#34;image&#34;&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;All that&amp;rsquo;s left to do now is to wire up our shiny new compile module, so it&amp;rsquo;spicked up as by the compiler. This is done in the app.config (or web.configfor ASP.NET project) file of the console application:&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://lh3.googleusercontent.com/-c0O9RpidPHA/VqQb94kYdQI/AAAAAAAAH68/xfsT_G3v2ts/s1600-h/image%25255B26%25255D.png&#34;&gt;&lt;img src=&#34;https://lh3.googleusercontent.com/-nfjOiGIXEW8/VqQb-j5W9qI/AAAAAAAAH7E/Lcdxf6VhVjY/image_thumb%25255B16%25255D.png?imgmax=800&#34; alt=&#34;image&#34;&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;The modified build script forwards the project&amp;rsquo;s config file to the compiler(this is especially useful when it needs to actually precompile razor views,but this is out of scope of this post). Also, be sure to add theprecompilation module as a project reference to the console application.&lt;/p&gt;&lt;p&gt;After everything is set up correctly, a quick build and decompilation canverify that the emitted assembly contains optimized string builder calls:&lt;/p&gt;&lt;p&gt;[&lt;img src=&#34;https://lh3.googleusercontent.com/-XZVP33Q7iG8/VqQcAMkUfjI/AAAAAAAAH7U/n7cfwoXJLUY/image_thumb%25255B20%25255D.png?imgmax=800&#34; alt=&#34;image&#34;&gt;](&lt;a href=&#34;https://lh3.googleusercontent.com/-zjm-&#34;&gt;https://lh3.googleusercontent.com/-zjm-&lt;/a&gt;pQfLE1s/VqQb_GwLFPI/AAAAAAAAH7I/Af-N2jpH5qk/s1600-h/image%25255B32%25255D.png)&lt;/p&gt;&lt;p&gt;The sources for this demo, along with commits reflecting the described steps,can be found on &lt;a href=&#34;https://github.com/m0sa/PrecompilationDemo&#34;&gt;GitHub&lt;/a&gt;.&lt;/p&gt;&lt;hr&gt;&lt;p&gt;P.S. a useful trick for debugging your compilation module is to add a&lt;code&gt;Debugger.Launch()&lt;/code&gt; call to attach your current VS instance to the currentlyexecuting compilation.&lt;/p&gt;</description>
     </item>
   
     <item>
       <title>Roslyn Adventures: Optimizing StringBuilder string interpolation</title>
       <link>https://m0sa.net/posts/2015-10-roslyn-adventures-optimizing-stringbuilder-interpolation/</link>
       <pubDate>Mon, 19 Oct 2015 00:00:00 +0000</pubDate>
       
       <guid>https://m0sa.net/posts/2015-10-roslyn-adventures-optimizing-stringbuilder-interpolation/</guid>
       <description>&lt;p&gt;C# string interpolation is awesome. But we can make it even more awesome bymaking it less wasteful. Consider this line of code:&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-csharp&#34; data-lang=&#34;csharp&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; StringBuilder().Append(&lt;span style=&#34;color:#e6db74&#34;&gt;$&amp;#34;foo {1}&amp;#34;&lt;/span&gt;);&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Currently Roslyn emits the following IL for this call:&lt;/p&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;IL_0001:  newobj      System.Text.StringBuilder..ctorIL_0006:  ldstr       &amp;quot;foo {0}&amp;quot;IL_000B:  ldc.i4.1IL_000C:  box         System.Int32IL_0011:  call        System.String.FormatIL_0016:  call        System.Text.StringBuilder.Append&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You see what it does there? Let&amp;rsquo;s translate that back to C#, to make it moreobvious:&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-csharp&#34; data-lang=&#34;csharp&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; StringBuilder().Append(&lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt;.Format(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;foo {0}&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;));&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It [allocates](&lt;a href=&#34;http://blog.marcgravell.com/2013/11/allocaction-allocation-&#34;&gt;http://blog.marcgravell.com/2013/11/allocaction-allocation-&lt;/a&gt;allocation.html) another string and possibly even&lt;a href=&#34;http://referencesource.microsoft.com/#mscorlib/system/string.cs,bdf91919a8d3537e&#34;&gt;another&lt;/a&gt;&lt;a href=&#34;http://referencesource.microsoft.com/#mscorlib/system/text/stringbuildercache.cs,9ce41f3defeef16f&#34;&gt;StringBuilder&lt;/a&gt;.The thing is, you wouldn&amp;rsquo;t be using a &lt;code&gt;StringBuilder&lt;/code&gt; if you weren&amp;rsquo;t concernedabout allocations. My initial idea of how to solve this was&lt;code&gt;FormattableString&lt;/code&gt;. So basically something like this:&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-csharp&#34; data-lang=&#34;csharp&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Foo&lt;/span&gt;{  &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt; Bar(FormattableString str)  {    Console.WriteLine(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;FORMAT: &amp;#34;&lt;/span&gt; + str.Format, str.GetArguments());  }  &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt; Bar(&lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt; str)  {    Console.WriteLine(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;STRING: &amp;#34;&lt;/span&gt; + str);  }  &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt; Main()  {    Bar(&lt;span style=&#34;color:#e6db74&#34;&gt;$&amp;#34;a test {42}&amp;#34;&lt;/span&gt;);  }}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Unfortunately overload resolution doesn&amp;rsquo;t work in favor of the methodaccepting the &lt;code&gt;FormattableString&lt;/code&gt; whenever there is an overload for the stringparameter. So the example above would write &lt;code&gt;STRING: a test 42&lt;/code&gt; to theconsole. If we could make the overload resolution smarter (e.g. make thecompiler create FormattableString instances wherever there&amp;rsquo;s an matchingoverload accepting FormattableString instead of a string argument) thesolution would be as easy as creating &lt;code&gt;Append/AppendLine(FormattableString)&lt;/code&gt;extension methods for &lt;code&gt;StringBuilder&lt;/code&gt;.&lt;/p&gt;&lt;h2 id=&#34;roslyn-to-the-rescue&#34;&gt;Roslyn to the rescue&lt;/h2&gt;&lt;p&gt;Luckily we can use Roslyn to do some metaprogramming magic and work aroundthis. Basically we need to rewrite &lt;code&gt;StringBuilder.Append/AppendLine&lt;/code&gt; calls to&lt;code&gt;StringBuilder.AppendFormat&lt;/code&gt;:&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-csharp&#34; data-lang=&#34;csharp&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; StringBuilder().AppendFormat(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;foo {0}&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;);&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&amp;hellip; in IL speak:&lt;/p&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;IL_0001:  newobj      System.Text.StringBuilder..ctorIL_0006:  ldstr       &amp;quot;foo {0}&amp;quot;IL_000B:  ldc.i4.1IL_000C:  box         System.Int32IL_0011:  call        System.Text.StringBuilder.AppendFormat&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Let&amp;rsquo;s create a reusable Roslyn-based solution that knows how to do thatoptimization.&lt;br&gt;We can use the class above to rewrite each &lt;code&gt;SyntaxTree&lt;/code&gt; in a&lt;code&gt;CSharpCompilation&lt;/code&gt;:&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-csharp&#34; data-lang=&#34;csharp&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;StringBuilderInterpolationOptimizer&lt;/span&gt; : CSharpSyntaxRewriter{  &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;readonly&lt;/span&gt; SemanticModel &lt;span style=&#34;color:#ae81ff&#34;&gt;_&lt;/span&gt;model;  &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; StringBuilderInterpolationOptimizer(SemanticModel model)  {    &lt;span style=&#34;color:#ae81ff&#34;&gt;_&lt;/span&gt;model = model;  }  &lt;span style=&#34;color:#75715e&#34;&gt;// we use the semantic model to get the type information of the method being called&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;bool&lt;/span&gt; CanRewriteSymbol(SymbolInfo symbolInfo, &lt;span style=&#34;color:#66d9ef&#34;&gt;out&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;bool&lt;/span&gt; appendNewLine)  {    appendNewLine = &lt;span style=&#34;color:#66d9ef&#34;&gt;false&lt;/span&gt;;    IMethodSymbol methodSymbol = symbolInfo.Symbol &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; IMethodSymbol;    &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (methodSymbol == &lt;span style=&#34;color:#66d9ef&#34;&gt;null&lt;/span&gt;) &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;false&lt;/span&gt;;    &lt;span style=&#34;color:#66d9ef&#34;&gt;switch&lt;/span&gt; (methodSymbol.Name)    {      &lt;span style=&#34;color:#66d9ef&#34;&gt;case&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;AppendLine&amp;#34;&lt;/span&gt;:      &lt;span style=&#34;color:#66d9ef&#34;&gt;case&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Append&amp;#34;&lt;/span&gt;:        &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (methodSymbol.ContainingType.ToString() == &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;System.Text.StringBuilder&amp;#34;&lt;/span&gt;)        {          appendNewLine = methodSymbol.Name == &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;AppendLine&amp;#34;&lt;/span&gt;;          &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;true&lt;/span&gt;;        }        &lt;span style=&#34;color:#66d9ef&#34;&gt;break&lt;/span&gt;;    }    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;false&lt;/span&gt;;  }  &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;override&lt;/span&gt; SyntaxNode VisitInvocationExpression(InvocationExpressionSyntax node)  {    &lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; memberAccess = node.Expression &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; MemberAccessExpressionSyntax;    &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (memberAccess != &lt;span style=&#34;color:#66d9ef&#34;&gt;null&lt;/span&gt; &amp;amp;&amp;amp; node.ArgumentList.Arguments.Count == &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;)    {      &lt;span style=&#34;color:#75715e&#34;&gt;// check if the single method argument is an interpolated string&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;      &lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; interpolatedStringSyntax = node.ArgumentList.Arguments[&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;].Expression &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; olatedStringExpressionSyntax;      &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (interpolatedStringSyntax != &lt;span style=&#34;color:#66d9ef&#34;&gt;null&lt;/span&gt;)      {        &lt;span style=&#34;color:#66d9ef&#34;&gt;bool&lt;/span&gt; appendNewLine; &lt;span style=&#34;color:#75715e&#34;&gt;// this distinguishes Append and AppendLine calls&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (CanRewriteSymbol(&lt;span style=&#34;color:#ae81ff&#34;&gt;_&lt;/span&gt;model.GetSymbolInfo(memberAccess), &lt;span style=&#34;color:#66d9ef&#34;&gt;out&lt;/span&gt; appendNewLine))        {          &lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; formatCount = &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;;          &lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; formatString = &lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; StringBuilder();          &lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; formatArgs = &lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; List&amp;lt;ArgumentSyntax&amp;gt;();          &lt;span style=&#34;color:#75715e&#34;&gt;// build the format string&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;          &lt;span style=&#34;color:#66d9ef&#34;&gt;foreach&lt;/span&gt; (&lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; content &lt;span style=&#34;color:#66d9ef&#34;&gt;in&lt;/span&gt; interpolatedStringSyntax.Contents)          {            &lt;span style=&#34;color:#66d9ef&#34;&gt;switch&lt;/span&gt; (content.Kind())            {              &lt;span style=&#34;color:#66d9ef&#34;&gt;case&lt;/span&gt; SyntaxKind.InterpolatedStringText:                &lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; text = (InterpolatedStringTextSyntax)content;                formatString.Append(text.TextToken.Text);                &lt;span style=&#34;color:#66d9ef&#34;&gt;break&lt;/span&gt;;              &lt;span style=&#34;color:#66d9ef&#34;&gt;case&lt;/span&gt; SyntaxKind.Interpolation:                &lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; interpolation = (InterpolationSyntax)content;                formatString.Append(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;{&amp;#34;&lt;/span&gt;);                formatString.Append(formatCount++);                formatString.Append(interpolation.AlignmentClause);                formatString.Append(interpolation.FormatClause);                formatString.Append(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;}&amp;#34;&lt;/span&gt;);                &lt;span style=&#34;color:#75715e&#34;&gt;// the interpolations become arguments for the AppendFormat call&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;                formatArgs.Add(SyntaxFactory.Argument(interpolation.Expression));                &lt;span style=&#34;color:#66d9ef&#34;&gt;break&lt;/span&gt;;            }          }          &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (appendNewLine)          {            formatString.AppendLine();          }          &lt;span style=&#34;color:#75715e&#34;&gt;// the first parameter is the format string&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;          formatArgs.Insert(&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;,            SyntaxFactory.Argument(              SyntaxFactory.LiteralExpression(                SyntaxKind.StringLiteralExpression,                SyntaxFactory.Literal(formatString.ToString()))));                &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; node                  .WithExpression(memberAccess.WithName(SyntaxFactory.IdentifierName(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;AppendFormat&amp;#34;&lt;/span&gt;)))                  .WithArgumentList(SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(formatParams)));        }      }    }    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;base&lt;/span&gt;.VisitInvocationExpression(node);  }}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Originally I wanted the optimization to do the same for&lt;code&gt;TextWriter.Write/WriteLine&lt;/code&gt; and &lt;code&gt;Console.Write/WriteLine&lt;/code&gt; calls, but it turns outthat they actually &lt;a href=&#34;http://referencesource.microsoft.com/#mscorlib/system/io/textwriter.cs,534&#34;&gt;call&lt;code&gt;string.Format&lt;/code&gt;&lt;/a&gt;internally anyway. So, add that to the list of possible optimizations.&lt;/p&gt;&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;&lt;p&gt;As you&amp;rsquo;ve seen by yourself, it&amp;rsquo;s not particulairly difficult to optimize theback and forth between string interpolation and &lt;code&gt;StringBuilder&lt;/code&gt;. I reallythink optimizations like that should be in Roslyn. As long as that&amp;rsquo;s notimplemented though, using string interpolation to build big string fragments(like for example HTML&amp;hellip;) might be a bit more expensive than you might think.&lt;/p&gt;&lt;p&gt;Stay tuned for my next blog post, where I&amp;rsquo;ll show you how to plug theoptimization into the metaprogramming infrastructure of DNX and/or[StackExchange.Precompilation](&lt;a href=&#34;https://blog.stackoverflow.com/2015/07/announcing-&#34;&gt;https://blog.stackoverflow.com/2015/07/announcing-&lt;/a&gt;stackexchange-precompilation/), if you&amp;rsquo;re not ready to migrate to vNext yet.&lt;/p&gt;</description>
     </item>
   
     <item>
       <title>Roslyn Adventures: Walk the #line</title>
       <link>https://m0sa.net/posts/2014-11-roslyn-walk-the-line/</link>
       <pubDate>Fri, 14 Nov 2014 00:00:00 +0000</pubDate>
       
       <guid>https://m0sa.net/posts/2014-11-roslyn-walk-the-line/</guid>
       <description>&lt;p&gt;With Roslyn, C# developers now have a powerful tool which makes modifying thesource code a breeze. At &lt;a href=&#34;http://stackexchange.com/&#34;&gt;Stack Exchange&lt;/a&gt; we&amp;rsquo;veinvented our own little set of extension to C# for localization purposes,previously described on &lt;a href=&#34;http://mattjibson.com/blog/2013/02/28/careers-localization-part-2-api/&#34;&gt;Matt Jibson&amp;rsquo;sblog&lt;/a&gt;.The project originally supported ASP.NET MVC views only, but we&amp;rsquo;ve expanded itto C# source files (.cs) because our projects have strings that simply don&amp;rsquo;tfit into MVC views and rendering a view for each string is overkill. In thisblog series I&amp;rsquo;ll try to highlight some of the fun things I&amp;rsquo;ve learned on this&lt;code&gt;.cshtml&lt;/code&gt; -&amp;gt;&lt;code&gt;.cs&lt;/code&gt; journey.&lt;/p&gt;&lt;h2 id=&#34;localization-adventures-walk-the-line&#34;&gt;Localization Adventures: Walk the &lt;code&gt;#line&lt;/code&gt;&lt;/h2&gt;&lt;p&gt;With code rewriting being the biggest problem, one can quickly forget tosupport other things in the development workflow, like &lt;strong&gt;debugging&lt;/strong&gt;. TakeASP.NET MVC&amp;rsquo;s &lt;code&gt;.cshtml&lt;/code&gt; views for example. Although they are compiled to C#source code (you get CodeDOM from Razor, ASP.NET vNext will change that) youcan still step through breakpoints in the &lt;code&gt;.cshtml&lt;/code&gt; file. In C# we have the&lt;code&gt;#line&lt;/code&gt; [preprocessor directive](&lt;a href=&#34;http://msdn.microsoft.com/en-&#34;&gt;http://msdn.microsoft.com/en-&lt;/a&gt;us/library/34dk387t.aspx) that can enables us to do this. As &lt;em&gt;trivial&lt;/em&gt; as itmight seem, it&amp;rsquo;s not so easy to get right. The fun part is having more thanone statement on a single line. Let&amp;rsquo;s say we have this simple program fragmentthat we &lt;a href=&#34;https://gist.github.com/m0sa/01a16f5dc056ce0e5c8d#file-program-original-cs&#34;&gt;need to rewrite&lt;/a&gt;:&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-csharp&#34; data-lang=&#34;csharp&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;namespace&lt;/span&gt; ConsoleApplication1{    &lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Program&lt;/span&gt;    {        &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt; Main(&lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt;[] args)        {            &lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt; foobar = &lt;span style=&#34;color:#ae81ff&#34;&gt;_&lt;/span&gt;s(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;$foo$ bar&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; { foo = Foo() }), baz = Baz();        }        &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt; Foo() { &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Foo&amp;#34;&lt;/span&gt;; }        &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt; Baz() { &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Baz&amp;#34;&lt;/span&gt;; }    }}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;… and the whole thing get&amp;rsquo;s &lt;a href=&#34;https://gist.github.com/m0sa/01a16f5dc056ce0e5c8d#file-program-rewritten-badly-cs&#34;&gt;(badly) rewritten into&lt;/a&gt;:&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-csharp&#34; data-lang=&#34;csharp&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;namespace&lt;/span&gt; ConsoleApplication1{    &lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Program&lt;/span&gt;    {        &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt; Main(&lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt;[] args)        {            &lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;_&lt;/span&gt;s_123 = &lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt;.Concat(Foo(), &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34; bar&amp;#34;&lt;/span&gt;);            &lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt; foobar = &lt;span style=&#34;color:#ae81ff&#34;&gt;_&lt;/span&gt;s_123, baz = Baz();        }        &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt; Foo() { &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Foo&amp;#34;&lt;/span&gt;; }        &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt; Baz() { &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Baz&amp;#34;&lt;/span&gt;; }    }}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The question is, how to place the &lt;code&gt;#line&lt;/code&gt; directives to hit a breakpoint onthe &lt;code&gt;baz = Baz()&lt;/code&gt; statement? A clue lies in the breakpoint itself, whichVisual Studio shows as &lt;code&gt;Program.Original.cs, line 7 character 67&lt;/code&gt;. As you cansee though, there is no way to specify &lt;code&gt;character 67&lt;/code&gt; using the &lt;code&gt;#line&lt;/code&gt;directive. To solve the problem we need to be aware of two simple facts the&lt;a href=&#34;http://msdn.microsoft.com/en-us/library/34dk387t.aspx&#34;&gt;MSDN page&lt;/a&gt; forgets tomention:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;the same line number can be used by multiple #line directives&lt;/li&gt;&lt;li&gt;whitespace is important when using the #line directive&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Which brings us to the&lt;a href=&#34;https://gist.github.com/m0sa/01a16f5dc056ce0e5c8d&#34;&gt;solution&lt;/a&gt;:&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-csharp&#34; data-lang=&#34;csharp&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#line 1 &amp;#34;Program.Original.cs&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;namespace&lt;/span&gt; ConsoleApplication1{    &lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Program&lt;/span&gt;    {        &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt; Main(&lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt;[] args)        {&lt;span style=&#34;color:#75715e&#34;&gt;#line hidden&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;            &lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;_&lt;/span&gt;s_123 = &lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt;.Concat(Foo(), &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34; bar&amp;#34;&lt;/span&gt;);&lt;span style=&#34;color:#75715e&#34;&gt;#line 7&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;            &lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt; foobar = &lt;span style=&#34;color:#ae81ff&#34;&gt;_&lt;/span&gt;s_123                              ,&lt;span style=&#34;color:#75715e&#34;&gt;#line 7&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;                                                                  baz = Baz();        }        &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt; Foo() { &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Foo&amp;#34;&lt;/span&gt;; }        &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt; Baz() { &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Baz&amp;#34;&lt;/span&gt;; }    }}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;a href=&#34;https://i.imgur.com/SDYjx73.png&#34;&gt;&lt;img src=&#34;https://i.imgur.com/SDYjx73.png&#34; alt=&#34;pics or it didn&amp;rsquo;thappen&#34;&gt;&lt;/a&gt;&lt;/p&gt;</description>
     </item>
   
     <item>
       <title>ZeroMQ XPUB sockets</title>
       <link>https://m0sa.net/posts/2012-04-zeromq-xpub-sockets/</link>
       <pubDate>Wed, 04 Apr 2012 00:00:00 +0000</pubDate>
       
       <guid>https://m0sa.net/posts/2012-04-zeromq-xpub-sockets/</guid>
       <description>&lt;p&gt;I was wlaying around with the new &lt;a href=&#34;http://www.zeromq.org/whitepapers:0mq-3-0-pubsub&#34;&gt;XPUB &amp;amp;XSUB&lt;/a&gt; ZeroMQ socket typesthat allow subscription forwarding, e.g. the producer (XPUB) can track whatvalues he has to produce, which comes in handy when the value production is anexpensive operation and the producer cannot afford to produce &lt;em&gt;all&lt;/em&gt; values.&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; thread&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; time&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; zmq&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; zmq.core.socket &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; Socket&lt;span style=&#34;color:#75715e&#34;&gt;# global zmg context&lt;/span&gt;context &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; zmq&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;Context()endpoint &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;tcp://*:8888&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# the subscriber thread function&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;subscriber&lt;/span&gt;(name, address, cnt, subscriptions):    print (&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;starting worker thread &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;%s&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; subscribing to &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;%s&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; for &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;%s&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;%&lt;/span&gt;(name,address,subscriptions))    sub &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; Socket(context, zmq&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;SUB)    sub&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;connect(address)    &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; subscription &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; subscriptions:        sub&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;setsockopt(zmq&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;SUBSCRIBE, subscription)    &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; x &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; range(&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, cnt):        print (&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;%s&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; received &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;%s&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;%&lt;/span&gt; (name, sub&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;recv()))    print (&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;%s&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; closing socket after &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;%d&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; messages&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;%&lt;/span&gt; (name, cnt))    sub&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;close()&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;main&lt;/span&gt;():    publisher &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; Socket(context, zmq&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;XPUB)    publisher&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;bind(endpoint)    address &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;tcp://localhost:8888&amp;#34;&lt;/span&gt;    thread&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;start_new(subscriber, (&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;s1&amp;#34;&lt;/span&gt;, address, &lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt;, [&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;a&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;b&amp;#34;&lt;/span&gt;]))    thread&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;start_new(subscriber, (&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;s2&amp;#34;&lt;/span&gt;, address, &lt;span style=&#34;color:#ae81ff&#34;&gt;20&lt;/span&gt;, [&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;b&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;c&amp;#34;&lt;/span&gt;]))    subscriptions &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; []    r &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;while&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;:        &lt;span style=&#34;color:#75715e&#34;&gt;# handle subscription flow first to decide what messages need to be produced&lt;/span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;while&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;:            &lt;span style=&#34;color:#66d9ef&#34;&gt;try&lt;/span&gt;:                rc &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; publisher&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;recv(zmq&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;NOBLOCK)                subscription &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; rc[&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;:]                status &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; rc[&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;]&lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;\x01&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;                method &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; subscriptions&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;append &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; status &lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt; subscriptions&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;remove                method(subscription)            &lt;span style=&#34;color:#66d9ef&#34;&gt;except&lt;/span&gt; zmq&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;ZMQError:                &lt;span style=&#34;color:#66d9ef&#34;&gt;break&lt;/span&gt;        &lt;span style=&#34;color:#75715e&#34;&gt;# produce a value for each existing subscription&lt;/span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; subscription &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; subscriptions:            print &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;sending &amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; subscription            publisher&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;send(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;%s&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;%d&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;%&lt;/span&gt; (subscription, r))        time&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;sleep(&lt;span style=&#34;color:#ae81ff&#34;&gt;0.5&lt;/span&gt;)        r &lt;span style=&#34;color:#f92672&#34;&gt;+=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; __name__ &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;__main__&amp;#34;&lt;/span&gt;:    main()&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
     </item>
   
     <item>
       <title>C# gotcha</title>
       <link>https://m0sa.net/posts/2011-04-csharp-gotcha_html/</link>
       <pubDate>Thu, 14 Apr 2011 00:00:00 +0000</pubDate>
       
       <guid>https://m0sa.net/posts/2011-04-csharp-gotcha_html/</guid>
       <description>&lt;p&gt;Consider you have the following code:&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-csharp&#34; data-lang=&#34;csharp&#34;&gt;SomeClass reference = &lt;span style=&#34;color:#66d9ef&#34;&gt;null&lt;/span&gt;;  Console.WriteLine(reference.Echo(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;echo&amp;#34;&lt;/span&gt;));&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Your first thought might be &lt;code&gt;NullReferenceException&lt;/code&gt;. Ha, think again! What ifyou have an class defining an extension method like this:&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-csharp&#34; data-lang=&#34;csharp&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;SomeClassExt&lt;/span&gt;{    &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt; Echo(&lt;span style=&#34;color:#66d9ef&#34;&gt;this&lt;/span&gt; SomeClass x, &lt;span style=&#34;color:#66d9ef&#34;&gt;string&lt;/span&gt; str)    {        &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; str;    }}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You have been warned :)&lt;/p&gt;</description>
     </item>
   
     <item>
       <title>Async Producer-Consumer with Linq, IEnumerable&lt;T&gt; and Threading.Tasks</title>
       <link>https://m0sa.net/posts/2011-03-async-producer-consumer-with-linq/</link>
       <pubDate>Wed, 09 Mar 2011 00:00:00 +0000</pubDate>
       
       <guid>https://m0sa.net/posts/2011-03-async-producer-consumer-with-linq/</guid>
       <description>&lt;p&gt;Lately I&amp;rsquo;ve been dealing a lot with IEnumerable&lt;!-- raw HTML omitted --&gt; classes and the yieldreturn keyword. I found it really useful for stream like processing ofotherwise really memory consuming operations. The workflow looks like this:&lt;/p&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;\- Retrieve Item 1 _(yield return)_\- Process Item 1 _(GetNext())_\- …\- Retrieve Item N\- Process Item N&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The approximate time it takes to process the whole operation is:&lt;/p&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;N x AvgTime(Retrieve) + N x AvgTime(Process)&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So I was thinking how to parallelize the whole operation, preferably in a Linqfashion, so it could fit into my existing code. Here is the result, using the(relatively) new System.Collections.Concurrent namespace and the Task ParallelLibrary:&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-csharp&#34; data-lang=&#34;csharp&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; IEnumerable&amp;lt;TResult&amp;gt; SelectAsync&amp;lt;TProduct, TResult&amp;gt;(    &lt;span style=&#34;color:#66d9ef&#34;&gt;this&lt;/span&gt; IEnumerable&amp;lt;TProduct&amp;gt; producer,    Func&amp;lt;TProduct, TResult&amp;gt; consumer,    &lt;span style=&#34;color:#66d9ef&#34;&gt;int?&lt;/span&gt; productsBoundedCapacity = &lt;span style=&#34;color:#66d9ef&#34;&gt;null&lt;/span&gt;,    &lt;span style=&#34;color:#66d9ef&#34;&gt;int?&lt;/span&gt; resultsBoundedCapacity = &lt;span style=&#34;color:#66d9ef&#34;&gt;null&lt;/span&gt;){    &lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; productsQueue = productsBoundedCapacity.HasValue        ? &lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; BlockingCollection&amp;lt;TProduct&amp;gt;(productsBoundedCapacity.Value)        : &lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; BlockingCollection&amp;lt;TProduct&amp;gt;();    &lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; resultsQueue = resultsBoundedCapacity.HasValue        ? &lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; BlockingCollection&amp;lt;Tuple&amp;lt;&lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt;, TResult&amp;gt;&amp;gt;(resultsBoundedCapacity.Value)        : &lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; BlockingCollection&amp;lt;Tuple&amp;lt;&lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt;, TResult&amp;gt;&amp;gt;();    Task.Factory.StartNew(() =&amp;gt;    {        &lt;span style=&#34;color:#66d9ef&#34;&gt;foreach&lt;/span&gt; (&lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; product &lt;span style=&#34;color:#66d9ef&#34;&gt;in&lt;/span&gt; producer)        {            productsQueue.Add(product);        }        productsQueue.CompleteAdding();    });    Task.Factory.StartNew(() =&amp;gt;    {        &lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; tasks = productsQueue            .GetConsumingEnumerable()            .Select((product, ind) =&amp;gt; Task.Factory.StartNew(() =&amp;gt;                    resultsQueue.Add(&lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; Tuple&amp;lt;&lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt;, TResult&amp;gt;(ind, consumer(product)))))            .ToArray();        Task.WaitAll(tasks);        resultsQueue.CompleteAdding();    });    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; resultsQueue        .GetConsumingEnumerable()        .OrderBy(x =&amp;gt; x.Item1)        .Select(x =&amp;gt; x.Item2);}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Note however, that the producer part parallelizes correctly only with theusage of true enumerables that yield return the long running operation result(Arrays, List etc. already contain all the data). Here is a small proof ofconcept app:&lt;/p&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;class Program{    private static IEnumerable&amp;lt;int&amp;gt; Producer()    {        for (int i = 0; i &amp;lt; 100; i++)        {            Thread.Sleep(50); // long running operation            yield return i;        }    }    private static int Consumer(int item)    {        Thread.Sleep(100); // long running operation        return item * item;    }    static void Main()    {        var sw = new Stopwatch();        sw.Start();        var resultSync = Producer().Select(Consumer).Sum();        sw.Stop();        Console.WriteLine(&amp;quot;Result: {0},  {1}ms&amp;quot;, resultSync, sw.ElapsedMilliseconds);        sw.Reset();        sw.Start();        var resultAsync = Producer().SelectAsync(Consumer).Sum();        sw.Stop();        Console.WriteLine(&amp;quot;Result: {0},  {1}ms&amp;quot;, resultAsync, sw.ElapsedMilliseconds);        Console.ReadLine();    }}&lt;/code&gt;&lt;/pre&gt;</description>
     </item>
   
     <item>
       <title>Unit Testing JsonResult Actions</title>
       <link>https://m0sa.net/posts/2011-02-mvc-unit-testing-jsonresult-actions/</link>
       <pubDate>Thu, 10 Feb 2011 00:00:00 +0000</pubDate>
       
       <guid>https://m0sa.net/posts/2011-02-mvc-unit-testing-jsonresult-actions/</guid>
       <description>&lt;p&gt;Today I had to write a unit test for a Asp.net MVC controller that returned aJsonResult. The Controller&amp;rsquo;s code looked somewhat like this:&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-csharp&#34; data-lang=&#34;csharp&#34;&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;    [HttpPost]&lt;/span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; JsonResult JsonData(Query query)    {      &lt;span style=&#34;color:#75715e&#34;&gt;// ... process query, get total_row_cnt and rows&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;      &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; Json(&lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; { total_row_cnt = rowCount, rows });    }&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I often return anonymous types as JsonResult, just because I don&amp;rsquo;t want tohave additional &lt;em&gt;return model classes&lt;/em&gt; for JavaScript that are used only byone controller action and nowhere else.&lt;/p&gt;&lt;p&gt;And what about unit testing such actions? Sure JsonResult exposes a propertycalled Data (of type object), that contains the argument passed to theController&amp;rsquo;s Json() call. If the type of the Data is known you can easily castit to the known type and do further processing. But what if you pass ananonymous type as data to the JsonResult? How can you verify the results then?&lt;/p&gt;&lt;p&gt;Well you have the following options:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;convert the anonymous type to a class,&lt;/li&gt;&lt;li&gt;directly use reflection on the returned object (magic strings!),&lt;/li&gt;&lt;li&gt;or make use of C# 4 dynamics!&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;The first two options are trivial, and I&amp;rsquo;m going to discuss only the lastoption. Simply casting the JsonResult.Data won&amp;rsquo;t do, since the type objectdoes not implement the necessary infrastructure. I decided to implement awrapper class that implements DynamicObject. Since anonymous types can offeronly readonly properties all I had to do was to override the TryGetMembermethod. So here is the implementation:&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-csharp&#34; data-lang=&#34;csharp&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;using&lt;/span&gt; System;&lt;span style=&#34;color:#66d9ef&#34;&gt;using&lt;/span&gt; System.Dynamic;&lt;span style=&#34;color:#66d9ef&#34;&gt;using&lt;/span&gt; System.Reflection;&lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;sealed&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;AnonymusDynamicGetWrapper&lt;/span&gt; : DynamicObject{  &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;readonly&lt;/span&gt; Type &lt;span style=&#34;color:#ae81ff&#34;&gt;_&lt;/span&gt;subjectType;  &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;readonly&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;object&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;_&lt;/span&gt;subject;  &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;dynamic&lt;/span&gt; Create(&lt;span style=&#34;color:#66d9ef&#34;&gt;object&lt;/span&gt; subject)  {    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; AnonymusDynamicGetWrapper(subject);  }  &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt; AnonymusDynamicGetWrapper(&lt;span style=&#34;color:#66d9ef&#34;&gt;object&lt;/span&gt; subject)  {    &lt;span style=&#34;color:#ae81ff&#34;&gt;_&lt;/span&gt;subject = subject;    &lt;span style=&#34;color:#ae81ff&#34;&gt;_&lt;/span&gt;subjectType = subject.GetType();  }  &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;override&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;bool&lt;/span&gt; TryGetMember(GetMemberBinder binder, &lt;span style=&#34;color:#66d9ef&#34;&gt;out&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;object&lt;/span&gt; result)  {    result = &lt;span style=&#34;color:#66d9ef&#34;&gt;null&lt;/span&gt;;    &lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; propertyInfo = &lt;span style=&#34;color:#ae81ff&#34;&gt;_&lt;/span&gt;subjectType.GetProperty(binder.Name);    &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (propertyInfo == &lt;span style=&#34;color:#66d9ef&#34;&gt;null&lt;/span&gt;) &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;false&lt;/span&gt;;    &lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; getter = propertyInfo.GetGetMethod();    &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (getter == &lt;span style=&#34;color:#66d9ef&#34;&gt;null&lt;/span&gt;) &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;false&lt;/span&gt;;    result = getter.Invoke(&lt;span style=&#34;color:#ae81ff&#34;&gt;_&lt;/span&gt;subject, &lt;span style=&#34;color:#66d9ef&#34;&gt;null&lt;/span&gt;);    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;true&lt;/span&gt;;  }}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And finally the unit test method:&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-csharp&#34; data-lang=&#34;csharp&#34;&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;[TestMethod]&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt; Simple_Data_Test(){  &lt;span style=&#34;color:#75715e&#34;&gt;// ... setup the test, controller and query&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; data = controller.JsonData(query).Data;  &lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; rowCnt = AnonymusDynamicGetWrapper.Create(data).total_row_cnt  Assert.AreEqual(&lt;span style=&#34;color:#ae81ff&#34;&gt;25&lt;/span&gt;, rowCnt);}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
     </item>
   
     <item>
       <title>Obfuscated JS Hello World CodeGolf</title>
       <link>https://m0sa.net/posts/2011-02-obfuscated-javascript-hello-world/</link>
       <pubDate>Fri, 04 Feb 2011 00:00:00 +0000</pubDate>
       
       <guid>https://m0sa.net/posts/2011-02-obfuscated-javascript-hello-world/</guid>
       <description>&lt;p&gt;Finally the &lt;a href=&#34;https://codegolf.stackexchage.com&#34;&gt;codegolf Stackexchange&lt;/a&gt; site is in beta. So I decided to give it atry, as I found&lt;a href=&#34;https://codegolf.stackexchange.com/questions/307/obfuscated-hello-world&#34;&gt;this&lt;/a&gt;nice codegolf:&lt;/p&gt;&lt;p&gt;[&lt;img src=&#34;https://lh6.googleusercontent.com/_-AYvMXY8i7Q/TUtOiX1x-ZI/AAAAAAAAAEE/2ipQ5l8Q4d4/golf_thumb%5B1%5D.png?imgmax=800&#34; alt=&#34;golf&#34;&gt;](&lt;a href=&#34;https://lh6.googleusercontent.com/_-AYvMXY8i7Q/TUtOiKTSVGI/AAAAAAAAAEA/RWA-&#34;&gt;https://lh6.googleusercontent.com/_-AYvMXY8i7Q/TUtOiKTSVGI/AAAAAAAAAEA/RWA-&lt;/a&gt;_dMVaaE/s1600-h/golf%5B3%5D.png)&lt;/p&gt;&lt;p&gt;So…. Behold my[solution](&lt;a href=&#34;https://codegolf.stackexchange.com/questions/307/obfuscated-hello-&#34;&gt;https://codegolf.stackexchange.com/questions/307/obfuscated-hello-&lt;/a&gt;world/453#453) with JavaScript:&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;javascript&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;_&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;_&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;[][(&lt;span style=&#34;color:#a6e22e&#34;&gt;f&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=!!&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;_&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;&amp;#39;&lt;/span&gt;)&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;_&lt;/span&gt;)[&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;]&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;b&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;({}&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;_&lt;/span&gt;)[&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;])&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;$&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;d&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=!&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;_&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;_&lt;/span&gt;)[&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;])&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;d&lt;/span&gt;[&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;])])())[&lt;span style=&#34;color:#a6e22e&#34;&gt;f&lt;/span&gt;[&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;]&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;l&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;a&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;d&lt;/span&gt;[&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;])&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;$&lt;/span&gt;](&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;H&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;a&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;ll&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;b&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39; W&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;b&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;c&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;ld&amp;#39;&lt;/span&gt;);&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Feel free to try it out. Just paste it in your browser&amp;rsquo;s address bar and letit do the magic.&lt;/p&gt;&lt;p&gt;Well if you would like to know how it works I suggest you read this blog postfirst (I&amp;rsquo;ve actually shared it in my Google Reader feed a month back, so youmight have already seen it). The script uses the same principle to acquire thesort() function (and consequently the window object and alert() function) asdescribed there. I also found this nice &lt;a href=&#34;https://vimeo.com/15961577&#34;&gt;video&lt;/a&gt;that was very helpful.&lt;/p&gt;</description>
     </item>
   
     <item>
       <title>jQuery.validation breaks jQuery 1.5 ajax API</title>
       <link>https://m0sa.net/posts/2011-02-jqueryvalidation-breaks-jquery-15-ajax/</link>
       <pubDate>Thu, 03 Feb 2011 00:00:00 +0000</pubDate>
       
       <guid>https://m0sa.net/posts/2011-02-jqueryvalidation-breaks-jquery-15-ajax/</guid>
       <description>&lt;p&gt;Today I found out (the hard way) that the use of the[jQuery.validation](&lt;a href=&#34;http://bassistance.de/jquery-plugins/jquery-plugin-&#34;&gt;http://bassistance.de/jquery-plugins/jquery-plugin-&lt;/a&gt;validation/) plugin breaks the&lt;a href=&#34;http://api.jquery.com/jQuery.ajax/&#34;&gt;jQuery.ajax&lt;/a&gt; API. Since the &lt;a href=&#34;http://www.asp.net/mvc&#34;&gt;Asp.net MVC3&lt;/a&gt; uses jQuery.validation for its [unobtrusivevalidation](&lt;a href=&#34;http://weblogs.asp.net/mikaelsoderstrom/archive/2010/10/06/unobtrusive-&#34;&gt;http://weblogs.asp.net/mikaelsoderstrom/archive/2010/10/06/unobtrusive-&lt;/a&gt;validation-in-asp-net-mvc-3.aspx) it is included in the default projecttemplate. You can imagine my surprise when I updated jQuery to v1.5, and myheavy ajaxified MVC page stopped working correctly. Every ajax requestcontained an &lt;em&gt;parseerror&lt;/em&gt; with an error message stating that the callback&lt;em&gt;jQuery[some random string] was not called&lt;/em&gt;. I tried to fiddle with the &lt;a href=&#34;http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings&#34;&gt;ajaxsettings&lt;/a&gt; for JSONP(namely &lt;em&gt;jsonp&lt;/em&gt; and &lt;em&gt;jsonpCallback&lt;/em&gt;), but no matter how I set them, the errorwas still present.&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://lh5.googleusercontent.com/_-AYvMXY8i7Q/TUnzgfsqExI/AAAAAAAAAD4/-l5paatvD30/s1600-h/error_example%5B145%5D.png&#34;&gt;![error_example](https://lh6.googleusercontent.com/_-AYvMXY8i7Q/TUnzg-ApZ4I/AAAAAAAAAD8/5T_rDsz_0L4/error_example_thumb%5B143%5D.png?imgmax=800)&lt;/a&gt;&lt;/p&gt;&lt;p&gt;So I started digging around, trying to replicate the problem. When I ransimple html pages &lt;a href=&#34;http://api.jquery.com/jQuery.ajax/&#34;&gt;$.ajax&lt;/a&gt; worked asexpected. Then I started to gradually test all the jQuery plugins and foundout that the plugin causing the error was[jQuery.validation](&lt;a href=&#34;http://bassistance.de/jquery-plugins/jquery-plugin-&#34;&gt;http://bassistance.de/jquery-plugins/jquery-plugin-&lt;/a&gt;validation/). So here is my reproduction and a simple workaround (the wholeproject is available &lt;a href=&#34;http://dl.dropbox.com/u/6819076/jQuery15Test.7z&#34;&gt;here&lt;/a&gt;):&lt;/p&gt;&lt;p&gt;&lt;strong&gt;The controller:&lt;/strong&gt;&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-csharp&#34; data-lang=&#34;csharp&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;using&lt;/span&gt; System.Linq;&lt;span style=&#34;color:#66d9ef&#34;&gt;using&lt;/span&gt; System.Web.Mvc;&lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;partial&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;DefaultController&lt;/span&gt; : Controller{  &lt;span style=&#34;color:#66d9ef&#34;&gt;private&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;readonly&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt;[] numbers = Enumerable.Range(&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt;).ToArray();  &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;virtual&lt;/span&gt; ActionResult Index()  {    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; View();  }&lt;span style=&#34;color:#a6e22e&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;  [HttpPost]&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;  [ValidateAntiForgeryToken]&lt;/span&gt;  &lt;span style=&#34;color:#66d9ef&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;virtual&lt;/span&gt; JsonResult SimpleArray(&lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; id)  {    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; Json(numbers.Concat(&lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; [] { id }));  }}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;The view:&lt;/strong&gt;&lt;/p&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code class=&#34;language-cshtml&#34; data-lang=&#34;cshtml&#34;&gt;@{ Layout = null; }  &amp;lt;!DOCTYPE html&amp;gt;  &amp;lt;html&amp;gt;  &amp;lt;head&amp;gt;  &amp;lt;title&amp;gt;Index&amp;lt;/title&amp;gt;    &amp;lt;script src=&amp;quot;/Scripts/jquery-1.5.js&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;    &amp;lt;script src=&amp;quot;/Scripts/jquery.validate.js&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;  &amp;lt;/head&amp;gt;    &amp;lt;body&amp;gt;    &amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;      $(function () {        $(&#39;a&#39;).click(function (e) {          var req = $.ajax({              url: &#39;@Url.Action(MVC.Default.SimpleArray())&#39;,              type: &#39;POST&#39;,               data: $(&#39;form&#39;).serializeArray(),              dataType: &#39;json&#39;           });          req.success(function (response, status, xhr) { alert(&#39;Success: &#39; + response); });          req.error(function (xhr, error, msg) { alert(&#39;Error &amp;quot;&#39; + error + &#39;&amp;quot;: &#39; + msg); });        });      });    &amp;lt;/script&amp;gt;    &amp;lt;a href=&amp;quot;javascript:void(0);&amp;quot;&amp;gt;Do request&amp;lt;/a&amp;gt;    &amp;lt;form action=&amp;quot;#&amp;quot;&amp;gt;      &amp;lt;input type=&amp;quot;hidden&amp;quot; value=&amp;quot;-1&amp;quot; name=&amp;quot;id&amp;quot; /&amp;gt;      @Html.AntiForgeryToken()    &amp;lt;/form&amp;gt;  &amp;lt;/body&amp;gt;  &amp;lt;/html&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;The workaround:&lt;/strong&gt;&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-js&#34; data-lang=&#34;js&#34;&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;$&lt;/span&gt;(&lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt; () {  &lt;span style=&#34;color:#a6e22e&#34;&gt;$&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;ajaxSettings&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;cache&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;false&lt;/span&gt;;  &lt;span style=&#34;color:#a6e22e&#34;&gt;$&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;ajaxSettings&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;jsonp&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;undefined&lt;/span&gt;;  &lt;span style=&#34;color:#a6e22e&#34;&gt;$&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;ajaxSettings&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;jsonpCallback&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;undefined&lt;/span&gt;;})&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The cause of the problem is this line of JavaScript in jQuery.validate.js,that overrides the settings you pass into the $.ajax call with all the defaultones (and jQuery.ajaxSettings defaults to { jsonp: &amp;ldquo;callback&amp;rdquo;, jsonpCallback:function() {&amp;hellip;}}):&lt;/p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-js&#34; data-lang=&#34;js&#34;&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;// create settings for compatibility with ajaxSetup&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;settings&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;$&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;extend&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;settings&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;$&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;extend&lt;/span&gt;({}, &lt;span style=&#34;color:#a6e22e&#34;&gt;$&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;ajaxSettings&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;settings&lt;/span&gt;));&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
     </item>
   
 </channel>
</rss>
