{"generator":"Jekyll","link":[{"@attributes":{"href":"https:\/\/cklutz.github.io\/feed.xml","rel":"self","type":"application\/atom+xml"}},{"@attributes":{"href":"https:\/\/cklutz.github.io\/","rel":"alternate","type":"text\/html"}}],"updated":"2023-12-15T13:18:11+00:00","id":"https:\/\/cklutz.github.io\/feed.xml","title":"HIMEM.SYS","subtitle":"A place to dump information about software development related stuff, so I don't have to remember it.\n","entry":[{"title":"Getting a minidump from Environment.FailFast","link":{"@attributes":{"href":"https:\/\/cklutz.github.io\/2023\/12\/15\/environment-failfast-wer\/","rel":"alternate","type":"text\/html","title":"Getting a minidump from Environment.FailFast"}},"published":"2023-12-15T07:00:00+00:00","updated":"2023-12-15T07:00:00+00:00","id":"https:\/\/cklutz.github.io\/2023\/12\/15\/environment-failfast-wer","content":"<p>This is just a quick reminder why <code class=\"language-plaintext highlighter-rouge\">Environment.FailFast<\/code> may not generate a mini dump on Windows.<\/p>\n\n<p>It only does that, when the Windows Error Reporting Service (<code class=\"language-plaintext highlighter-rouge\">WerSvc<\/code>) is not disabled.<\/p>\n\n<p>Additionally, registry entries could disable WER, even if the service is not disabled.<\/p>\n\n<p>The following C# [1] code checks if WER is enabled.<\/p>\n\n<div class=\"language-csharp highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>    <span class=\"c1\">\/\/\/ &lt;summary&gt;<\/span>\n    <span class=\"c1\">\/\/\/ Checks if Windows Error Reporting (WER) is currently enabled.<\/span>\n    <span class=\"c1\">\/\/\/ &lt;\/summary&gt;<\/span>\n    <span class=\"c1\">\/\/\/ &lt;returns&gt;<\/span>\n    <span class=\"c1\">\/\/\/ &lt;c&gt;true&lt;\/c&gt; enabled, &lt;c&gt;false&lt;\/c&gt; if not.<\/span>\n    <span class=\"c1\">\/\/\/ &lt;\/returns&gt;<\/span>\n    <span class=\"k\">public<\/span> <span class=\"k\">static<\/span> <span class=\"kt\">bool<\/span> <span class=\"nf\">IsWerEnabled<\/span><span class=\"p\">()<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"k\">const<\/span> <span class=\"kt\">string<\/span> <span class=\"n\">WerBaseKey<\/span> <span class=\"p\">=<\/span> <span class=\"s\">@\"Software\\Microsoft\\Windows\\Windows Error Reporting\"<\/span><span class=\"p\">;<\/span>\n\n        <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">Registry<\/span><span class=\"p\">.<\/span><span class=\"n\">CurrentUser<\/span><span class=\"p\">.<\/span><span class=\"nf\">GetBooleanValue<\/span><span class=\"p\">(<\/span><span class=\"n\">WerBaseKey<\/span><span class=\"p\">,<\/span> <span class=\"s\">\"Disabled\"<\/span><span class=\"p\">)<\/span> <span class=\"p\">||<\/span>\n            <span class=\"n\">Registry<\/span><span class=\"p\">.<\/span><span class=\"n\">LocalMachine<\/span><span class=\"p\">.<\/span><span class=\"nf\">GetBooleanValue<\/span><span class=\"p\">(<\/span><span class=\"n\">WerBaseKey<\/span><span class=\"p\">,<\/span> <span class=\"s\">\"Disabled\"<\/span><span class=\"p\">))<\/span>\n        <span class=\"p\">{<\/span>\n            <span class=\"k\">return<\/span> <span class=\"k\">false<\/span><span class=\"p\">;<\/span>\n        <span class=\"p\">}<\/span>\n\n        <span class=\"k\">using<\/span> <span class=\"p\">(<\/span><span class=\"kt\">var<\/span> <span class=\"n\">sc<\/span> <span class=\"p\">=<\/span> <span class=\"k\">new<\/span> <span class=\"nf\">ServiceController<\/span><span class=\"p\">(<\/span><span class=\"s\">\"WerSvc\"<\/span><span class=\"p\">))<\/span>\n        <span class=\"p\">{<\/span>\n            <span class=\"c1\">\/\/ Technically, the service has a start type of \"Manual (Trigger Start)\",<\/span>\n            <span class=\"c1\">\/\/ but other non-disabled start types (like Automatic) work as well.<\/span>\n            <span class=\"k\">return<\/span> <span class=\"n\">sc<\/span><span class=\"p\">.<\/span><span class=\"n\">StartType<\/span> <span class=\"p\">!=<\/span> <span class=\"n\">ServiceStartMode<\/span><span class=\"p\">.<\/span><span class=\"n\">Disabled<\/span><span class=\"p\">;<\/span>\n        <span class=\"p\">}<\/span>\n    <span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The CLR will also generate a mini dump on an uncaught exception. This, however, does not\ndepend on WER, but is handled by the CLR internally. For .NET Core the CLR will launch\nthe external <code class=\"language-plaintext highlighter-rouge\">createdump.exe<\/code> tool (either in the .NET shared directory or in your \nbin directory if you build self contained).<\/p>\n\n<p>[1] <code class=\"language-plaintext highlighter-rouge\">GetBooleanValue<\/code> is a simple extension method:<\/p>\n\n<div class=\"language-csharp highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>        <span class=\"k\">public<\/span> <span class=\"k\">static<\/span> <span class=\"kt\">bool<\/span> <span class=\"nf\">GetBooleanValue<\/span><span class=\"p\">(<\/span><span class=\"k\">this<\/span> <span class=\"n\">RegistryKey<\/span> <span class=\"n\">key<\/span><span class=\"p\">,<\/span> <span class=\"kt\">string<\/span> <span class=\"n\">subkeyName<\/span><span class=\"p\">,<\/span> <span class=\"kt\">string<\/span> <span class=\"k\">value<\/span><span class=\"p\">)<\/span>\n        <span class=\"p\">{<\/span>\n            <span class=\"k\">using<\/span> <span class=\"p\">(<\/span><span class=\"kt\">var<\/span> <span class=\"n\">subKey<\/span> <span class=\"p\">=<\/span> <span class=\"n\">key<\/span><span class=\"p\">.<\/span><span class=\"nf\">OpenSubKey<\/span><span class=\"p\">(<\/span><span class=\"n\">subkeyName<\/span><span class=\"p\">))<\/span>\n            <span class=\"p\">{<\/span>\n                <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">subKey<\/span> <span class=\"p\">!=<\/span> <span class=\"k\">null<\/span><span class=\"p\">)<\/span>\n                <span class=\"p\">{<\/span>\n                    <span class=\"kt\">object<\/span> <span class=\"n\">obj<\/span> <span class=\"p\">=<\/span> <span class=\"n\">subKey<\/span><span class=\"p\">.<\/span><span class=\"nf\">GetValue<\/span><span class=\"p\">(<\/span><span class=\"k\">value<\/span><span class=\"p\">);<\/span>\n                    <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">obj<\/span> <span class=\"k\">is<\/span> <span class=\"kt\">int<\/span><span class=\"p\">)<\/span>\n                    <span class=\"p\">{<\/span>\n                        <span class=\"k\">return<\/span> <span class=\"p\">((<\/span><span class=\"kt\">int<\/span><span class=\"p\">)<\/span><span class=\"n\">obj<\/span><span class=\"p\">)<\/span> <span class=\"p\">!=<\/span> <span class=\"m\">0<\/span><span class=\"p\">;<\/span>\n                    <span class=\"p\">}<\/span>\n                <span class=\"p\">}<\/span>\n            <span class=\"p\">}<\/span>\n\n            <span class=\"k\">return<\/span> <span class=\"k\">false<\/span><span class=\"p\">;<\/span>\n        <span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>","author":{"name":{}},"category":[{"@attributes":{"term":".net"}},{"@attributes":{"term":".netcore"}},{"@attributes":{"term":"minidump"}},{"@attributes":{"term":"debugging"}}],"summary":"This is just a quick reminder why Environment.FailFast may not generate a mini dump on Windows. It only does that, when the Windows Error Reporting Service (WerSvc) is not disabled. Additionally, registry entries could disable WER, even if the service is not disabled. The following C# [1] code checks if WER is enabled. \/\/\/ &lt;summary&gt; \/\/\/ Checks if Windows Error Reporting (WER) is currently enabled. \/\/\/ &lt;\/summary&gt; \/\/\/ &lt;returns&gt; \/\/\/ &lt;c&gt;true&lt;\/c&gt; enabled, &lt;c&gt;false&lt;\/c&gt; if not. \/\/\/ &lt;\/returns&gt; public static bool IsWerEnabled() { const string WerBaseKey = @\"Software\\Microsoft\\Windows\\Windows Error Reporting\"; if (Registry.CurrentUser.GetBooleanValue(WerBaseKey, \"Disabled\") || Registry.LocalMachine.GetBooleanValue(WerBaseKey, \"Disabled\")) { return false; } using (var sc = new ServiceController(\"WerSvc\")) { \/\/ Technically, the service has a start type of \"Manual (Trigger Start)\", \/\/ but other non-disabled start types (like Automatic) work as well. return sc.StartType != ServiceStartMode.Disabled; } } The CLR will also generate a mini dump on an uncaught exception. This, however, does not depend on WER, but is handled by the CLR internally. For .NET Core the CLR will launch the external createdump.exe tool (either in the .NET shared directory or in your bin directory if you build self contained). [1] GetBooleanValue is a simple extension method: public static bool GetBooleanValue(this RegistryKey key, string subkeyName, string value) { using (var subKey = key.OpenSubKey(subkeyName)) { if (subKey != null) { object obj = subKey.GetValue(value); if (obj is int) { return ((int)obj) != 0; } } } return false; }"},{"title":"A life without ReSharper","link":{"@attributes":{"href":"https:\/\/cklutz.github.io\/2020\/02\/04\/a-life-without-resharper\/","rel":"alternate","type":"text\/html","title":"A life without ReSharper"}},"published":"2020-02-04T09:00:00+00:00","updated":"2020-02-04T09:00:00+00:00","id":"https:\/\/cklutz.github.io\/2020\/02\/04\/a-life-without-resharper","content":"<p><em>Note: This topic is not finished. As I\u2019ll discover new things, I\u2019ll provide additional parts\nto this post (or might update this post). So check back if you\u2019re interested enough.<\/em><\/p>\n\n<p><em>Update: 04.02.2020 - Added \u201cStacktrace Explorer\u201d.<\/em><\/p>\n\n<p>I have been a long time ReSharper user. I have used quite a lot of features, but surely not all.\nAs probably many users of ReSharper I have always been lamenting about the performance impact it\nhas on especially loading (larger) solutions. For example, the one I work with currently have\nabout 150 projects. Additionally, the whole product is split about into 5 solutions in total,\nwhere the \u201csmaller\u201d ones still have around 50 projects.<\/p>\n\n<p>In the past, for experimental purposes, I have disabled ReSharper and surely noticed faster\nloading times.<\/p>\n\n<p>Now, to be fair, I would have to do some real benchmarking, then compare that with the value\nadded by ReSharper and then make a judgement of whether extended loading times <em>and<\/em> unspecified\ndelays when actually working are actually worth it. They very well could be.<\/p>\n\n<p>But that is not the point of this post (and I don\u2019t feel like actually doing that). The point is,\nthat today every new Visual Studio versions gains features that could replace what ReSharper can\ndo at least partly. Possibly being complemented by a range of free extensions for some features\nthat are not yet (or may never) available as part of Visual Studio itself.<\/p>\n\n<p>To make this one really count, one would have to do a full feature list of ReSharper, comparing\neach item with what is available from Visual Studio or, preferably, free extension.<\/p>\n\n<p>But I\u2019m not going to do that either. This post is a very opinionated piece, were I will only\nconsider those features that <em>I<\/em> have been using most in the past. Your mileage <em>will<\/em> vary,\nalso depending on when you actually read this. Because, as I said, Visual Studio <em>and<\/em>\nReSharper of course gain new features all the time.<\/p>\n\n<p>As a last disclaimer, I write this as I\u2019m basically only a couple of weeks into this thing,\nso I might have not even recognized yet that I\u2019m sorely missing a big feature of ReSharper\nthat I don\u2019t use too often.<\/p>\n\n<p>Having all that said, I\u2019ll be looking at Visual Studio 2019 16.4 and ReSharper 2019.3.<\/p>\n\n<p>So, in no particular order and relevance here we go:<\/p>\n\n<h2 id=\"code-style--fixes\">Code Style &amp; Fixes<\/h2>\n\n<p>This one is one (next to <a href=\"#refactorings\">refactorings<\/a> one of the big selling points of ReSharper.\nHowever, with the introduction of Roslyn, and especially <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/roslyn-sdk\/tutorials\/how-to-write-csharp-analyzer-code-fix\">analyzers &amp; fixers<\/a>,\nthere has been a constant increase in available code style checks and fixes from Visual Studio\nitself and also from extensions.<\/p>\n\n<p>You make your choice, but I\u2019m currently using <a href=\"https:\/\/github.com\/JosefPihrt\/Roslynator\">Roslynator<\/a> plus\nsome very specific home-grown analyzers to augment those that are available in Visual Studio.<\/p>\n\n<p>Also using the ever increasing <a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/create-portable-custom-editor-options?view=vs-2019\">.editorconfig support<\/a>\nin Visual Studio 2019 (plus the option to configure stuff in Visual Studio settings and the <a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/code-styles-and-code-cleanup?view=vs-2019\">export it to\na new or existing <code class=\"language-plaintext highlighter-rouge\">.editorconfig<\/code> file<\/a>)\nis really useful and sufficient for me.<\/p>\n\n<p>In fact even ReSharper can export (some?) of its code style settings to <code class=\"language-plaintext highlighter-rouge\">.editorconfig<\/code> right now,\nbut of course using different directives\/names than what VS does. So don\u2019t get too excited.<\/p>\n\n<p>In the end, when moving from ReSharper to Visual Studio (builtin + extensions). It is is possibly best,\nmore so required, to manually reconfigure the required code styles.<\/p>\n\n<p>That should be a one time process and you shouldn\u2019t sweat it. (It is BTW <em>the<\/em> main reason I didn\u2019t do this whole endeavor \nbefore but in the grand scheme of things, so not worth the worries, that I regret not trying it\nearlier.)<\/p>\n\n<h2 id=\"refactorings\">Refactorings<\/h2>\n\n<p>This is of course a biggy and possibly <em>the<\/em> place where a feature comparison would be both in order and also possibly\nobsolete \/ incomplete the moment you\u2019d take the time to write it down in detail. Personally, I find that what Visual\nStudio 2019 supports sufficient for most things I do daily.<\/p>\n\n<h2 id=\"unit-testing\">Unit Testing<\/h2>\n\n<p>I find the <a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/test\/run-unit-tests-with-test-explorer?view=vs-2019\">new Visual Studio Unit Text Explorer<\/a>\nto be quite enough for what I do. Other options like running a test from the context menu in the editor or solution explorer are also\nthere and OK for my purposes.<\/p>\n\n<h2 id=\"navigation\">Navigation<\/h2>\n\n<h3 id=\"goto-all--anything\">Goto All \/ Anything<\/h3>\n\n<p>Visual Studio has that feature for a while. The command is called <code class=\"language-plaintext highlighter-rouge\">Edit.GotoAll<\/code>.\nYou can bind it to the typical ReSharper-shortcut <code class=\"language-plaintext highlighter-rouge\">CTRL+T<\/code>.<\/p>\n\n<h3 id=\"find-all-references\">Find all References<\/h3>\n\n<p>Visual Stuido has that feature for a while. The command is called <code class=\"language-plaintext highlighter-rouge\">Edit.FindAllReferences<\/code>.\nYou can bind it to the typical ReSharper-shortcut <code class=\"language-plaintext highlighter-rouge\">SHIFT+F12<\/code>.<\/p>\n\n<h3 id=\"location-in-solution-explorer\">Location in Solution Explorer<\/h3>\n\n<p>Visual Studio has that feature for a while. The command is called <code class=\"language-plaintext highlighter-rouge\">SolutionExplorer.SyncWithActiveDocument<\/code>.\nYou can bind it to the typical ReSharper-shortcut <code class=\"language-plaintext highlighter-rouge\">CTRL+R CTRL+L<\/code>.<\/p>\n\n<h2 id=\"filling-some-gaps\">Filling some gaps<\/h2>\n\n<p>Here are some extensions that I found useful to fill some gaps. They are all open source,\nwhich is (for me) especially important. ReSharper has a rather good track record at being\nreadily available when a new Visual Studio version (even minor versions of \u201cdate versions\u201d,\ne.g. 16.1, 16.2, 16.3, etc. of 2019) comes out. While it doesn\u2019t always support the newest\nC# language feature (which I can image can be a drag given the rate at which C# evolves\ncurrently). However, there is a chance that your favorite open source extension is not\nreadily available, if ever. So you want to at least have a fair chance to fix that yourself,\nif you\u2019re dependent on it for whatever reason.\nAlso, as you will see below, different extensions have possibly overlapping features that\nyou want to watch out for to not have them collide, do unnecessary things (performance),\nor simply satisfy that <a href=\"https:\/\/en.wikipedia.org\/wiki\/Monk_(TV_series)\">Monk<\/a> in you.\nAlso, you want to test each (new) extension for a while to make sure it does not have\na negative impact your setup concerning stability, performance, etc.<\/p>\n\n<ul>\n  <li><a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=VisualStudioPlatformTeam.MatchMargin\">Match Margin<\/a>\n    <ul>\n      <li>Depending on your other VS settings use disable \u201cShow matches in Editor\u201d, to not have that done twice,\nfrom VS and the extension.<\/li>\n    <\/ul>\n  <\/li>\n  <li><a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ygoe.SelectionTodoHighlighter\">Selection and TODO Highlighter<\/a>\n    <ul>\n      <li><em>NOT<\/em> Open Source AFAICT, there is an older versions that can do the same and are open source, but for VS 2019\nthe author recommends this. Still free though.<\/li>\n      <li>Disable \u201cselection highlighting\u201d from options to not collide with \u201cMatch Margin\u201d-extension.<\/li>\n      <li>There are other extensions that can do roughly the same. Search the VS Marketplace for \u201cTODO highlighter\u201d, YMMV.<\/li>\n    <\/ul>\n  <\/li>\n  <li><a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=SamirBoulema.StackTraceExplorer#review-details\">Stacktrace Explorer<\/a><\/li>\n<\/ul>\n\n<h2 id=\"stuff-still-missing\">Stuff still missing<\/h2>\n\n<ul>\n  <li><del>The capability of ReSharper to provide intellisense for <code class=\"language-plaintext highlighter-rouge\">ToString(string format, ...)<\/code> style overloads.<\/del> Available built-in since Visual Studio 2019, 16.9.<\/li>\n  <li><a href=\"https:\/\/www.jetbrains.com\/help\/resharper\/Code_Analysis__String_Formatting_Methods.html\">String.Format() code analysis including for your own String.Format-like functions<\/a><\/li>\n<\/ul>","author":{"name":{}},"category":[{"@attributes":{"term":"resharper"}},{"@attributes":{"term":"tools"}},{"@attributes":{"term":"visualstudio"}}],"summary":"Note: This topic is not finished. As I\u2019ll discover new things, I\u2019ll provide additional parts to this post (or might update this post). So check back if you\u2019re interested enough."},{"title":"AppDomains are gone, so?","link":{"@attributes":{"href":"https:\/\/cklutz.github.io\/2020\/01\/27\/appdomains-are-gone-so\/","rel":"alternate","type":"text\/html","title":"AppDomains are gone, so?"}},"published":"2020-01-27T07:00:00+00:00","updated":"2020-01-27T07:00:00+00:00","id":"https:\/\/cklutz.github.io\/2020\/01\/27\/appdomains-are-gone-so","content":"<p>I\u2019ve just started a new project that is part of a bigger initiative to port a largish application from the .NET Framework to .NET Core (or .NET 5 if you will).<\/p>\n\n<p>One major thing there is the fact that it is heavily using AppDomains. It does that in a way that somewhat resembles what IIS is doing, if that helps you understand.\nThat is, each AppDomain hosts its own logical application (or service). Certain libraries used inside these assume that they have their own \u201cspace\u201d (like statics\/globals,\nsettings, etc.). So <em>not<\/em> just to \u201csimply\u201d ensure one can load different versions of the same assembly (e.g. JSON.NET).<\/p>\n\n<p>So just using the new <code class=\"language-plaintext highlighter-rouge\">AssemblyLoadContext<\/code>-mechansim is not enough for use. No, we need to do what Microsoft suggests (as one) possible\nway to deal with that: using separate processes.<\/p>\n\n<p>Mind you, that the project currently is alpha-state, at best, but nevertheless basically works - if you\u2019re interested.<\/p>\n\n<p>With no further ado, here is the content of the readme. For code and more goto <a href=\"https:\/\/github.com\/cklutz\/ProcessIsolation\">cklutz\/ProcessIsolation<\/a>\non Github.<\/p>\n\n<h2 id=\"processisolation\">ProcessIsolation<\/h2>\n\n<p>The ProcessIsolation project tries to provide a means of running (managed) code isolated from one another\nusing separate processes. This is one of the recommended scenarios that Microsoft suggests, now that\nAppDomains are no more.<\/p>\n\n<p>The basic architecture is conceptually rather simple. You have your own application (called the host),\nthat wants to run code inside isolated containers, so it doesn\u2019t interfere with stability of your\nhost process. This project uses operating system processes as those containers.<\/p>\n\n<p>The host references the <code class=\"language-plaintext highlighter-rouge\">ProcessIsolation.dll<\/code> assembly, which contains all the code required to\nmanage the isolation processes. The isolation processes themselves are instances of the <code class=\"language-plaintext highlighter-rouge\">pihost.exe<\/code>\nexecutable, that uses the .NET <code class=\"language-plaintext highlighter-rouge\">AssemblyLoadContext<\/code>-class to load the code that should actually\nrun in isolation.<\/p>\n\n<p>By using <code class=\"language-plaintext highlighter-rouge\">AssemblyLoadContext<\/code>, the isolated code may even reference different versions of dependencies\nthan the <code class=\"language-plaintext highlighter-rouge\">pihost.exe<\/code> and thus provides maximum flexibility in this regard.<\/p>\n\n<p>Additionally, the host can specify a number of properties for the isolation process. Including\nresource limits (like CPU or memory usage). Also, the ProcessIsolation framework ensures that\nno orphaned processes remain should the host crash. It also allows the host to transparently\nrestart an isolation process, should that be required.<\/p>\n\n<p>The ProcessIsolation framework comes as three nuget packages:<\/p>\n\n<ul>\n  <li><code class=\"language-plaintext highlighter-rouge\">ProcessIsolation<\/code>: contains the host side part of the framework<\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">ProcessIsolation.Host<\/code>: contains the <code class=\"language-plaintext highlighter-rouge\">pihost.exe<\/code> executable<\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">ProcessIsolation.Shared<\/code>: contains shared assemblies used by both of the above<\/li>\n<\/ul>\n\n<p>An example for a host process is available as <a href=\"example\/SampleHost\/Program.cs\">example\/SampleHost<\/a>.\nTypical code that could be loaded into an isolation process is available as <a href=\"example\/SampleLib\/SampleClass.cs\">example\/SampleLib<\/a>.\nNote that the this code has no reference to <code class=\"language-plaintext highlighter-rouge\">ProcessIsolation<\/code>.<\/p>\n\n<p>Don\u2019t get bogged down by the apparent complexity of the SampleHost. It also serves as a testbed\nto exercise most functions that the ProcessIsolation framework provides and thus has a multitude\nof command line options in this regard.<\/p>\n\n<p>The basic code to run code (from another assembly) in isolation is this:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-c#\" data-lang=\"c#\"><span class=\"kt\">var<\/span> <span class=\"n\">options<\/span> <span class=\"p\">=<\/span> <span class=\"k\">new<\/span> <span class=\"nf\">IsolationOptions<\/span><span class=\"p\">();<\/span>\n<span class=\"k\">using<\/span> <span class=\"p\">(<\/span><span class=\"kt\">var<\/span> <span class=\"n\">isolator<\/span> <span class=\"p\">=<\/span> <span class=\"k\">await<\/span> <span class=\"n\">ProcessIsolator<\/span><span class=\"p\">.<\/span><span class=\"nf\">CreateAsync<\/span><span class=\"p\">(<\/span><span class=\"n\">options<\/span><span class=\"p\">).<\/span><span class=\"nf\">ConfigureAwait<\/span><span class=\"p\">(<\/span><span class=\"k\">false<\/span><span class=\"p\">))<\/span>\n<span class=\"p\">{<\/span>\n    <span class=\"kt\">int<\/span> <span class=\"n\">res<\/span> <span class=\"p\">=<\/span> <span class=\"k\">await<\/span> <span class=\"n\">isolator<\/span><span class=\"p\">.<\/span><span class=\"nf\">InvokeMethodAsync<\/span><span class=\"p\">(<\/span><span class=\"s\">\"assembly.dll\"<\/span><span class=\"p\">,<\/span> <span class=\"s\">\"namespace.class.method\"<\/span><span class=\"p\">).<\/span><span class=\"nf\">ConfigureAwait<\/span><span class=\"p\">(<\/span><span class=\"k\">false<\/span><span class=\"p\">);<\/span>\n    <span class=\"k\">await<\/span> <span class=\"n\">isolator<\/span><span class=\"p\">.<\/span><span class=\"nf\">WaitForExitAsync<\/span><span class=\"p\">().<\/span><span class=\"nf\">ConfigureAwait<\/span><span class=\"p\">(<\/span><span class=\"k\">false<\/span><span class=\"p\">);<\/span>\n<span class=\"p\">}<\/span><\/code><\/pre><\/figure>\n\n<p>As you can see the current interface is somewhat limited. Basically it mimics the <code class=\"language-plaintext highlighter-rouge\">AppDomain.ExecuteAssembly()<\/code>-method.<\/p>\n\n<p>To make it work, the assembly being specified with the <code class=\"language-plaintext highlighter-rouge\">InvokeMethodAsync()<\/code>-method must have a method with the following\nsignature:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-c#\" data-lang=\"c#\">    <span class=\"k\">public<\/span> <span class=\"k\">static<\/span> <span class=\"kt\">int<\/span> <span class=\"nf\">Method<\/span><span class=\"p\">(<\/span><span class=\"kt\">string<\/span><span class=\"p\">[]<\/span> <span class=\"n\">args<\/span><span class=\"p\">)<\/span><\/code><\/pre><\/figure>\n\n<p>That is, it must<\/p>\n<ul>\n  <li>be public<\/li>\n  <li>be static<\/li>\n  <li>have a return type of <code class=\"language-plaintext highlighter-rouge\">int<\/code> (returning <code class=\"language-plaintext highlighter-rouge\">0<\/code> means OK, anything else an error)<\/li>\n  <li>accept a <code class=\"language-plaintext highlighter-rouge\">string[]<\/code> as arguments<\/li>\n<\/ul>\n\n<p>Thus, the interface between the host process and the isolation process (and thus the code running within) is rather\narcane. This is so, because in this first attempt of the project we didn\u2019t want to settle on a n IPC mechanism yet.<\/p>\n\n<p>As actual value of plain <code class=\"language-plaintext highlighter-rouge\">System.Diagnostics.Process<\/code> this project - as said above - provides a number of options,\nsettings and APIs to manage and handle the isolated processes. Explore the <code class=\"language-plaintext highlighter-rouge\">IsolationOptions<\/code> and <code class=\"language-plaintext highlighter-rouge\">ProcessIsolator<\/code>\nclasses for more information.<\/p>\n\n<p>The later is of course required in some form to allow complex communication between the host and the code in the\nisolation process. Something like remoting (which is no longer available).<\/p>\n\n<p>Currently, gRPC is not an option as it requires a full HTTP\/2 capable stack and thus would make Kestrel a dependency.\nAlso gRPC at the moment doesn\u2019t allow for local-host-only IPC or something based on named pipes. All in all that \nseems a bit heavyweight at the moment.<\/p>\n\n<p>Other options exist of course, as this project is using <a href=\"https:\/\/github.com\/jacqueskang\/IpcServiceFramework\"><code class=\"language-plaintext highlighter-rouge\">jacqueskang\/IpcServiceFramework<\/code><\/a> internally at the moment.<\/p>\n\n<p>Finally, there is nothing preventing a host and isolated user code to establish their on IPC (even gRPC) on top.<\/p>\n\n<p>To build the project use the <code class=\"language-plaintext highlighter-rouge\">build.ps<\/code> file:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-powershell\" data-lang=\"powershell\"><span class=\"n\">PS<\/span><span class=\"err\">&gt;<\/span><span class=\"w\"> <\/span><span class=\"nx\">build.ps1<\/span><span class=\"w\"> <\/span><span class=\"nt\">-Tidy<\/span><span class=\"w\"> <\/span><span class=\"nt\">-Build<\/span><span class=\"w\"> <\/span><span class=\"nt\">-Test<\/span><span class=\"w\"> <\/span><span class=\"nt\">-Example<\/span><\/code><\/pre><\/figure>","author":{"name":{}},"category":[{"@attributes":{"term":"appdomain"}},{"@attributes":{"term":".net"}},{"@attributes":{"term":".netcore"}}],"summary":"I\u2019ve just started a new project that is part of a bigger initiative to port a largish application from the .NET Framework to .NET Core (or .NET 5 if you will)."},{"title":"Fun with NaN","link":{"@attributes":{"href":"https:\/\/cklutz.github.io\/2019\/10\/03\/fun-with-nan\/","rel":"alternate","type":"text\/html","title":"Fun with NaN"}},"published":"2019-10-03T12:00:00+00:00","updated":"2019-10-03T12:00:00+00:00","id":"https:\/\/cklutz.github.io\/2019\/10\/03\/fun-with-nan","content":"<p>Ok, sorry for the (bad) pun, but naming is hard. In this post I just wanted to dump some stuff I recently did in an attempt to figure out, were in a couple of dozen lines of code of double-arithmetic a <code class=\"language-plaintext highlighter-rouge\">Double.Nan<\/code> occurred.<\/p>\n\n<h2 id=\"first-attempt-tinker-with-the-fpu-exception-settings\">First attempt: tinker with the FPU exception settings<\/h2>\n\n<p>First I thought it would be kind of cool, if the debugger just hit a breakpoint when a floating point value comes out to NaN. Unfortunately there is no such kind of setting (at least not for managed code). However, there is another thing that looks promising:<\/p>\n\n<p>If the FPU encounters some types of results  (NaN, Overflow, Underflow, etc.) it raises a hardware exception. Back in the day those would come up and result in real \u201cexceptions\u201d (or whatever the mechanism of choice of the respective language \/ platform was). However in most modern environments this no longer happens (the exceptions are disabled) and such conditions are translate into other constructs (like a <code class=\"language-plaintext highlighter-rouge\">Double.NaN<\/code> in the CLR).<\/p>\n\n<p>One can tinker with these settings by P\/Invoking <code class=\"language-plaintext highlighter-rouge\">_controlfp_s()<\/code> or some of its variants. However, there are some things to consider:<\/p>\n\n<p>1.) The occurance of NaN is converted into an exception (but different ones for the 32-bit vs. the 64-bit CLR)\n2.) Differences in behavior after an exception is thrown between 32bit (X86) and 64bit (X64) CLR.\n3.) <code class=\"language-plaintext highlighter-rouge\">Double.NaN<\/code> is used quite often inside the BCL itself and the value can occur in lots of places (for example in Hashtable, Dictionary, etc. when calculating the load factor, inside WPF when dealing with coordinates, etc.). This can lead to an annoying number of false positives you\u2019d have to deal with.<\/p>\n\n<p>Let\u2019s look at an example. The following is just a simple .NET Core 3.1 console application (the full framework CLR behaves the same):<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-c#\" data-lang=\"c#\"><span class=\"k\">using<\/span> <span class=\"nn\">System<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">using<\/span> <span class=\"nn\">System.Runtime.InteropServices<\/span><span class=\"p\">;<\/span>\n\n<span class=\"k\">namespace<\/span> <span class=\"nn\">ConsoleApp1<\/span>\n<span class=\"p\">{<\/span>\n    <span class=\"k\">class<\/span> <span class=\"nc\">Program<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"p\">[<\/span><span class=\"nf\">DllImport<\/span><span class=\"p\">(<\/span><span class=\"s\">\"msvcrt.dll\"<\/span><span class=\"p\">,<\/span> <span class=\"n\">CallingConvention<\/span> <span class=\"p\">=<\/span> <span class=\"n\">CallingConvention<\/span><span class=\"p\">.<\/span><span class=\"n\">Cdecl<\/span><span class=\"p\">)]<\/span>\n        <span class=\"k\">private<\/span> <span class=\"k\">static<\/span> <span class=\"k\">extern<\/span> <span class=\"kt\">uint<\/span> <span class=\"nf\">_controlfp_s<\/span><span class=\"p\">(<\/span><span class=\"k\">out<\/span> <span class=\"kt\">uint<\/span> <span class=\"n\">currentWord<\/span><span class=\"p\">,<\/span> <span class=\"kt\">uint<\/span> <span class=\"n\">a<\/span><span class=\"p\">,<\/span> <span class=\"kt\">uint<\/span> <span class=\"n\">b<\/span><span class=\"p\">);<\/span>\n\n        <span class=\"p\">[<\/span><span class=\"nf\">DllImport<\/span><span class=\"p\">(<\/span><span class=\"s\">\"msvcrt.dll\"<\/span><span class=\"p\">,<\/span> <span class=\"n\">CallingConvention<\/span> <span class=\"p\">=<\/span> <span class=\"n\">CallingConvention<\/span><span class=\"p\">.<\/span><span class=\"n\">Cdecl<\/span><span class=\"p\">)]<\/span>\n        <span class=\"k\">private<\/span> <span class=\"k\">static<\/span> <span class=\"k\">extern<\/span> <span class=\"kt\">uint<\/span> <span class=\"nf\">_clearfp<\/span><span class=\"p\">();<\/span>\n\n        <span class=\"k\">private<\/span> <span class=\"k\">const<\/span> <span class=\"kt\">uint<\/span> <span class=\"n\">_MCW_EM<\/span> <span class=\"p\">=<\/span> <span class=\"m\">0x0008001f<\/span><span class=\"p\">;<\/span> <span class=\"c1\">\/\/ From float.h<\/span>\n\n        <span class=\"p\">[<\/span><span class=\"n\">Flags<\/span><span class=\"p\">]<\/span>\n        <span class=\"k\">public<\/span> <span class=\"k\">enum<\/span> <span class=\"n\">FloatingPointExceptionMask<\/span>\n        <span class=\"p\">{<\/span>\n            <span class=\"n\">_EM_INVALID<\/span> <span class=\"p\">=<\/span> <span class=\"m\">0x00000010<\/span><span class=\"p\">,<\/span>\n            <span class=\"n\">_EM_DENOMRAL<\/span> <span class=\"p\">=<\/span> <span class=\"m\">0x00080000<\/span><span class=\"p\">,<\/span>\n            <span class=\"n\">_EM_ZERODIVIDE<\/span> <span class=\"p\">=<\/span> <span class=\"m\">0x00000008<\/span><span class=\"p\">,<\/span>\n            <span class=\"n\">_EM_OVERFLOW<\/span> <span class=\"p\">=<\/span> <span class=\"m\">0x00000004<\/span><span class=\"p\">,<\/span>\n            <span class=\"n\">_EM_UNDERFLOW<\/span> <span class=\"p\">=<\/span> <span class=\"m\">0x00000002<\/span><span class=\"p\">,<\/span>\n            <span class=\"n\">_EM_INEXACT<\/span> <span class=\"p\">=<\/span> <span class=\"m\">0x00000001<\/span>\n        <span class=\"p\">}<\/span>\n\n        <span class=\"p\">[<\/span><span class=\"nf\">CLSCompliant<\/span><span class=\"p\">(<\/span><span class=\"k\">false<\/span><span class=\"p\">)]<\/span>\n        <span class=\"k\">public<\/span> <span class=\"k\">static<\/span> <span class=\"kt\">uint<\/span> <span class=\"nf\">DebuggingSetThrowOnFPError<\/span><span class=\"p\">(<\/span><span class=\"kt\">bool<\/span> <span class=\"n\">enable<\/span><span class=\"p\">)<\/span>\n        <span class=\"p\">{<\/span>\n            <span class=\"k\">const<\/span> <span class=\"n\">FloatingPointExceptionMask<\/span> <span class=\"n\">all<\/span> <span class=\"p\">=<\/span> <span class=\"n\">FloatingPointExceptionMask<\/span><span class=\"p\">.<\/span><span class=\"n\">_EM_INVALID<\/span> <span class=\"p\">|<\/span>\n                                      <span class=\"n\">FloatingPointExceptionMask<\/span><span class=\"p\">.<\/span><span class=\"n\">_EM_DENOMRAL<\/span> <span class=\"p\">|<\/span>\n                                      <span class=\"n\">FloatingPointExceptionMask<\/span><span class=\"p\">.<\/span><span class=\"n\">_EM_ZERODIVIDE<\/span> <span class=\"p\">|<\/span>\n                                      <span class=\"n\">FloatingPointExceptionMask<\/span><span class=\"p\">.<\/span><span class=\"n\">_EM_OVERFLOW<\/span> <span class=\"p\">|<\/span>\n                                      <span class=\"n\">FloatingPointExceptionMask<\/span><span class=\"p\">.<\/span><span class=\"n\">_EM_UNDERFLOW<\/span> <span class=\"p\">|<\/span>\n                                      <span class=\"n\">FloatingPointExceptionMask<\/span><span class=\"p\">.<\/span><span class=\"n\">_EM_INEXACT<\/span><span class=\"p\">;<\/span>\n            <span class=\"k\">return<\/span> <span class=\"nf\">_controlfp_s<\/span><span class=\"p\">(<\/span><span class=\"k\">out<\/span> <span class=\"n\">_<\/span><span class=\"p\">,<\/span> <span class=\"p\">(<\/span><span class=\"kt\">uint<\/span><span class=\"p\">)(<\/span><span class=\"n\">enable<\/span> <span class=\"p\">?<\/span> <span class=\"p\">~<\/span><span class=\"n\">all<\/span> <span class=\"p\">:<\/span> <span class=\"n\">all<\/span><span class=\"p\">),<\/span> <span class=\"n\">_MCW_EM<\/span><span class=\"p\">);<\/span>\n        <span class=\"p\">}<\/span>\n\n        <span class=\"p\">[<\/span><span class=\"nf\">CLSCompliant<\/span><span class=\"p\">(<\/span><span class=\"k\">false<\/span><span class=\"p\">)]<\/span>\n        <span class=\"k\">public<\/span> <span class=\"k\">static<\/span> <span class=\"n\">FloatingPointExceptionMask<\/span> <span class=\"nf\">DebuggingGetThrowOnFPError<\/span><span class=\"p\">()<\/span>\n        <span class=\"p\">{<\/span>\n            <span class=\"kt\">uint<\/span> <span class=\"n\">res<\/span> <span class=\"p\">=<\/span> <span class=\"nf\">_controlfp_s<\/span><span class=\"p\">(<\/span><span class=\"k\">out<\/span> <span class=\"kt\">var<\/span> <span class=\"n\">current<\/span><span class=\"p\">,<\/span> <span class=\"m\">0<\/span><span class=\"p\">,<\/span> <span class=\"m\">0<\/span><span class=\"p\">);<\/span>\n            <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">res<\/span> <span class=\"p\">!=<\/span> <span class=\"m\">0<\/span><span class=\"p\">)<\/span>\n            <span class=\"p\">{<\/span>\n                <span class=\"n\">Console<\/span><span class=\"p\">.<\/span><span class=\"nf\">WriteLine<\/span><span class=\"p\">(<\/span><span class=\"s\">\"Could not get FP status {0}.\"<\/span><span class=\"p\">,<\/span> <span class=\"n\">res<\/span><span class=\"p\">);<\/span>\n                <span class=\"k\">return<\/span> <span class=\"m\">0<\/span><span class=\"p\">;<\/span>\n            <span class=\"p\">}<\/span>\n\n            <span class=\"k\">return<\/span> <span class=\"p\">(<\/span><span class=\"n\">FloatingPointExceptionMask<\/span><span class=\"p\">)(<\/span><span class=\"n\">current<\/span> <span class=\"p\">&amp;<\/span> <span class=\"n\">_MCW_EM<\/span><span class=\"p\">);<\/span>\n        <span class=\"p\">}<\/span>\n\n        <span class=\"k\">static<\/span> <span class=\"k\">void<\/span> <span class=\"nf\">DoNaN<\/span><span class=\"p\">()<\/span>\n        <span class=\"p\">{<\/span>\n            <span class=\"kt\">double<\/span> <span class=\"n\">x<\/span> <span class=\"p\">=<\/span> <span class=\"m\">0.0<\/span><span class=\"p\">;<\/span>\n            <span class=\"kt\">double<\/span> <span class=\"n\">y<\/span> <span class=\"p\">=<\/span> <span class=\"n\">x<\/span> <span class=\"p\">\/<\/span> <span class=\"m\">0.0<\/span><span class=\"p\">;<\/span>\n            <span class=\"n\">Console<\/span><span class=\"p\">.<\/span><span class=\"nf\">WriteLine<\/span><span class=\"p\">(<\/span><span class=\"s\">\"Double.IsNan: \"<\/span> <span class=\"p\">+<\/span> <span class=\"n\">Double<\/span><span class=\"p\">.<\/span><span class=\"nf\">IsNaN<\/span><span class=\"p\">(<\/span><span class=\"n\">y<\/span><span class=\"p\">));<\/span>\n        <span class=\"p\">}<\/span>\n\n        <span class=\"k\">static<\/span> <span class=\"k\">void<\/span> <span class=\"nf\">Main<\/span><span class=\"p\">(<\/span><span class=\"kt\">string<\/span><span class=\"p\">[]<\/span> <span class=\"n\">args<\/span><span class=\"p\">)<\/span>\n        <span class=\"p\">{<\/span>\n            <span class=\"n\">Console<\/span><span class=\"p\">.<\/span><span class=\"nf\">WriteLine<\/span><span class=\"p\">(<\/span><span class=\"s\">\"BEFORE 1:\"<\/span> <span class=\"p\">+<\/span> <span class=\"nf\">DebuggingGetThrowOnFPError<\/span><span class=\"p\">());<\/span>\n            <span class=\"nf\">DoNaN<\/span><span class=\"p\">();<\/span>\n            <span class=\"n\">Console<\/span><span class=\"p\">.<\/span><span class=\"nf\">WriteLine<\/span><span class=\"p\">(<\/span><span class=\"nf\">DebuggingSetThrowOnFPError<\/span><span class=\"p\">(<\/span><span class=\"k\">true<\/span><span class=\"p\">));<\/span>\n\n            <span class=\"n\">Console<\/span><span class=\"p\">.<\/span><span class=\"nf\">WriteLine<\/span><span class=\"p\">(<\/span><span class=\"s\">\"BEFORE 2: \"<\/span> <span class=\"p\">+<\/span> <span class=\"nf\">DebuggingGetThrowOnFPError<\/span><span class=\"p\">());<\/span>\n            <span class=\"k\">try<\/span>\n            <span class=\"p\">{<\/span>\n                <span class=\"nf\">DoNaN<\/span><span class=\"p\">();<\/span>\n            <span class=\"p\">}<\/span>\n            <span class=\"k\">catch<\/span> <span class=\"p\">(<\/span><span class=\"n\">ArithmeticException<\/span> <span class=\"n\">ex<\/span><span class=\"p\">)<\/span>\n            <span class=\"p\">{<\/span>\n                <span class=\"n\">Console<\/span><span class=\"p\">.<\/span><span class=\"nf\">WriteLine<\/span><span class=\"p\">(<\/span><span class=\"s\">\"GOT AN ArithmeticException: \"<\/span> <span class=\"p\">+<\/span> <span class=\"n\">ex<\/span><span class=\"p\">.<\/span><span class=\"n\">Message<\/span><span class=\"p\">);<\/span>\n            <span class=\"p\">}<\/span>\n            <span class=\"k\">catch<\/span> <span class=\"p\">(<\/span><span class=\"n\">SEHException<\/span> <span class=\"n\">ex<\/span><span class=\"p\">)<\/span>\n            <span class=\"p\">{<\/span>\n                <span class=\"n\">Console<\/span><span class=\"p\">.<\/span><span class=\"nf\">WriteLine<\/span><span class=\"p\">(<\/span><span class=\"s\">\"GOT AN SEHException: \"<\/span> <span class=\"p\">+<\/span> <span class=\"n\">ex<\/span><span class=\"p\">.<\/span><span class=\"n\">Message<\/span><span class=\"p\">);<\/span>\n            <span class=\"p\">}<\/span>\n\n            <span class=\"c1\">\/\/ On x86 this will reset the FPU state to what the CLR expects it to be<\/span>\n            <span class=\"c1\">\/\/ (i.e. all exceptions are disabled.) <\/span>\n            <span class=\"k\">try<\/span> <span class=\"p\">{<\/span> <span class=\"k\">throw<\/span> <span class=\"k\">new<\/span> <span class=\"nf\">Exception<\/span><span class=\"p\">(<\/span><span class=\"s\">\"STOP\"<\/span><span class=\"p\">);<\/span> <span class=\"p\">}<\/span> <span class=\"k\">catch<\/span> <span class=\"p\">{<\/span> <span class=\"p\">}<\/span>\n\n            <span class=\"n\">Console<\/span><span class=\"p\">.<\/span><span class=\"nf\">WriteLine<\/span><span class=\"p\">(<\/span><span class=\"s\">\"BEFORE 3: \"<\/span> <span class=\"p\">+<\/span> <span class=\"nf\">DebuggingGetThrowOnFPError<\/span><span class=\"p\">());<\/span>\n            <span class=\"k\">try<\/span>\n            <span class=\"p\">{<\/span>\n                <span class=\"nf\">DoNaN<\/span><span class=\"p\">();<\/span>\n            <span class=\"p\">}<\/span>\n            <span class=\"k\">catch<\/span> <span class=\"p\">(<\/span><span class=\"n\">ArithmeticException<\/span> <span class=\"n\">ex<\/span><span class=\"p\">)<\/span>\n            <span class=\"p\">{<\/span>\n                <span class=\"n\">Console<\/span><span class=\"p\">.<\/span><span class=\"nf\">WriteLine<\/span><span class=\"p\">(<\/span><span class=\"s\">\"GOT AN ArithmeticException: \"<\/span> <span class=\"p\">+<\/span> <span class=\"n\">ex<\/span><span class=\"p\">.<\/span><span class=\"n\">Message<\/span><span class=\"p\">);<\/span>\n            <span class=\"p\">}<\/span>\n            <span class=\"k\">catch<\/span> <span class=\"p\">(<\/span><span class=\"n\">SEHException<\/span> <span class=\"n\">ex<\/span><span class=\"p\">)<\/span>\n            <span class=\"p\">{<\/span>\n                <span class=\"n\">Console<\/span><span class=\"p\">.<\/span><span class=\"nf\">WriteLine<\/span><span class=\"p\">(<\/span><span class=\"s\">\"GOT AN SEHException: \"<\/span> <span class=\"p\">+<\/span> <span class=\"n\">ex<\/span><span class=\"p\">.<\/span><span class=\"n\">Message<\/span><span class=\"p\">);<\/span>\n            <span class=\"p\">}<\/span>\n\n            <span class=\"n\">Console<\/span><span class=\"p\">.<\/span><span class=\"nf\">WriteLine<\/span><span class=\"p\">(<\/span><span class=\"nf\">DebuggingSetThrowOnFPError<\/span><span class=\"p\">(<\/span><span class=\"k\">false<\/span><span class=\"p\">));<\/span>\n\n            <span class=\"n\">Console<\/span><span class=\"p\">.<\/span><span class=\"nf\">WriteLine<\/span><span class=\"p\">(<\/span><span class=\"s\">\"BEFORE 4: \"<\/span> <span class=\"p\">+<\/span> <span class=\"nf\">DebuggingGetThrowOnFPError<\/span><span class=\"p\">());<\/span>\n            <span class=\"nf\">DoNaN<\/span><span class=\"p\">();<\/span>\n        <span class=\"p\">}<\/span>\n    <span class=\"p\">}<\/span>\n<span class=\"p\">}<\/span><\/code><\/pre><\/figure>\n\n<p>The output for an X64 build is:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>BEFORE 1:_EM_INEXACT, _EM_UNDERFLOW, _EM_OVERFLOW, _EM_ZERODIVIDE, _EM_INVALID, _EM_DENOMRAL\nDouble.IsNan: True\n0\nBEFORE 2: _EM_DENOMRAL\nGOT AN ArithmeticException: Overflow or underflow in the arithmetic operation.\nBEFORE 3: _EM_DENOMRAL\nGOT AN ArithmeticException: Overflow or underflow in the arithmetic operation.\n0\nBEFORE 4: _EM_INEXACT, _EM_UNDERFLOW, _EM_OVERFLOW, _EM_ZERODIVIDE, _EM_INVALID, _EM_DENOMRAL\nDouble.IsNan: True\n<\/code><\/pre><\/div><\/div>\n\n<p>As you can see the 64 CLR converts NaN into <code class=\"language-plaintext highlighter-rouge\">System.ArithmeticException<\/code> (albeit with a slightly\nconfusing or generic message about \u201coverflow or underflow\u201d, which is not the FPU state in this\ncase.) Note also that the \u201cdummy\u201d throw\/catch has no influence on the FPU settings we forced on\nusing <code class=\"language-plaintext highlighter-rouge\">_controlfp_s<\/code>.<\/p>\n\n<p>The X86 build outputs:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>BEFORE 1:_EM_INEXACT, _EM_UNDERFLOW, _EM_OVERFLOW, _EM_ZERODIVIDE, _EM_INVALID, _EM_DENOMRAL\nDouble.IsNan: True\n0\nBEFORE 2: _EM_DENOMRAL\nGOT AN SEHException: External component has thrown an exception.\nBEFORE 3: _EM_INEXACT, _EM_UNDERFLOW, _EM_OVERFLOW, _EM_ZERODIVIDE, _EM_INVALID, _EM_DENOMRAL\nGOT AN SEHException: External component has thrown an exception.\n0\nBEFORE 4: _EM_INEXACT, _EM_UNDERFLOW, _EM_OVERFLOW, _EM_ZERODIVIDE, _EM_INVALID, _EM_DENOMRAL\nDouble.IsNan: True\n<\/code><\/pre><\/div><\/div>\n\n<p>Note that the kind of exception throw is an <code class=\"language-plaintext highlighter-rouge\">System.Runtime.InteropServices.SEHException<\/code>,\nwhich gives the whole thing a much more naughty feeling as it typically an indicator for a\nmuch more serious issue. As note that \u201cBEFORE 3\u201d outputs the default FPU settings again,\nthat is the dummy throw\/catch actually resets those settings inside the CLR (see this\ncomment https:\/\/stackoverflow.com\/a\/25206025\/21567 and the respective\n(source code)[https:\/\/github.com\/dotnet\/runtime\/blob\/master\/src\/coreclr\/src\/vm\/excep.cpp#L7857]).<\/p>\n\n<p>This alone makes to whole practice much more unreliable when looking for issues than in the\n64-bit build.<\/p>\n\n<p>All in all, the whole idea seems to be more of an academic exercise and probably not reliable\nenough to track down bugs. YMMV, of course.<\/p>\n\n<h2 id=\"second-attempt-watch-fpu-register-while-debugging\">Second attempt: watch FPU register while debugging<\/h2>\n\n<p>When the FPU encounters a NaN the status of some registers will change. In general, you should watch the <code class=\"language-plaintext highlighter-rouge\">MXCSR<\/code> register.\nTo do so, open from \u201cDebug \/ Windows\u201d the \u201cRegisters\u201d window, and make sure you select\n\u201cSSE\u201d from the context menu.<\/p>\n\n<p>When starting and hitting a breakpoint the registers window should look something like this:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>XMM0\u00a0=\u00a00000000000000000-000000008C9F9CE8 \n[...]\nXMM15\u00a0=\u00a00000000000000000-0000000000000000 \nMXCSR\u00a0=\u00a000001FA0 \n<\/code><\/pre><\/div><\/div>\n\n<p>Note the initial values of <code class=\"language-plaintext highlighter-rouge\">MXCSR<\/code> as <code class=\"language-plaintext highlighter-rouge\">0x00001FA0<\/code>. Now single step the region of code you suspect contains or produces the NaN value in question and watch those to registers.\nWhen you encounter the respective statement, their values change to <code class=\"language-plaintext highlighter-rouge\">MXCSR<\/code> <code class=\"language-plaintext highlighter-rouge\">0x00001FA1<\/code>.<\/p>\n\n<p>The <code class=\"language-plaintext highlighter-rouge\">MXCSR<\/code> register is only the place to look for .NET Core applications (32- and 64-bit) or 64-bit .NET full framework applications.\nWhen using 32-bit full .NET framework applications, the JIT compiler being used is not RyuJIT, but the legacy JIT.\nThat one doesn\u2019t use the SSE-instructions for floating point, but still the x87 FPU stack (see https:\/\/github.com\/dotnet\/roslyn\/issues\/7333#issuecomment-560197038),\nthus one would need to rather look at the <code class=\"language-plaintext highlighter-rouge\">STAT<\/code> register (initial value <code class=\"language-plaintext highlighter-rouge\">0x4020<\/code>, NaN value <code class=\"language-plaintext highlighter-rouge\">0x4021<\/code>; select \u201cFloating Point\u201d\nfrom the Registers window context menu to display the register set for the x86 FPU stack).<\/p>\n\n<p>This really does work reliably, but requires you to able to narrow down the location at least to some extend.<\/p>","author":{"name":{}},"category":[{"@attributes":{"term":"double"}},{"@attributes":{"term":"nan"}}],"summary":"Ok, sorry for the (bad) pun, but naming is hard. In this post I just wanted to dump some stuff I recently did in an attempt to figure out, were in a couple of dozen lines of code of double-arithmetic a Double.Nan occurred."},{"title":"Inheritable string localizers for ASP.NET Core","link":{"@attributes":{"href":"https:\/\/cklutz.github.io\/2019\/01\/06\/inheritable-string-localizers\/","rel":"alternate","type":"text\/html","title":"Inheritable string localizers for ASP.NET Core"}},"published":"2019-01-06T07:10:25+00:00","updated":"2019-01-06T07:10:25+00:00","id":"https:\/\/cklutz.github.io\/2019\/01\/06\/inheritable-string-localizers","content":"<h1 id=\"introduction\">Introduction<\/h1>\n\n<p>ASP.NET Core provides the new type of <code class=\"language-plaintext highlighter-rouge\">IStringLocalizer&lt;T&gt;<\/code> to deal with string resources\nand localization. There has been written a lot about this already and a simple search\nwill provide you with all background information you need - so I\u2019ll skip this here, only\ntalking about the basic bits where they are required to what is done and why.<\/p>\n\n<p>Usually, you would use a <code class=\"language-plaintext highlighter-rouge\">IStringLocalizer&lt;T&gt;<\/code> where <code class=\"language-plaintext highlighter-rouge\">T<\/code> is a type (say <code class=\"language-plaintext highlighter-rouge\">MyService<\/code>)\nand then also provide a <code class=\"language-plaintext highlighter-rouge\">.resx<\/code> file of the same name, including the culture name\nfor the translations (e.g. <code class=\"language-plaintext highlighter-rouge\">MyService.de-DE.resx<\/code>). Note that you specifically, don\u2019t\nneed to provide a resource file for the default language of your application.<\/p>\n\n<p>Let\u2019s look at the following example to make this more clear:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-c#\" data-lang=\"c#\"><span class=\"k\">public<\/span> <span class=\"k\">class<\/span> <span class=\"nc\">MyService<\/span> <span class=\"p\">:<\/span> <span class=\"n\">IMyService<\/span> \n<span class=\"p\">{<\/span>\n    <span class=\"k\">private<\/span> <span class=\"k\">readonly<\/span> <span class=\"n\">IStringLocalizer<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">MyService<\/span><span class=\"p\">&gt;<\/span> <span class=\"n\">m_strings<\/span><span class=\"p\">;<\/span>\n\n    <span class=\"k\">public<\/span> <span class=\"nf\">MyService<\/span><span class=\"p\">(<\/span><span class=\"n\">IStringLocalizer<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">MyService<\/span><span class=\"p\">&gt;<\/span> <span class=\"n\">strings<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"n\">m_strings<\/span> <span class=\"p\">=<\/span> <span class=\"n\">strings<\/span><span class=\"p\">;<\/span>\n    <span class=\"p\">}<\/span>\n\n    <span class=\"k\">public<\/span> <span class=\"kt\">string<\/span> <span class=\"nf\">GetResult<\/span><span class=\"p\">()<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"k\">return<\/span> <span class=\"n\">m_strings<\/span><span class=\"p\">[<\/span><span class=\"s\">\"The results\"<\/span><span class=\"p\">];<\/span>\n    <span class=\"p\">}<\/span>\n<span class=\"p\">}<\/span><\/code><\/pre><\/figure>\n\n<p>In the above you can see a couple of things:<\/p>\n\n<ul>\n  <li>The <code class=\"language-plaintext highlighter-rouge\">IStringLocalizer&lt;MyService&gt;<\/code> is usually provided by DI.<\/li>\n  <li>You get a string from the resources by using the index-operator.\nThe text you pass is used as the <em>key<\/em> into the resource file\nfor the \u201ccurrent\u201d culture. If it doesn\u2019t exist in the resource\nfile, the text passed is returned.<\/li>\n<\/ul>\n\n<h1 id=\"whats-the-problem-then\">What\u2019s the problem then?<\/h1>\n\n<p>For the following, there is one important thing to keep in mind:\nYou cannot create instances of <code class=\"language-plaintext highlighter-rouge\">IStringLocalizer&lt;T&gt;<\/code> directly;\nyou are expected to have them injected.<\/p>\n\n<p>That doesn\u2019t sound too bad, but consider the following example:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-c#\" data-lang=\"c#\"><span class=\"k\">internal<\/span> <span class=\"k\">static<\/span> <span class=\"k\">class<\/span> <span class=\"nc\">Helpers<\/span>\n<span class=\"p\">{<\/span>\n    <span class=\"c1\">\/\/ Helper method called by multiple implementations.<\/span>\n    <span class=\"k\">internal<\/span> <span class=\"k\">static<\/span> <span class=\"kt\">string<\/span> <span class=\"nf\">DoTheFoo<\/span><span class=\"p\">(<\/span><span class=\"n\">IStringLocalizer<\/span> <span class=\"n\">strings<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"k\">return<\/span> <span class=\"n\">strings<\/span><span class=\"p\">[<\/span><span class=\"s\">\"The foo was done.\"<\/span><span class=\"p\">];<\/span>\n    <span class=\"p\">}<\/span>\n<span class=\"p\">}<\/span><\/code><\/pre><\/figure>\n\n<p>So you have an helper method (static to make the case obvious that\nDI is possible) that is used by multiple other services or code.<\/p>\n\n<p>Where would you put the resources for this? You could put them\ninto <code class=\"language-plaintext highlighter-rouge\">IStringLocalizer&lt;SharedResources&gt;<\/code>, but simply defining\nan empty class called <code class=\"language-plaintext highlighter-rouge\">SharedResources<\/code> and <code class=\"language-plaintext highlighter-rouge\">SharedResources.&lt;culture&gt;.resx<\/code>\nthat goes with it.<\/p>\n\n<p>However, that doesn\u2019t scale too well in bigger projects:<\/p>\n\n<ul>\n  <li>You end up with a \u201cGod\u201d-resources sink that is probably not a good thing<\/li>\n  <li>You need to pass\/inject multiple <code class=\"language-plaintext highlighter-rouge\">IStringLocalizer&lt;T&gt;<\/code> into other classes<\/li>\n  <li>You need to <em>know<\/em> all the <code class=\"language-plaintext highlighter-rouge\">IStringLocalizer&lt;T&gt;<\/code> you need to inject to\nsatisfy all code that a service may call downstream.<\/li>\n<\/ul>\n\n<p>Especially the last point breaks encapsulation and is plain cumbersome to\nmaintain.<\/p>\n\n<p>For example, consider this:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-c#\" data-lang=\"c#\"><span class=\"k\">public<\/span> <span class=\"k\">class<\/span> <span class=\"nc\">MyService<\/span> <span class=\"p\">:<\/span> <span class=\"n\">IMyService<\/span> \n<span class=\"p\">{<\/span>\n    <span class=\"k\">public<\/span> <span class=\"nf\">MyService<\/span><span class=\"p\">(<\/span>\n        <span class=\"n\">IStringLocalizer<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">MyService<\/span><span class=\"p\">&gt;<\/span> <span class=\"n\">strings<\/span><span class=\"p\">,<\/span> <span class=\"c1\">\/\/ for the service itself<\/span>\n        <span class=\"n\">IStringLocalizer<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">IOHelpers<\/span><span class=\"p\">&gt;<\/span> <span class=\"n\">ioStrings<\/span><span class=\"p\">,<\/span> <span class=\"c1\">\/\/ for the IOHelpers tools<\/span>\n        <span class=\"n\">IStringLocalizer<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">SharedResources<\/span><span class=\"p\">&gt;<\/span> <span class=\"n\">sharedStrings<\/span> <span class=\"c1\">\/\/ generic stuff<\/span>\n        <span class=\"p\">)<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"c1\">\/\/ elided for brevity<\/span>\n    <span class=\"p\">}<\/span>\n<span class=\"p\">}<\/span><\/code><\/pre><\/figure>\n\n<h1 id=\"a-possible-solution\">A possible solution<\/h1>\n\n<h2 id=\"overview\">Overview<\/h2>\n\n<p>A possible solution to this is to only inject the on <code class=\"language-plaintext highlighter-rouge\">IStringLocalizer&lt;T&gt;<\/code>\ninto a service, the one that is directly \u201cbound\u201d to it. But then, have\nthat localizer name dependencies that should also be available.<\/p>\n\n<p>For exempale:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-c#\" data-lang=\"c#\"><span class=\"p\">[<\/span><span class=\"nf\">InheritStringLocalization<\/span><span class=\"p\">(<\/span><span class=\"k\">typeof<\/span><span class=\"p\">(<\/span><span class=\"n\">SharedResources<\/span><span class=\"p\">))]<\/span>\n<span class=\"p\">[<\/span><span class=\"nf\">InheritStringLocalization<\/span><span class=\"p\">(<\/span><span class=\"k\">typeof<\/span><span class=\"p\">(<\/span><span class=\"n\">IOHelpers<\/span><span class=\"p\">))]<\/span>\n<span class=\"k\">public<\/span> <span class=\"k\">class<\/span> <span class=\"nc\">MyServiceResources<\/span>\n<span class=\"p\">{<\/span>\n<span class=\"p\">}<\/span>\n\n<span class=\"c1\">\/\/ --<\/span>\n\n<span class=\"k\">public<\/span> <span class=\"k\">class<\/span> <span class=\"nc\">MyService<\/span> <span class=\"p\">:<\/span> <span class=\"n\">IMyService<\/span> \n<span class=\"p\">{<\/span>\n    <span class=\"k\">public<\/span> <span class=\"nf\">MyService<\/span><span class=\"p\">(<\/span><span class=\"n\">IStringLocalizer<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">MyServiceResources<\/span><span class=\"p\">&gt;<\/span> <span class=\"n\">strings<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"c1\">\/\/ elided for brevity<\/span>\n    <span class=\"p\">}<\/span>\n<span class=\"p\">}<\/span><\/code><\/pre><\/figure>\n\n<p>OK, you still have to note all the dependencies here, but at least not\non the constructor directly. Additionally, if you define a proper\nresource hierarchy for your project you can make things a little\nmore obvious.<\/p>\n\n<p>For example, in the project where I employed this technique basically\nall code was structured like this:<\/p>\n\n<ul>\n  <li>Controllers\n    <ul>\n      <li>Services\n        <ul>\n          <li>Utility classes<\/li>\n        <\/ul>\n      <\/li>\n    <\/ul>\n  <\/li>\n<\/ul>\n\n<p>Each level could have resources so that by default every controller\nresource would only have <code class=\"language-plaintext highlighter-rouge\">InheritStringLocalization<\/code>-attributes for\nthe services it uses, and every service resource would only have them\nfor the utility classes it uses. In other words, each level only has\nexplicit dependencies to the immediate next level. Something which\nis <em>not possible<\/em> if you use the plain DI-approach that the framework\nprovides.<\/p>\n\n<h2 id=\"how-does-it-work\">How does it work<\/h2>\n\n<p>The <code class=\"language-plaintext highlighter-rouge\">InheritStringLocalization<\/code>-attribute basically says that when\nattempting to resolve a resource from the type it is applied on, the\ntype specified by the attribute should also be consider, if the\nresource is not found in the type itself. It does that down the\nchain. So if the type specified by the attribute does not contain\nthe resource, but has an <code class=\"language-plaintext highlighter-rouge\">InheritStringLocalization<\/code>-attribute itself,\nthis type is searched, and so on.<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-c#\" data-lang=\"c#\"><span class=\"p\">[<\/span><span class=\"nf\">InheritStringLocalization<\/span><span class=\"p\">(<\/span><span class=\"k\">typeof<\/span><span class=\"p\">(<\/span><span class=\"n\">CheckAfterResourceType<\/span><span class=\"p\">))]<\/span>\n<span class=\"k\">public<\/span> <span class=\"k\">class<\/span> <span class=\"nc\">ResourceType<\/span> <span class=\"p\">{<\/span> <span class=\"p\">}<\/span>\n\n<span class=\"p\">[<\/span><span class=\"nf\">InheritStringLocalization<\/span><span class=\"p\">(<\/span><span class=\"k\">typeof<\/span><span class=\"p\">(<\/span><span class=\"n\">CheckAfterResourceType2<\/span><span class=\"p\">))]<\/span>\n<span class=\"k\">public<\/span> <span class=\"k\">class<\/span> <span class=\"nc\">CheckAfterResourceType<\/span> <span class=\"p\">{<\/span> <span class=\"p\">}<\/span>\n\n<span class=\"k\">public<\/span> <span class=\"k\">class<\/span> <span class=\"nc\">CheckAfterResourceType2<\/span> <span class=\"p\">{<\/span> <span class=\"p\">}<\/span><\/code><\/pre><\/figure>\n\n<p>If multiple attributes are applied to a resource type, the first that\ncontains the resource is used. The search order can be influenced by\nan optional <code class=\"language-plaintext highlighter-rouge\">Priority<\/code>-parameter for the attribute:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-c#\" data-lang=\"c#\"><span class=\"p\">[<\/span><span class=\"nf\">InheritStringLocalization<\/span><span class=\"p\">(<\/span><span class=\"k\">typeof<\/span><span class=\"p\">(<\/span><span class=\"n\">SharedResources<\/span><span class=\"p\">),<\/span> <span class=\"n\">Priority<\/span><span class=\"p\">=<\/span><span class=\"m\">2<\/span><span class=\"p\">)]<\/span>\n<span class=\"p\">[<\/span><span class=\"nf\">InheritStringLocalization<\/span><span class=\"p\">(<\/span><span class=\"k\">typeof<\/span><span class=\"p\">(<\/span><span class=\"n\">IOHelpers<\/span><span class=\"p\">),<\/span> <span class=\"n\">Priority<\/span><span class=\"p\">=<\/span><span class=\"m\">1<\/span><span class=\"p\">)]<\/span>\n<span class=\"k\">public<\/span> <span class=\"k\">class<\/span> <span class=\"nc\">MyServiceResources<\/span>\n<span class=\"p\">{<\/span>\n<span class=\"p\">}<\/span><\/code><\/pre><\/figure>\n\n<p>Lower priority values are considered first.<\/p>\n\n<p>Finally note, that using this approach one can create a resource\ntype that serves merely as a bundle, but does not define any\nresources itself:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-c#\" data-lang=\"c#\"><span class=\"c1\">\/\/ No SystemResources.*.resx exists<\/span>\n<span class=\"p\">[<\/span><span class=\"nf\">InheritStringLocalization<\/span><span class=\"p\">(<\/span><span class=\"k\">typeof<\/span><span class=\"p\">(<\/span><span class=\"n\">IOSystemResources<\/span><span class=\"p\">))]<\/span>\n<span class=\"p\">[<\/span><span class=\"nf\">InheritStringLocalization<\/span><span class=\"p\">(<\/span><span class=\"k\">typeof<\/span><span class=\"p\">(<\/span><span class=\"n\">UISystemResources<\/span><span class=\"p\">))]<\/span>\n<span class=\"k\">public<\/span> <span class=\"k\">class<\/span> <span class=\"nc\">SystemResources<\/span> <span class=\"p\">{<\/span> <span class=\"p\">}<\/span>\n\n<span class=\"c1\">\/\/ IOSystemResources.*.resx exists<\/span>\n<span class=\"k\">public<\/span> <span class=\"k\">class<\/span> <span class=\"nc\">IOSystemResources<\/span> <span class=\"p\">{<\/span> <span class=\"p\">}<\/span>\n\n<span class=\"c1\">\/\/ UISystemResources.*.resx exists<\/span>\n<span class=\"k\">public<\/span> <span class=\"k\">class<\/span> <span class=\"nc\">UISystemResources<\/span> <span class=\"p\">{<\/span> <span class=\"p\">}<\/span><\/code><\/pre><\/figure>\n\n<h2 id=\"implementation\">Implementation<\/h2>\n\n<h3 id=\"inheritstringlocalizationattribute\">InheritStringLocalizationAttribute<\/h3>\n\n<p>This is the attribute that is applied to resource types to indicate that they should\n\u201cinherit\u201d resources from the specified \u201cInheritFrom\u201d type.<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-c#\" data-lang=\"c#\"><span class=\"p\">[<\/span><span class=\"nf\">AttributeUsage<\/span><span class=\"p\">(<\/span><span class=\"n\">AttributeTargets<\/span><span class=\"p\">.<\/span><span class=\"n\">Class<\/span><span class=\"p\">,<\/span> <span class=\"n\">AllowMultiple<\/span> <span class=\"p\">=<\/span> <span class=\"k\">true<\/span><span class=\"p\">)]<\/span>\n<span class=\"k\">public<\/span> <span class=\"k\">sealed<\/span> <span class=\"k\">class<\/span> <span class=\"nc\">InheritStringLocalizationAttribute<\/span> <span class=\"p\">:<\/span> <span class=\"n\">Attribute<\/span>\n<span class=\"p\">{<\/span>\n    <span class=\"k\">public<\/span> <span class=\"nf\">InheritStringLocalizationAttribute<\/span><span class=\"p\">(<\/span><span class=\"n\">Type<\/span> <span class=\"n\">inheritFrom<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"n\">InheritFrom<\/span> <span class=\"p\">=<\/span> <span class=\"n\">inheritFrom<\/span> <span class=\"p\">??<\/span> <span class=\"k\">throw<\/span> <span class=\"k\">new<\/span> <span class=\"nf\">ArgumentNullException<\/span><span class=\"p\">(<\/span><span class=\"k\">nameof<\/span><span class=\"p\">(<\/span><span class=\"n\">inheritFrom<\/span><span class=\"p\">));<\/span>\n    <span class=\"p\">}<\/span>\n    <span class=\"k\">public<\/span> <span class=\"n\">Type<\/span> <span class=\"n\">InheritFrom<\/span> <span class=\"p\">{<\/span> <span class=\"k\">get<\/span><span class=\"p\">;<\/span> <span class=\"p\">}<\/span>\n    <span class=\"k\">public<\/span> <span class=\"kt\">int<\/span> <span class=\"n\">Priority<\/span> <span class=\"p\">{<\/span> <span class=\"k\">get<\/span><span class=\"p\">;<\/span> <span class=\"k\">set<\/span><span class=\"p\">;<\/span> <span class=\"p\">}<\/span>\n<span class=\"p\">}<\/span><\/code><\/pre><\/figure>\n\n<h2 id=\"multistringlocalizer\">MultiStringLocalizer<\/h2>\n\n<p>This is the implementation behind an <code class=\"language-plaintext highlighter-rouge\">IStringLocalizer&lt;T&gt;<\/code> that uses a <code class=\"language-plaintext highlighter-rouge\">T<\/code> that has at least\n<code class=\"language-plaintext highlighter-rouge\">InheritStringLocalizationAttribute<\/code> applied.<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-c#\" data-lang=\"c#\"><span class=\"k\">public<\/span> <span class=\"k\">class<\/span> <span class=\"nc\">MultiStringLocalizer<\/span> <span class=\"p\">:<\/span> <span class=\"n\">IStringLocalizer<\/span>\n<span class=\"p\">{<\/span>\n    <span class=\"k\">private<\/span> <span class=\"k\">readonly<\/span> <span class=\"n\">List<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">IStringLocalizer<\/span><span class=\"p\">&gt;<\/span> <span class=\"n\">m_localizers<\/span><span class=\"p\">;<\/span>\n    <span class=\"k\">private<\/span> <span class=\"k\">readonly<\/span> <span class=\"n\">ILogger<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">MultiStringLocalizer<\/span><span class=\"p\">&gt;<\/span> <span class=\"n\">m_logger<\/span><span class=\"p\">;<\/span>\n    <span class=\"k\">private<\/span> <span class=\"k\">readonly<\/span> <span class=\"n\">CultureInfo<\/span> <span class=\"n\">m_cultureInfo<\/span><span class=\"p\">;<\/span>\n\n    <span class=\"k\">public<\/span> <span class=\"nf\">MultiStringLocalizer<\/span><span class=\"p\">(<\/span><span class=\"n\">List<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">IStringLocalizer<\/span><span class=\"p\">&gt;<\/span> <span class=\"n\">localizers<\/span><span class=\"p\">,<\/span> <span class=\"n\">ILogger<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">MultiStringLocalizer<\/span><span class=\"p\">&gt;<\/span> <span class=\"n\">logger<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">localizers<\/span> <span class=\"p\">==<\/span> <span class=\"k\">null<\/span><span class=\"p\">)<\/span>\n            <span class=\"k\">throw<\/span> <span class=\"k\">new<\/span> <span class=\"nf\">ArgumentNullException<\/span><span class=\"p\">(<\/span><span class=\"k\">nameof<\/span><span class=\"p\">(<\/span><span class=\"n\">localizers<\/span><span class=\"p\">));<\/span>\n        <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">localizers<\/span><span class=\"p\">.<\/span><span class=\"n\">Count<\/span> <span class=\"p\">==<\/span> <span class=\"m\">0<\/span><span class=\"p\">)<\/span>\n            <span class=\"k\">throw<\/span> <span class=\"k\">new<\/span> <span class=\"nf\">ArgumentException<\/span><span class=\"p\">(<\/span><span class=\"s\">\"Empty not supported\"<\/span><span class=\"p\">,<\/span> <span class=\"k\">nameof<\/span><span class=\"p\">(<\/span><span class=\"n\">localizers<\/span><span class=\"p\">));<\/span>\n        <span class=\"n\">m_localizers<\/span> <span class=\"p\">=<\/span> <span class=\"n\">localizers<\/span><span class=\"p\">;<\/span>\n        <span class=\"n\">m_logger<\/span> <span class=\"p\">=<\/span> <span class=\"n\">logger<\/span> <span class=\"p\">??<\/span> <span class=\"n\">NullLogger<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">MultiStringLocalizer<\/span><span class=\"p\">&gt;.<\/span><span class=\"n\">Instance<\/span><span class=\"p\">;<\/span>\n    <span class=\"p\">}<\/span>\n\n    <span class=\"k\">private<\/span> <span class=\"nf\">MultiStringLocalizer<\/span><span class=\"p\">(<\/span><span class=\"n\">List<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">IStringLocalizer<\/span><span class=\"p\">&gt;<\/span> <span class=\"n\">localizers<\/span><span class=\"p\">,<\/span> <span class=\"n\">ILogger<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">MultiStringLocalizer<\/span><span class=\"p\">&gt;<\/span> <span class=\"n\">logger<\/span><span class=\"p\">,<\/span> <span class=\"n\">CultureInfo<\/span> <span class=\"n\">cultureInfo<\/span><span class=\"p\">)<\/span>\n        <span class=\"p\">:<\/span> <span class=\"k\">this<\/span><span class=\"p\">(<\/span><span class=\"n\">localizers<\/span><span class=\"p\">.<\/span><span class=\"nf\">Select<\/span><span class=\"p\">(<\/span><span class=\"n\">l<\/span> <span class=\"p\">=&gt;<\/span> <span class=\"n\">l<\/span><span class=\"p\">.<\/span><span class=\"nf\">WithCulture<\/span><span class=\"p\">(<\/span><span class=\"n\">cultureInfo<\/span><span class=\"p\">)).<\/span><span class=\"nf\">ToList<\/span><span class=\"p\">(),<\/span> <span class=\"n\">logger<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"n\">m_cultureInfo<\/span> <span class=\"p\">=<\/span> <span class=\"n\">cultureInfo<\/span><span class=\"p\">;<\/span>\n    <span class=\"p\">}<\/span>\n\n    <span class=\"k\">public<\/span> <span class=\"n\">IEnumerable<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">LocalizedString<\/span><span class=\"p\">&gt;<\/span> <span class=\"nf\">GetAllStrings<\/span><span class=\"p\">(<\/span><span class=\"kt\">bool<\/span> <span class=\"n\">includeParentCultures<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"kt\">var<\/span> <span class=\"n\">result<\/span> <span class=\"p\">=<\/span> <span class=\"k\">new<\/span> <span class=\"n\">Dictionary<\/span><span class=\"p\">&lt;<\/span><span class=\"kt\">string<\/span><span class=\"p\">,<\/span> <span class=\"n\">LocalizedString<\/span><span class=\"p\">&gt;();<\/span>\n        <span class=\"k\">foreach<\/span> <span class=\"p\">(<\/span><span class=\"kt\">var<\/span> <span class=\"n\">localizer<\/span> <span class=\"k\">in<\/span> <span class=\"n\">m_localizers<\/span><span class=\"p\">)<\/span>\n        <span class=\"p\">{<\/span>\n            <span class=\"k\">foreach<\/span> <span class=\"p\">(<\/span><span class=\"kt\">var<\/span> <span class=\"n\">entry<\/span> <span class=\"k\">in<\/span> <span class=\"n\">localizer<\/span><span class=\"p\">.<\/span><span class=\"nf\">GetAllStrings<\/span><span class=\"p\">(<\/span><span class=\"n\">includeParentCultures<\/span><span class=\"p\">))<\/span>\n            <span class=\"p\">{<\/span>\n                <span class=\"k\">if<\/span> <span class=\"p\">(!<\/span><span class=\"n\">result<\/span><span class=\"p\">.<\/span><span class=\"nf\">ContainsKey<\/span><span class=\"p\">(<\/span><span class=\"n\">entry<\/span><span class=\"p\">.<\/span><span class=\"n\">Name<\/span><span class=\"p\">))<\/span>\n                <span class=\"p\">{<\/span>\n                    <span class=\"n\">result<\/span><span class=\"p\">.<\/span><span class=\"nf\">Add<\/span><span class=\"p\">(<\/span><span class=\"n\">entry<\/span><span class=\"p\">.<\/span><span class=\"n\">Name<\/span><span class=\"p\">,<\/span> <span class=\"n\">entry<\/span><span class=\"p\">);<\/span>\n                <span class=\"p\">}<\/span>\n            <span class=\"p\">}<\/span>\n        <span class=\"p\">}<\/span>\n\n        <span class=\"k\">return<\/span> <span class=\"n\">result<\/span><span class=\"p\">.<\/span><span class=\"n\">Values<\/span><span class=\"p\">;<\/span>\n    <span class=\"p\">}<\/span>\n\n    <span class=\"k\">public<\/span> <span class=\"n\">IStringLocalizer<\/span> <span class=\"nf\">WithCulture<\/span><span class=\"p\">(<\/span><span class=\"n\">CultureInfo<\/span> <span class=\"n\">culture<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">culture<\/span> <span class=\"p\">==<\/span> <span class=\"k\">null<\/span><span class=\"p\">)<\/span>\n        <span class=\"p\">{<\/span>\n            <span class=\"k\">return<\/span> <span class=\"k\">new<\/span> <span class=\"nf\">MultiStringLocalizer<\/span><span class=\"p\">(<\/span><span class=\"n\">m_localizers<\/span><span class=\"p\">.<\/span><span class=\"nf\">ToList<\/span><span class=\"p\">(),<\/span> <span class=\"n\">m_logger<\/span><span class=\"p\">);<\/span>\n        <span class=\"p\">}<\/span>\n\n        <span class=\"k\">return<\/span> <span class=\"k\">new<\/span> <span class=\"nf\">MultiStringLocalizer<\/span><span class=\"p\">(<\/span><span class=\"n\">m_localizers<\/span><span class=\"p\">,<\/span> <span class=\"n\">m_logger<\/span><span class=\"p\">,<\/span> <span class=\"n\">culture<\/span><span class=\"p\">);<\/span>\n    <span class=\"p\">}<\/span>\n\n    <span class=\"k\">private<\/span> <span class=\"k\">void<\/span> <span class=\"nf\">OnLogAttempt<\/span><span class=\"p\">(<\/span><span class=\"n\">IStringLocalizer<\/span> <span class=\"n\">localizer<\/span><span class=\"p\">,<\/span> <span class=\"n\">LocalizedString<\/span> <span class=\"n\">result<\/span><span class=\"p\">,<\/span> <span class=\"n\">CultureInfo<\/span> <span class=\"n\">cultureInfo<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"kt\">var<\/span> <span class=\"n\">keyCulture<\/span> <span class=\"p\">=<\/span> <span class=\"n\">cultureInfo<\/span> <span class=\"p\">??<\/span> <span class=\"n\">CultureInfo<\/span><span class=\"p\">.<\/span><span class=\"n\">CurrentUICulture<\/span><span class=\"p\">;<\/span>\n        <span class=\"k\">if<\/span> <span class=\"p\">(!<\/span><span class=\"n\">result<\/span><span class=\"p\">.<\/span><span class=\"n\">ResourceNotFound<\/span><span class=\"p\">)<\/span>\n        <span class=\"p\">{<\/span>\n            <span class=\"n\">m_logger<\/span><span class=\"p\">.<\/span><span class=\"nf\">LogDebug<\/span><span class=\"p\">(<\/span><span class=\"s\">$\"<\/span><span class=\"p\">{<\/span><span class=\"n\">localizer<\/span><span class=\"p\">.<\/span><span class=\"nf\">GetType<\/span><span class=\"p\">()}<\/span><span class=\"s\"> found '<\/span><span class=\"p\">{<\/span><span class=\"n\">result<\/span><span class=\"p\">.<\/span><span class=\"n\">Name<\/span><span class=\"p\">}<\/span><span class=\"s\">' in '<\/span><span class=\"p\">{<\/span><span class=\"n\">result<\/span><span class=\"p\">.<\/span><span class=\"n\">SearchedLocation<\/span><span class=\"p\">}<\/span><span class=\"s\">' with culture '<\/span><span class=\"p\">{<\/span><span class=\"n\">keyCulture<\/span><span class=\"p\">}<\/span><span class=\"s\">'\"<\/span><span class=\"p\">);<\/span>\n        <span class=\"p\">}<\/span>\n        <span class=\"k\">else<\/span>\n        <span class=\"p\">{<\/span>\n            <span class=\"n\">m_logger<\/span><span class=\"p\">.<\/span><span class=\"nf\">LogDebug<\/span><span class=\"p\">(<\/span><span class=\"s\">$\"<\/span><span class=\"p\">{<\/span><span class=\"n\">localizer<\/span><span class=\"p\">.<\/span><span class=\"nf\">GetType<\/span><span class=\"p\">()}<\/span><span class=\"s\"> searched for '<\/span><span class=\"p\">{<\/span><span class=\"n\">result<\/span><span class=\"p\">.<\/span><span class=\"n\">Name<\/span><span class=\"p\">}<\/span><span class=\"s\">' in '<\/span><span class=\"p\">{<\/span><span class=\"n\">result<\/span><span class=\"p\">.<\/span><span class=\"n\">SearchedLocation<\/span><span class=\"p\">}<\/span><span class=\"s\">' with culture '<\/span><span class=\"p\">{<\/span><span class=\"n\">keyCulture<\/span><span class=\"p\">}<\/span><span class=\"s\">'\"<\/span><span class=\"p\">);<\/span>\n        <span class=\"p\">}<\/span>\n    <span class=\"p\">}<\/span>\n\n    <span class=\"k\">public<\/span> <span class=\"n\">LocalizedString<\/span> <span class=\"k\">this<\/span><span class=\"p\">[<\/span><span class=\"kt\">string<\/span> <span class=\"n\">name<\/span><span class=\"p\">]<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"k\">get<\/span>\n        <span class=\"p\">{<\/span>\n            <span class=\"n\">LocalizedString<\/span> <span class=\"n\">s<\/span> <span class=\"p\">=<\/span> <span class=\"k\">null<\/span><span class=\"p\">;<\/span>\n            <span class=\"k\">foreach<\/span> <span class=\"p\">(<\/span><span class=\"kt\">var<\/span> <span class=\"n\">localizer<\/span> <span class=\"k\">in<\/span> <span class=\"n\">m_localizers<\/span><span class=\"p\">)<\/span>\n            <span class=\"p\">{<\/span>\n                <span class=\"n\">s<\/span> <span class=\"p\">=<\/span> <span class=\"n\">localizer<\/span><span class=\"p\">[<\/span><span class=\"n\">name<\/span><span class=\"p\">];<\/span>\n                <span class=\"k\">if<\/span> <span class=\"p\">(!<\/span><span class=\"n\">s<\/span><span class=\"p\">.<\/span><span class=\"n\">ResourceNotFound<\/span><span class=\"p\">)<\/span>\n                <span class=\"p\">{<\/span>\n                    <span class=\"nf\">OnLogAttempt<\/span><span class=\"p\">(<\/span><span class=\"n\">localizer<\/span><span class=\"p\">,<\/span> <span class=\"n\">s<\/span><span class=\"p\">,<\/span> <span class=\"n\">m_cultureInfo<\/span><span class=\"p\">);<\/span>\n                    <span class=\"k\">return<\/span> <span class=\"n\">s<\/span><span class=\"p\">;<\/span>\n                <span class=\"p\">}<\/span>\n\n                <span class=\"nf\">OnLogAttempt<\/span><span class=\"p\">(<\/span><span class=\"n\">localizer<\/span><span class=\"p\">,<\/span> <span class=\"n\">s<\/span><span class=\"p\">,<\/span> <span class=\"n\">m_cultureInfo<\/span><span class=\"p\">);<\/span>\n            <span class=\"p\">}<\/span>\n            <span class=\"n\">Debug<\/span><span class=\"p\">.<\/span><span class=\"nf\">Assert<\/span><span class=\"p\">(<\/span><span class=\"n\">s<\/span> <span class=\"p\">!=<\/span> <span class=\"k\">null<\/span><span class=\"p\">);<\/span>\n            <span class=\"k\">return<\/span> <span class=\"n\">s<\/span><span class=\"p\">;<\/span>\n        <span class=\"p\">}<\/span>\n    <span class=\"p\">}<\/span>\n\n    <span class=\"k\">public<\/span> <span class=\"n\">LocalizedString<\/span> <span class=\"k\">this<\/span><span class=\"p\">[<\/span><span class=\"kt\">string<\/span> <span class=\"n\">name<\/span><span class=\"p\">,<\/span> <span class=\"k\">params<\/span> <span class=\"kt\">object<\/span><span class=\"p\">[]<\/span> <span class=\"n\">arguments<\/span><span class=\"p\">]<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"k\">get<\/span>\n        <span class=\"p\">{<\/span>\n            <span class=\"n\">LocalizedString<\/span> <span class=\"n\">s<\/span> <span class=\"p\">=<\/span> <span class=\"k\">null<\/span><span class=\"p\">;<\/span>\n            <span class=\"k\">foreach<\/span> <span class=\"p\">(<\/span><span class=\"kt\">var<\/span> <span class=\"n\">localizer<\/span> <span class=\"k\">in<\/span> <span class=\"n\">m_localizers<\/span><span class=\"p\">)<\/span>\n            <span class=\"p\">{<\/span>\n                <span class=\"n\">s<\/span> <span class=\"p\">=<\/span> <span class=\"n\">localizer<\/span><span class=\"p\">[<\/span><span class=\"n\">name<\/span><span class=\"p\">,<\/span> <span class=\"n\">arguments<\/span><span class=\"p\">];<\/span>\n                <span class=\"k\">if<\/span> <span class=\"p\">(!<\/span><span class=\"n\">s<\/span><span class=\"p\">.<\/span><span class=\"n\">ResourceNotFound<\/span><span class=\"p\">)<\/span>\n                <span class=\"p\">{<\/span>\n                    <span class=\"nf\">OnLogAttempt<\/span><span class=\"p\">(<\/span><span class=\"n\">localizer<\/span><span class=\"p\">,<\/span> <span class=\"n\">s<\/span><span class=\"p\">,<\/span> <span class=\"n\">m_cultureInfo<\/span><span class=\"p\">);<\/span>\n                    <span class=\"k\">return<\/span> <span class=\"n\">s<\/span><span class=\"p\">;<\/span>\n                <span class=\"p\">}<\/span>\n\n                <span class=\"nf\">OnLogAttempt<\/span><span class=\"p\">(<\/span><span class=\"n\">localizer<\/span><span class=\"p\">,<\/span> <span class=\"n\">s<\/span><span class=\"p\">,<\/span> <span class=\"n\">m_cultureInfo<\/span><span class=\"p\">);<\/span>\n            <span class=\"p\">}<\/span>\n            <span class=\"n\">Debug<\/span><span class=\"p\">.<\/span><span class=\"nf\">Assert<\/span><span class=\"p\">(<\/span><span class=\"n\">s<\/span> <span class=\"p\">!=<\/span> <span class=\"k\">null<\/span><span class=\"p\">);<\/span>\n            <span class=\"k\">return<\/span> <span class=\"n\">s<\/span><span class=\"p\">;<\/span>\n        <span class=\"p\">}<\/span>\n    <span class=\"p\">}<\/span>\n<span class=\"p\">}<\/span><\/code><\/pre><\/figure>\n\n<p>The implementation is rather straight forward. Simply iterate all passed localizers\nuntil one returns the resource in question.<\/p>\n\n<h3 id=\"inheritstringlocalizerfactory\">InheritStringLocalizerFactory<\/h3>\n\n<p>This class is responsible for actually creating our <code class=\"language-plaintext highlighter-rouge\">MultiStringLocalizer<\/code> types.\nIt needs to be registered with DI, which can be done with code like this:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-c#\" data-lang=\"c#\"><span class=\"k\">public<\/span> <span class=\"k\">static<\/span> <span class=\"k\">class<\/span> <span class=\"nc\">LocalizerExtensions<\/span>\n<span class=\"p\">{<\/span>\n    <span class=\"k\">public<\/span> <span class=\"k\">static<\/span> <span class=\"n\">IServiceCollection<\/span> <span class=\"nf\">AddInheritStringLocalizerFactory<\/span><span class=\"p\">(<\/span><span class=\"k\">this<\/span> <span class=\"n\">IServiceCollection<\/span> <span class=\"n\">services<\/span><span class=\"p\">,<\/span> <span class=\"n\">Action<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">LocalizationOptions<\/span><span class=\"p\">&gt;<\/span> <span class=\"n\">action<\/span> <span class=\"p\">=<\/span> <span class=\"k\">null<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">services<\/span> <span class=\"p\">==<\/span> <span class=\"k\">null<\/span><span class=\"p\">)<\/span>\n        <span class=\"p\">{<\/span>\n            <span class=\"k\">throw<\/span> <span class=\"k\">new<\/span> <span class=\"nf\">ArgumentNullException<\/span><span class=\"p\">(<\/span><span class=\"k\">nameof<\/span><span class=\"p\">(<\/span><span class=\"n\">services<\/span><span class=\"p\">));<\/span>\n        <span class=\"p\">}<\/span>\n\n        <span class=\"n\">services<\/span><span class=\"p\">.<\/span><span class=\"nf\">AddOptions<\/span><span class=\"p\">();<\/span>\n        <span class=\"n\">services<\/span><span class=\"p\">.<\/span><span class=\"n\">AddSingleton<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">IStringLocalizerFactory<\/span><span class=\"p\">,<\/span> <span class=\"n\">InheritStringLocalizerFactory<\/span><span class=\"p\">&gt;();<\/span>\n        <span class=\"n\">services<\/span><span class=\"p\">.<\/span><span class=\"nf\">AddTransient<\/span><span class=\"p\">(<\/span><span class=\"k\">typeof<\/span> <span class=\"p\">(<\/span><span class=\"n\">IStringLocalizer<\/span><span class=\"p\">&lt;&gt;),<\/span> <span class=\"k\">typeof<\/span> <span class=\"p\">(<\/span><span class=\"n\">StringLocalizer<\/span><span class=\"p\">&lt;&gt;));<\/span>\n        <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">action<\/span> <span class=\"p\">!=<\/span> <span class=\"k\">null<\/span><span class=\"p\">)<\/span>\n        <span class=\"p\">{<\/span>\n            <span class=\"n\">services<\/span><span class=\"p\">.<\/span><span class=\"nf\">Configure<\/span><span class=\"p\">(<\/span><span class=\"n\">action<\/span><span class=\"p\">);<\/span>\n        <span class=\"p\">}<\/span>\n\n        <span class=\"k\">return<\/span> <span class=\"n\">services<\/span><span class=\"p\">;<\/span>\n    <span class=\"p\">}<\/span>\n<span class=\"p\">}<\/span><\/code><\/pre><\/figure>\n\n<p>The actual factory implementation looks like this:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-c#\" data-lang=\"c#\"><span class=\"k\">public<\/span> <span class=\"k\">class<\/span> <span class=\"nc\">InheritStringLocalizerFactory<\/span> <span class=\"p\">:<\/span> <span class=\"n\">IStringLocalizerFactory<\/span>\n<span class=\"p\">{<\/span>\n    <span class=\"k\">private<\/span> <span class=\"k\">readonly<\/span> <span class=\"n\">ConcurrentDictionary<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">Type<\/span><span class=\"p\">,<\/span> <span class=\"n\">IStringLocalizer<\/span><span class=\"p\">&gt;<\/span> <span class=\"n\">m_cache<\/span> <span class=\"p\">=<\/span> <span class=\"k\">new<\/span> <span class=\"n\">ConcurrentDictionary<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">Type<\/span><span class=\"p\">,<\/span> <span class=\"n\">IStringLocalizer<\/span><span class=\"p\">&gt;();<\/span>\n    <span class=\"k\">private<\/span> <span class=\"k\">readonly<\/span> <span class=\"n\">ResourceManagerStringLocalizerFactory<\/span> <span class=\"n\">m_factory<\/span><span class=\"p\">;<\/span>\n    <span class=\"k\">private<\/span> <span class=\"k\">readonly<\/span> <span class=\"n\">ILoggerFactory<\/span> <span class=\"n\">m_loggerFactory<\/span><span class=\"p\">;<\/span>\n\n    <span class=\"k\">public<\/span> <span class=\"nf\">InheritStringLocalizerFactory<\/span><span class=\"p\">(<\/span><span class=\"n\">IOptions<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">LocalizationOptions<\/span><span class=\"p\">&gt;<\/span> <span class=\"n\">localizationOptions<\/span><span class=\"p\">,<\/span> <span class=\"n\">ILoggerFactory<\/span> <span class=\"n\">loggerFactory<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"n\">m_factory<\/span> <span class=\"p\">=<\/span> <span class=\"k\">new<\/span> <span class=\"nf\">ResourceManagerStringLocalizerFactory<\/span><span class=\"p\">(<\/span><span class=\"n\">localizationOptions<\/span><span class=\"p\">,<\/span> <span class=\"n\">loggerFactory<\/span><span class=\"p\">);<\/span>\n        <span class=\"n\">m_loggerFactory<\/span> <span class=\"p\">=<\/span> <span class=\"n\">loggerFactory<\/span><span class=\"p\">;<\/span>\n    <span class=\"p\">}<\/span>\n\n    <span class=\"k\">public<\/span> <span class=\"n\">IStringLocalizer<\/span> <span class=\"nf\">Create<\/span><span class=\"p\">(<\/span><span class=\"n\">Type<\/span> <span class=\"n\">resourceSource<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">resourceSource<\/span> <span class=\"p\">==<\/span> <span class=\"k\">null<\/span><span class=\"p\">)<\/span>\n            <span class=\"k\">throw<\/span> <span class=\"k\">new<\/span> <span class=\"nf\">ArgumentNullException<\/span><span class=\"p\">(<\/span><span class=\"k\">nameof<\/span><span class=\"p\">(<\/span><span class=\"n\">resourceSource<\/span><span class=\"p\">));<\/span>\n\n        <span class=\"k\">return<\/span> <span class=\"nf\">CreateStringLocalizer<\/span><span class=\"p\">(<\/span><span class=\"n\">resourceSource<\/span><span class=\"p\">);<\/span>\n    <span class=\"p\">}<\/span>\n\n    <span class=\"k\">private<\/span> <span class=\"n\">IStringLocalizer<\/span> <span class=\"nf\">CreateStringLocalizer<\/span><span class=\"p\">(<\/span><span class=\"n\">Type<\/span> <span class=\"n\">type<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"k\">return<\/span> <span class=\"n\">m_cache<\/span><span class=\"p\">.<\/span><span class=\"nf\">GetOrAdd<\/span><span class=\"p\">(<\/span><span class=\"n\">type<\/span><span class=\"p\">,<\/span> <span class=\"n\">CreateStringLocalizerDirect<\/span><span class=\"p\">);<\/span>\n    <span class=\"p\">}<\/span>\n\n    <span class=\"k\">private<\/span> <span class=\"n\">IStringLocalizer<\/span> <span class=\"nf\">CreateStringLocalizerDirect<\/span><span class=\"p\">(<\/span><span class=\"n\">Type<\/span> <span class=\"n\">t<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"kt\">var<\/span> <span class=\"n\">attributes<\/span> <span class=\"p\">=<\/span> <span class=\"n\">t<\/span><span class=\"p\">.<\/span><span class=\"n\">GetCustomAttributes<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">InheritStringLocalizationAttribute<\/span><span class=\"p\">&gt;();<\/span>\n        <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">attributes<\/span><span class=\"p\">.<\/span><span class=\"nf\">Any<\/span><span class=\"p\">())<\/span>\n        <span class=\"p\">{<\/span>\n            <span class=\"kt\">var<\/span> <span class=\"n\">localizers<\/span> <span class=\"p\">=<\/span> <span class=\"k\">new<\/span> <span class=\"n\">List<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">IStringLocalizer<\/span><span class=\"p\">&gt;();<\/span>\n            <span class=\"kt\">var<\/span> <span class=\"n\">localizer<\/span> <span class=\"p\">=<\/span> <span class=\"n\">m_factory<\/span><span class=\"p\">.<\/span><span class=\"nf\">Create<\/span><span class=\"p\">(<\/span><span class=\"n\">t<\/span><span class=\"p\">);<\/span>\n            <span class=\"n\">localizers<\/span><span class=\"p\">.<\/span><span class=\"nf\">Add<\/span><span class=\"p\">(<\/span><span class=\"n\">localizer<\/span><span class=\"p\">);<\/span>\n            <span class=\"k\">foreach<\/span> <span class=\"p\">(<\/span><span class=\"kt\">var<\/span> <span class=\"n\">attribute<\/span> <span class=\"k\">in<\/span> <span class=\"n\">attributes<\/span><span class=\"p\">.<\/span><span class=\"nf\">OrderBy<\/span><span class=\"p\">(<\/span><span class=\"n\">a<\/span> <span class=\"p\">=&gt;<\/span> <span class=\"n\">a<\/span><span class=\"p\">.<\/span><span class=\"n\">Priority<\/span><span class=\"p\">))<\/span>\n            <span class=\"p\">{<\/span>\n                <span class=\"n\">localizer<\/span> <span class=\"p\">=<\/span> <span class=\"nf\">CreateStringLocalizer<\/span><span class=\"p\">(<\/span><span class=\"n\">attribute<\/span><span class=\"p\">.<\/span><span class=\"n\">InheritFrom<\/span><span class=\"p\">);<\/span>\n                <span class=\"n\">localizers<\/span><span class=\"p\">.<\/span><span class=\"nf\">Add<\/span><span class=\"p\">(<\/span><span class=\"n\">localizer<\/span><span class=\"p\">);<\/span>\n            <span class=\"p\">}<\/span>\n\n            <span class=\"k\">return<\/span> <span class=\"k\">new<\/span> <span class=\"nf\">MultiStringLocalizer<\/span><span class=\"p\">(<\/span><span class=\"n\">localizers<\/span><span class=\"p\">,<\/span> <span class=\"n\">m_loggerFactory<\/span><span class=\"p\">.<\/span><span class=\"n\">CreateLogger<\/span><span class=\"p\">&lt;<\/span><span class=\"n\">MultiStringLocalizer<\/span><span class=\"p\">&gt;());<\/span>\n        <span class=\"p\">}<\/span>\n\n        <span class=\"k\">return<\/span> <span class=\"n\">m_factory<\/span><span class=\"p\">.<\/span><span class=\"nf\">Create<\/span><span class=\"p\">(<\/span><span class=\"n\">t<\/span><span class=\"p\">);<\/span>\n    <span class=\"p\">}<\/span>\n\n    <span class=\"k\">public<\/span> <span class=\"n\">IStringLocalizer<\/span> <span class=\"nf\">Create<\/span><span class=\"p\">(<\/span><span class=\"kt\">string<\/span> <span class=\"n\">baseName<\/span><span class=\"p\">,<\/span> <span class=\"kt\">string<\/span> <span class=\"n\">location<\/span><span class=\"p\">)<\/span> <span class=\"p\">=&gt;<\/span> <span class=\"n\">m_factory<\/span><span class=\"p\">.<\/span><span class=\"nf\">Create<\/span><span class=\"p\">(<\/span><span class=\"n\">baseName<\/span><span class=\"p\">,<\/span> <span class=\"n\">location<\/span><span class=\"p\">);<\/span>\n<span class=\"p\">}<\/span><\/code><\/pre><\/figure>","author":{"name":{}},"category":[{"@attributes":{"term":"asp.net"}},{"@attributes":{"term":"core"}},{"@attributes":{"term":"localization"}}],"summary":"Introduction"},{"title":"Creating a PDF with DocFX with numbered chapters","link":{"@attributes":{"href":"https:\/\/cklutz.github.io\/2018\/11\/05\/docfx-pdf-number-chapters\/","rel":"alternate","type":"text\/html","title":"Creating a PDF with DocFX with numbered chapters"}},"published":"2018-11-05T07:10:25+00:00","updated":"2018-11-05T07:10:25+00:00","id":"https:\/\/cklutz.github.io\/2018\/11\/05\/docfx-pdf-number-chapters","content":"<p>TL;DR: Just here for the tool? Alright, find it <a href=\"https:\/\/github.com\/cklutz\/DocFxChapterNumbers\">here<\/a>.<\/p>\n\n<p>Using <a href=\"https:\/\/dotnet.github.io\/docfx\/index.html\">DocFx<\/a> you can create\n\u201cconceptual content\u201d, that is content that is not API docs, but manually crafted content.<\/p>\n\n<p>Such content often goes along with API documentation, providing content like how to guides,\nadministration or installation topics, etc.<\/p>\n\n<p>Sometimes it is desirable to have such content available as PDF documentation, rather than\nonly as a web site. DocFx can generate a <a href=\"https:\/\/dotnet.github.io\/docfx\/tutorial\/walkthrough\/walkthrough_generate_pdf.html\">PDF<\/a>\nform markdown files, as it can generate web sites. It does so by invoking <a href=\"https:\/\/wkhtmltopdf.org\/\">wkhtmlpdf<\/a>\nin the background.<\/p>\n\n<p>As can be seen <a href=\"https:\/\/dotnet.github.io\/docfx\/tutorial\/walkthrough\/walkthrough_generate_pdf.html\">here<\/a>, the\nbasic idea is that you generate a <code class=\"language-plaintext highlighter-rouge\">toc.md<\/code> file and list the chapters of your conceptual topics there.<\/p>\n\n<p>For example a <code class=\"language-plaintext highlighter-rouge\">toc.md<\/code> file may contain the following:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-markdown\" data-lang=\"markdown\"><span class=\"gh\"># Administration Guide<\/span>\n<span class=\"gu\">## Installation Procedure<\/span>\n<span class=\"gu\">### [Prerequisites](admin\\prereq.md)<\/span>\n<span class=\"gu\">### [Installation](admin\\install.md)<\/span>\n<span class=\"gu\">## Usage<\/span>\n<span class=\"gu\">### [Start &amp; Stop](admin\\start_stop.md)<\/span>\n<span class=\"gu\">### [Troubleshooting](admin\\trouble.md)<\/span><\/code><\/pre><\/figure>\n\n<p>Each referenced file (e.g. <code class=\"language-plaintext highlighter-rouge\">admin\\start_stop.md<\/code>) then contains the complete chapter, including\nsub chapters.<\/p>\n\n<p>For example:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-markdown\" data-lang=\"markdown\"><span class=\"gu\">## Starting the Application<\/span>\n...\n<span class=\"gu\">### Starting as a service<\/span>\n...\n<span class=\"gu\">### Starting in the foreground<\/span>\n...<\/code><\/pre><\/figure>\n\n<p>So far so good. Having a proper <code class=\"language-plaintext highlighter-rouge\">docfx.json<\/code> file and running <code class=\"language-plaintext highlighter-rouge\">docfx.exe pdf<\/code> you will get PDF file containing\nthese topics, together with an introductionary TOC that reflects the <code class=\"language-plaintext highlighter-rouge\">toc.md<\/code> file.<\/p>\n\n<blockquote>\n  <p>NOTE: You can customize many aspects of the PDF, much more that is obvious, by providing your own template.\nIt is not <em>that<\/em> hard, see <a href=\"https:\/\/dotnet.github.io\/docfx\/tutorial\/walkthrough\/advanced_walkthrough.html\">this<\/a>\nfor more information. This includes having page numbers, custom headers\/footers per page, a cover page, etc.\n(This is probably a good topic for another post some time).<\/p>\n<\/blockquote>\n\n<p>The one thing that is lacking is the ability to have numbered headings\/chapters, that is have a structure like\nthis:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-markdown\" data-lang=\"markdown\">X. Starting the Application\nX.Y Starting as a service\nX.Z Starting in the forground<\/code><\/pre><\/figure>\n\n<p>It is possible to achieve somthing like this using a customized template and <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/CSS_Lists_and_Counters\/Using_CSS_counters\">CSS counters<\/a>\nlike shown <a href=\"https:\/\/gist.github.com\/patik\/89ee6092c72a9e39950445c01598517a\">here<\/a> for example.\nYou can add your custom classes for <code class=\"language-plaintext highlighter-rouge\">H1<\/code>, etc. into a custom template (see above).<\/p>\n\n<p>This actually works somewhat, but misses one critical thing: the numbers don\u2019t appear in the TOC.\nAdditionally, I haven\u2019t been able to get it working without resetting the header counts for each\nchapter listed in the TOC, making it somewhat useless.<\/p>\n\n<p>Another option is to use the <a href=\"https:\/\/wkhtmltopdf.org\/usage\/wkhtmltopdf.txt\"><code class=\"language-plaintext highlighter-rouge\">--xsl-style-sheet<\/code><\/a>, however\nI haven\u2019t been able get this working either. Somehow it was ignored.<\/p>\n\n<p>I thus figured a pre processor like tool could help. It would parse a <code class=\"language-plaintext highlighter-rouge\">toc.md<\/code> and the files it references,\nenumerating all the headings (markdown <code class=\"language-plaintext highlighter-rouge\">#<\/code>, <code class=\"language-plaintext highlighter-rouge\">##<\/code>, etc.) and assign chapter numbers to them. The resulting\noutput files would be written into an output directory that, after all is done, reflects the directory structure\nof the original files.<\/p>\n\n<p>This could be done using regular expressions, some hand crafted code, or using a markdown parser. I opted\nfor the last one, since there are a couple of mature libraries available for .NET and especially <a href=\"https:\/\/github.com\/lunet-io\/markdig\">Markdig<\/a>\nseems only natural, since that is what <code class=\"language-plaintext highlighter-rouge\">DocFx<\/code> uses as well.<\/p>\n\n<p>The actual code is rather straight forward and you can find it <a href=\"https:\/\/github.com\/cklutz\/DocFxChapterNumbers\/blob\/master\/DocFxChapterNumbers\/Program.cs\">here<\/a>.\nYup, it is rather tossed together, but for the simple use case it solves it is sufficient.<\/p>\n\n<p>The tool is also available on <a href=\"https:\/\/www.nuget.org\/packages\/DocFxChapterNumbers\/\">NuGet<\/a>.<\/p>\n\n<p>To complete the example from above, here is what the markdown files would look like after processing:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-markdown\" data-lang=\"markdown\"><span class=\"gh\"># Administration Guide<\/span>\n<span class=\"gu\">## 1 Installation Procedure<\/span>\n<span class=\"gu\">### [1.1 Prerequisites](admin\\prereq.md)<\/span>\n<span class=\"gu\">### [1.2 Installation](admin\\install.md)<\/span>\n<span class=\"gu\">## 2 Usage<\/span>\n<span class=\"gu\">### [2.1 Start &amp; Stop](admin\\start_stop.md)<\/span>\n<span class=\"gu\">### [2.2 Troubleshooting](admin\\trouble.md)<\/span><\/code><\/pre><\/figure>\n\n<p>and<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-markdown\" data-lang=\"markdown\"><span class=\"gh\"># 1 Installation Procedure<\/span>\n<span class=\"gu\">## 1.1 Prerequisites<\/span><\/code><\/pre><\/figure>\n\n<figure class=\"highlight\"><pre><code class=\"language-markdown\" data-lang=\"markdown\"><span class=\"gu\">## 1.2 Starting the Application<\/span>\n...\n<span class=\"gu\">### 1.2.1 Starting as a service<\/span>\n...\n<span class=\"gu\">### 1.2.2 Starting in the foreground<\/span>\n...<\/code><\/pre><\/figure>\n\n<p>Note that an artificial Level 1 heading is added to the content files of each level 1 from the TOC.\nIn this case the first file for the level-1-toc-chapter is <code class=\"language-plaintext highlighter-rouge\">prereq.md<\/code> so there the \nheading \u201c1 Installation Procedures\u201d will be added. The file <code class=\"language-plaintext highlighter-rouge\">install.md<\/code> is also under\nthe level-1-toc-chapter, but doesn\u2019t need this header again.<\/p>\n\n<p>The above approach works rather nicely for my purposes, YMMV of course.<\/p>\n\n<p>BTW, instead of a standalone tool a <a href=\"https:\/\/dotnet.github.io\/docfx\/templates-and-plugins\/plugins-dashboard.html\">DocFX plugin<\/a>\nwould have also been possible I guess. For now, I like the standalone approach better, because it is more easily testable\nand in general has no direct dependency to a particular DocFX version.<\/p>","author":{"name":{}},"category":[{"@attributes":{"term":"docfx"}},{"@attributes":{"term":"markdown"}},{"@attributes":{"term":"docascode"}}],"summary":"TL;DR: Just here for the tool? Alright, find it here."},{"title":"Convert NUnit to MSTest V2","link":{"@attributes":{"href":"https:\/\/cklutz.github.io\/2018\/10\/30\/Convert-NUnit-To-MSTestV2\/","rel":"alternate","type":"text\/html","title":"Convert NUnit to MSTest V2"}},"published":"2018-10-30T15:00:00+00:00","updated":"2018-10-30T15:00:00+00:00","id":"https:\/\/cklutz.github.io\/2018\/10\/30\/Convert-NUnit-To-MSTestV2","content":"<p>TL;DR: Just here for the tool? Alright, find it <a href=\"https:\/\/github.com\/cklutz\/NUnitToMSTest\">here<\/a>.<\/p>\n\n<h2 id=\"why-do-it\">Why do it?<\/h2>\n\n<p>MSTest has come a long way. The current version, collegially called \u201cV2\u201d, is much more\nflexible then the original version that shipped back in the day with Visual Studio 2005.<\/p>\n\n<p>MSTest consists of the execution engine\/runner, typically manifested as <code class=\"language-plaintext highlighter-rouge\">vstest.console.exe<\/code>\n(and sometimes referred to as \u201cVSTest\u201d) and the actual test framework, containing typical\ntypes like <code class=\"language-plaintext highlighter-rouge\">Assert<\/code>.<\/p>\n\n<p>The test runner can also execute test cases using different frameworks, provided an adapter exists.\nFor example, for <a href=\"https:\/\/github.com\/nunit\/nunit3-vs-adapter\">NUnit<\/a>, <a href=\"https:\/\/github.com\/xunit\/xunit\">XUnit<\/a>,\nand custom ones are also possible. In one of the projects I\u2019ve been on, we have developed\na custom adapter that can integrate a total custom test runner (for database tests using\nSSIS) into the Visual Studio and TFS using an adapter.<\/p>\n\n<p>One could ask why a migration from NUnit to MSTest V2 is actually useful, when there is an\nadapter in place that allows to run NUnit Tests directly using VSTest? Well, the reasons\nbasically are (and I\u2019m aware that at least some these may be eliminated in the future):<\/p>\n\n<ul>\n  <li>\n    <p>Reduction of complexity: less NuGet packages required, less components. Less things to check\nwhen things don\u2019t work as expected.<\/p>\n  <\/li>\n  <li>\n    <p>Direct support of features provided by MSTest but maybe not by others. And on the flip-side,\nobvious non-support of features supported by other frameworks but not by MSTest\/VSTest.\nFor example, NUnit, XUnit, etc. provide stuff that MSTest doesn\u2019t.<\/p>\n  <\/li>\n<\/ul>\n\n<p>Now as said, some of such issues may decay over time. Others, as is in the nature of\n\u201ccompeting\u201d concepts\/products, may never be fully supported - if even technically possible.<\/p>\n\n<p>That doesn\u2019t say anything negative about either test framework, but can be a hindrance in\nproductive use in a larger teams.  Consider developers might create\nunit tests locally (using a native test runner of said frameworks) and all is well; other\ndevelopers using VSTest (or TFS builds for that matter) may then choke on such features\nnot or not properly supported by the respective adapter.\n(As an example see Issues <a href=\"https:\/\/github.com\/nunit\/nunit3-vs-adapter\/issues\/319\">#319<\/a>,\n<a href=\"https:\/\/github.com\/nunit\/nunit3-vs-adapter\/issues\/438\">#438<\/a> or \n<a href=\"https:\/\/github.com\/nunit\/nunit3-vs-adapter\/issues\/47\">#47<\/a>).<\/p>\n\n<h2 id=\"open-source-yeah\">Open Source, yeah!<\/h2>\n\n<p>MSTest V2 is developed in the open, as are many newer Microsoft development products.\nOne can see and participate on GitHub for the <a href=\"https:\/\/github.com\/Microsoft\/vstest\">execution engine<\/a>\nand the <a href=\"https:\/\/github.com\/Microsoft\/testfx\">framework<\/a>.<\/p>\n\n<p>Being a liberal MIT-license, this has all the benefits one would expect from an open source\nsoftware.<\/p>\n\n<h2 id=\"its-the-new-things\">It\u2019s the new things<\/h2>\n\n<p>Now of course staying with your existing test framework (NUnit, for example) is an option\none can always take. However, if you consider to move to MSTest it must be MSTest V2 and\nnot the older \u201cV1\u201d version that shipped with Visual Studio 2005. MSTest V2 is actively\nbeing worked on and is also the version used when you create a new project using the\nUnitTest project template in Visual Studio 2017 and above.<\/p>\n\n<h2 id=\"ok-enough-how-to-do-the-switch\">OK, enough. How to do the switch<\/h2>\n\n<p>If you want to change I would suggest the following rough plan layed out in the following\nchapters.<\/p>\n\n<p>Oh, one more thing. The next chapters describe what was done in a large-ish project with\nabout 20 test assemblies and some couple thousand test cases. If you only have one test\nassembly with just a couple of tests (do you?), then you can get along with a much less\nstructured approach, just edit-build-edit-build, etc. your way through.<\/p>\n\n<h3 id=\"project-files---replace-references\">Project Files - Replace References<\/h3>\n\n<ul>\n  <li>Remove all NUnit references or NuGet packages from the test projects<\/li>\n  <li>Add MSTest V2 packages (<code class=\"language-plaintext highlighter-rouge\">MSTest.TestFramework<\/code> and <code class=\"language-plaintext highlighter-rouge\">MSTest.TestAdapter<\/code>)<\/li>\n<\/ul>\n\n<h3 id=\"sources---replace-simple-attributes\">Sources - Replace Simple Attributes<\/h3>\n\n<ul>\n  <li>Replace all simple attributes from NUnit to MSTest, including:\n    <ul>\n      <li><code class=\"language-plaintext highlighter-rouge\">TestFixture<\/code> -&gt; <code class=\"language-plaintext highlighter-rouge\">TestClass<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">SetUp<\/code>, <code class=\"language-plaintext highlighter-rouge\">TearDown<\/code> -&gt; <code class=\"language-plaintext highlighter-rouge\">TestInitialize<\/code>, <code class=\"language-plaintext highlighter-rouge\">TestCleanup<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">Test<\/code> -&gt; <code class=\"language-plaintext highlighter-rouge\">TestMethod<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">Category<\/code> -&gt; <code class=\"language-plaintext highlighter-rouge\">TestCategory<\/code><\/li>\n      <li><code class=\"language-plaintext highlighter-rouge\">TestCase<\/code> -&gt; <code class=\"language-plaintext highlighter-rouge\">DataRow<\/code> + <code class=\"language-plaintext highlighter-rouge\">DataTestMethod<\/code><\/li>\n    <\/ul>\n  <\/li>\n<\/ul>\n\n<h3 id=\"sources---replace-and-update-complex-attributes\">Sources - Replace and Update complex Attributes<\/h3>\n\n<ul>\n  <li><code class=\"language-plaintext highlighter-rouge\">OneTimeSetUp<\/code>, <code class=\"language-plaintext highlighter-rouge\">OneTimeTearDown<\/code> -&gt; <code class=\"language-plaintext highlighter-rouge\">ClassInitialize<\/code>, <code class=\"language-plaintext highlighter-rouge\">ClassCleanup<\/code>\n    <ul>\n      <li>Methods must be <code class=\"language-plaintext highlighter-rouge\">public static void<\/code><\/li>\n      <li>Method with <code class=\"language-plaintext highlighter-rouge\">ClassInitialize<\/code> must have one parameter of type <code class=\"language-plaintext highlighter-rouge\">TestContext<\/code><\/li>\n    <\/ul>\n  <\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">Property<\/code> -&gt; <code class=\"language-plaintext highlighter-rouge\">TestProperty<\/code>\n    <ul>\n      <li>Key\/Value, Value can only be a <code class=\"language-plaintext highlighter-rouge\">System.String<\/code>.<\/li>\n    <\/ul>\n  <\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">Explicit<\/code>: has no direct counterpart in MSTest. Alternatively, use <code class=\"language-plaintext highlighter-rouge\">Ignore<\/code>,\n <code class=\"language-plaintext highlighter-rouge\">Priority<\/code> or <code class=\"language-plaintext highlighter-rouge\">TestCategory<\/code> and filtering during execution to skip tests.<\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">Combinatorial<\/code> and <code class=\"language-plaintext highlighter-rouge\">Range<\/code>: again, no direct counterpart in MSTest (altough\n see the note about extending MSTest below). You could expand such things to\n single <code class=\"language-plaintext highlighter-rouge\">DataRow<\/code> attributes.<\/li>\n  <li><code class=\"language-plaintext highlighter-rouge\">Culture<\/code>: Manually set and reset <code class=\"language-plaintext highlighter-rouge\">Thread.Current(UI)Culture<\/code>.<\/li>\n  <li>In general: some attributes have less options, for example <code class=\"language-plaintext highlighter-rouge\">TestClass<\/code> has no \n<code class=\"language-plaintext highlighter-rouge\">Description<\/code> as <code class=\"language-plaintext highlighter-rouge\">TestClass<\/code> had.<\/li>\n<\/ul>\n\n<p>You can also <a href=\"https:\/\/blogs.msdn.microsoft.com\/devops\/2017\/07\/18\/extending-mstest-v2\/\">extend<\/a>\nand <a href=\"https:\/\/www.meziantou.net\/2018\/02\/26\/mstest-v2-customize-test-execution\">customize<\/a>\nMSTest V2 to add features that may be lost by changing from NUnit.\nI consider these advanced concepts and don\u2019t discuss them here further.\nPlease refer to the linked articles for more information. Also, arguably,\nif you create a standard <a href=\"https:\/\/www.agile-code.com\/blog\/the-anatomy-of-a-unit-test\/\">4-phase-test<\/a>\nyou probably don\u2019t need much extensions.<\/p>\n\n<h3 id=\"sources---assert-api\">Sources - Assert API<\/h3>\n\n<ul>\n  <li>In general replacing <code class=\"language-plaintext highlighter-rouge\">using NUnit.Framework<\/code> with <code class=\"language-plaintext highlighter-rouge\">using Microsoft.VisualStudio.TestTools.UnitTesting<\/code>\nwill make lots of stuff simply work.\n    <ul>\n      <li>Typical asserts have the same signature, e.g. <code class=\"language-plaintext highlighter-rouge\">Assert.AreEqual<\/code>.<\/li>\n      <li>Fluent APIs and other add ons are problematic, as they most likely have no counterpart\nfor MSTest.<\/li>\n      <li>NUnit built-in fluent API must be handled manually, e.g.:\n        <ul>\n          <li><code class=\"language-plaintext highlighter-rouge\">Assert.That(value, Is.Null)<\/code> -&gt; <code class=\"language-plaintext highlighter-rouge\">Assert.IsNull(value)<\/code><\/li>\n          <li><code class=\"language-plaintext highlighter-rouge\">Assert.That(expr, Throws.TypeOf&lt;Ex&gt;())<\/code> -&gt; <code class=\"language-plaintext highlighter-rouge\">Assert.ThrowsException&lt;Ex&gt;(expr)<\/code><\/li>\n          <li><code class=\"language-plaintext highlighter-rouge\">Assert.That(expr, Throws.TypeOf&lt;Ex&gt;().With.Message.Contains(str))<\/code> -&gt; <code class=\"language-plaintext highlighter-rouge\">StringAssert.Contains(Assert.ThrowsException&lt;Ex&gt;(expr).Message, str)<\/code><\/li>\n        <\/ul>\n      <\/li>\n    <\/ul>\n  <\/li>\n  <li>Some Assert APIs have slightly different names: <code class=\"language-plaintext highlighter-rouge\">Assert.True<\/code> -&gt; <code class=\"language-plaintext highlighter-rouge\">Assert.IsTrue<\/code>,\n<code class=\"language-plaintext highlighter-rouge\">Assert.NotNull<\/code> -&gt; <code class=\"language-plaintext highlighter-rouge\">Assert.IsNotNull<\/code>, etc.<\/li>\n  <li>Some have different signatures but the same functionality: e.g. <code class=\"language-plaintext highlighter-rouge\">Assert.IsInstanceOf(Type, value)<\/code> -&gt;\n<code class=\"language-plaintext highlighter-rouge\">Assert.IsInstanceOfType(value, Type)<\/code>.<\/li>\n<\/ul>\n\n<p>So, in general MSTest has a lot less Assert API surface than NUnit.\nThis however is not the worst thing (neglecting potential migration effort),\nbecause it forces you to write \u201csimpler\u201d tests. However, you could still extend\nMSTest, <a href=\"https:\/\/www.meziantou.net\/2018\/02\/19\/mstest-v2-create-new-asserts\">creating your own asserts<\/a>.<\/p>\n\n<h2 id=\"cant-the-computer-do-that-for-me\">Can\u2019t the computer do that for me?<\/h2>\n\n<p>Yes, it can. Generally, at least. Most of the tasks explained in the previous chapter\ncould of course be handled by a tool.<\/p>\n\n<p>The first tool of choice would be a simple search and replace using your text editor\u2019s capabilities.\nThis would work nicely for the easier things like simple attribute and assert method replacement,\nbut fails for the even slightly more complex things like <code class=\"language-plaintext highlighter-rouge\">Assert.That(value, Is.Null)<\/code> -&gt; <code class=\"language-plaintext highlighter-rouge\">Assert.IsNull(value)<\/code>.<\/p>\n\n<p>No, regular expressions are not the solution - although they might get you little further. Note that\nyour actual code might not be written as \u201cnicely\u201d as in <code class=\"language-plaintext highlighter-rouge\">Assert.That(value, Is.Null)<\/code>, more likely it\nis something like this:<\/p>\n\n<div class=\"language-csharp highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">Assert<\/span><span class=\"p\">.<\/span><span class=\"nf\">That<\/span><span class=\"p\">(<\/span>\n    <span class=\"n\">myObject<\/span><span class=\"p\">.<\/span><span class=\"n\">mamboJumbo<\/span><span class=\"p\">.<\/span><span class=\"nf\">GetValue<\/span><span class=\"p\">(<\/span><span class=\"m\">499<\/span><span class=\"p\">,<\/span> <span class=\"m\">399<\/span><span class=\"p\">),<\/span>\n    <span class=\"n\">Is<\/span><span class=\"p\">.<\/span><span class=\"n\">Null<\/span>\n<span class=\"p\">)<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Of course <em>still<\/em> doable with regex, but it gets nastier by the example. And remember, the result of\ntool supported migration should not be a half broken codebase.<\/p>\n\n<p>The technically I thus settled on was Roslyn - i.e. the C# compiler itself. If you have never heard\nof that, Roslyn I mean, please check <a href=\"https:\/\/en.wikipedia.org\/wiki\/.NET_Compiler_Platform\">this<\/a>\nand maybe <a href=\"https:\/\/github.com\/dotnet\/roslyn\/wiki\/Roslyn%20Overview\">this<\/a> and go from there, to\nget some background information. In short, using Roslyn one can parse C# source code, create an\nAST from it, manipulate it, and save the results.<\/p>\n\n<h2 id=\"nunittomstest-tool\">NUnitToMSTest Tool<\/h2>\n\n<p>You can find the source code for the NUnitToMSTest tool <a href=\"https:\/\/github.com\/cklutz\/NUnitToMSTest\">here<\/a>,\nalong with some instructions how it works (it is a Visual Studio extension that you can invoke via\nright click on a project) and what it can do and what not.<\/p>","author":{"name":{}},"category":[{"@attributes":{"term":"nunit"}},{"@attributes":{"term":"mstestv2"}},{"@attributes":{"term":"unit-tests"}},{"@attributes":{"term":"vstest"}},{"@attributes":{"term":"mstest"}}],"summary":"TL;DR: Just here for the tool? Alright, find it here."},{"title":"Running a documentation build separate from a software build","link":{"@attributes":{"href":"https:\/\/cklutz.github.io\/2018\/09\/11\/running-separate-docs-build\/","rel":"alternate","type":"text\/html","title":"Running a documentation build separate from a software build"}},"published":"2018-09-11T07:10:25+00:00","updated":"2018-09-11T07:10:25+00:00","id":"https:\/\/cklutz.github.io\/2018\/09\/11\/running-separate-docs-build","content":"<p>We have a build that just generates the API documentation for a software. It\ndoes so using <a href=\"https:\/\/dotnet.github.io\/docfx\/\"><code class=\"language-plaintext highlighter-rouge\">docfx.exe<\/code><\/a>, which does not\nrequire the binaries, but only the source code.<\/p>\n\n<p>We didn\u2019t want to include the documentation generation in the software build,\nbecause it takes some extra time. On the other hand we wanted to make sure that\nthe documentation always reflects a successful build of the software and not\nsome intermediate state.<\/p>\n\n<p>The idea is as follows:<\/p>\n\n<ul>\n  <li>\n    <p>Let the software be build using whatever form of TFS build is setup.<\/p>\n  <\/li>\n  <li>\n    <p>At a fixed time, normally in the night, run a separate scheduled\nbuild that is only responsible for building the documentation.\nTo fullfil the requirement of working with a \u201cstable set\u201d of sources,\nthis build needs to somehow determine the branch and change set that\nwas used for the software build. It will then need to get (checkout)\nthis version of the sources.<\/p>\n  <\/li>\n<\/ul>\n\n<p>The TFS build system does not support such a feature, so we\u2019ll have to\nroll our own process. We can, however, make use of some features and\nfacilities that TFS does provide to not only make our process integrate\nneatly into the TFS build process, but also to make it more stable.<\/p>\n\n<p>In this post we will use a PowerShell script, but really you could use\nany other language\/technology (or even a <a href=\"https:\/\/cklutz.github.io\/2017\/09\/06\/a-tfs-build-task-for-opencover\/\">custom build task<\/a>)\nthat is capable of performing the required actions.<\/p>\n\n<h2 id=\"authentication\">Authentication<\/h2>\n\n<p>In the following our build script will perform certain actions against\nthe TFS server. To do so, it must authenticate itself. You could do this\nusing multiple ways. For example, you could create a PAT (personal access token),\nand use that or you could create a specific user and use that from the script.\nBoth variants are cumbersome and require some maintenance (e.g. updating the\ntoken when it expires or changing the password if necessary) or downright\ninsecure (storing the user\u2019s password in the script - at least you could\nkeep it as an encrypted variable of the build definition).<\/p>\n\n<p>Luckily, there is a better way to handle this: you use the same credentials\nthat the build definition\/process itself uses. For example, when it runs the\n\u201cGet Sources\u201d task, the agent processing the build will also have to authenticate\nagainst the server. It does so using an access token. This token can be accessed\nfrom build scripts\/tasks when the build definition allows. To enable this,\nyou need to set the \u201cAllow scripts to access OAuth token\u201d setting in the \u201cOptions\u201d\nof your build definition.<\/p>\n\n<p><img src=\"https:\/\/cklutz.github.io\/images\/enable_system_accesstoken.png\" alt=\"Allow scripts to access OAuth token\" \/><\/p>\n\n<p>Once this is done, the environment variable <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/devops\/pipelines\/build\/variables?view=vsts\"><code class=\"language-plaintext highlighter-rouge\">SYSTEM_ACCESSTOKEN<\/code><\/a>\nwill be made available to scripts to access the access token:<\/p>\n\n<ul>\n  <li>You can then pass that to <code class=\"language-plaintext highlighter-rouge\">tf.exe<\/code> using the the <code class=\"language-plaintext highlighter-rouge\">\/login<\/code> and <code class=\"language-plaintext highlighter-rouge\">\/loginType<\/code>\ncommand line options.<\/li>\n  <li>You can pass the token when invoking TFS REST API using the <code class=\"language-plaintext highlighter-rouge\">Authorization<\/code> HTTP header\nheader.<\/li>\n<\/ul>\n\n<p>More on either of these uses later.<\/p>\n\n<h2 id=\"finding-the-last-successful-build\">Finding the last successful build<\/h2>\n\n<p>First we need to know the build definition that produces the builds we\u2019re\ninterested in. For this post we just assume it is \u201cPROD-B-1.0\u201d.<\/p>\n\n<p>Having this, we need to query TFS to find the last successful build and\nextract the required branch and change set information from that.<\/p>\n\n<p>For that purpose we can use the <a href=\"https:\/\/docs.microsoft.com\/en-us\/rest\/api\/vsts\/?view=vsts-rest-4.1\">TFS REST API<\/a>\nto send a HTTP GET request to the API using an URL like the <code class=\"language-plaintext highlighter-rouge\">http:\/\/&lt;servername&gt;\/&lt;collection&gt;\/&lt;project&gt;\/_apis\/build\/builds?&lt;query&gt;<\/code>.\nusing the <code class=\"language-plaintext highlighter-rouge\">Invoke-RestMethod<\/code> powershell command. As with most other system and build specific information,\nmany parts that comprise the URL are available as <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/devops\/pipelines\/build\/variables?view=vsts\">variables<\/a>,\nthat is can be accessed as environment variables.<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-powershell\" data-lang=\"powershell\"><span class=\"nv\">$definition<\/span><span class=\"w\"> <\/span><span class=\"o\">=<\/span><span class=\"w\"> <\/span><span class=\"s1\">'PROD-B-1.0'<\/span><span class=\"w\">\n<\/span><span class=\"nv\">$uri<\/span><span class=\"w\"> <\/span><span class=\"o\">=<\/span><span class=\"w\"> <\/span><span class=\"nv\">$<\/span><span class=\"nn\">env<\/span><span class=\"p\">:<\/span><span class=\"nv\">SYSTEM_TEAMFOUNDATIONCOLLECTIONURI<\/span><span class=\"w\"> <\/span><span class=\"err\">`<\/span><span class=\"w\">\n        <\/span><span class=\"o\">+<\/span><span class=\"w\"> <\/span><span class=\"nv\">$<\/span><span class=\"nn\">env<\/span><span class=\"p\">:<\/span><span class=\"nv\">SYSTEM_TEAMPROJECT<\/span><span class=\"w\"> <\/span><span class=\"err\">`<\/span><span class=\"w\">\n        <\/span><span class=\"o\">+<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"\/_apis\/build\/builds?definition=\"<\/span><span class=\"w\"> <\/span><span class=\"o\">+<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"n\">Uri<\/span><span class=\"p\">]::<\/span><span class=\"n\">EscapeUriString<\/span><span class=\"p\">(<\/span><span class=\"nv\">$definition<\/span><span class=\"p\">)<\/span><span class=\"w\"> <\/span><span class=\"err\">`<\/span><span class=\"w\">\n        <\/span><span class=\"o\">+<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"&amp;<\/span><span class=\"se\">`$<\/span><span class=\"s2\">top=1\"<\/span><span class=\"w\"> <\/span><span class=\"err\">`<\/span><span class=\"w\">\n        <\/span><span class=\"o\">+<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"&amp;status=Complete\n\n<\/span><span class=\"nv\">$info<\/span><span class=\"s2\"> = Invoke-RestMethod -Uri <\/span><span class=\"nv\">$uri<\/span><span class=\"s2\"> -Headers @{ Authorization = \"<\/span><span class=\"n\">Bearer<\/span><span class=\"w\"> <\/span><span class=\"nv\">$<\/span><span class=\"nn\">env<\/span><span class=\"p\">:<\/span><span class=\"nv\">SYSTEM_ACCESSTOKEN<\/span><span class=\"s2\">\" }\nif (!<\/span><span class=\"nv\">$info<\/span><span class=\"s2\"> -Or !<\/span><span class=\"nv\">$info<\/span><span class=\"s2\">.value -Or (<\/span><span class=\"nv\">$info<\/span><span class=\"s2\">.value.Length -ne 1)) {\n    throw \"<\/span><span class=\"nx\">No<\/span><span class=\"w\"> <\/span><span class=\"nx\">unique<\/span><span class=\"w\"> <\/span><span class=\"nx\">build<\/span><span class=\"w\"> <\/span><span class=\"nx\">found<\/span><span class=\"s2\">\"\n}<\/span><\/code><\/pre><\/figure>\n\n<p>For more information on the URI and parameters see the <a href=\"https:\/\/docs.microsoft.com\/en-us\/rest\/api\/vsts\/build\/builds\/get?view=vsts-rest-4.1\">documentation<\/a>.\nIn short we\u2019re asking TFS to return the newest ($top=1) build of our build definition that has succeeded (status = Completed).<\/p>\n\n<p>Thanks to <code class=\"language-plaintext highlighter-rouge\">Invoke-RestMethod<\/code>, the result is already a PSObject and we can pick individual members:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-powershell\" data-lang=\"powershell\"><span class=\"nv\">$branch<\/span><span class=\"w\"> <\/span><span class=\"o\">=<\/span><span class=\"w\"> <\/span><span class=\"nv\">$info<\/span><span class=\"o\">.<\/span><span class=\"n\">value<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">]<\/span><span class=\"o\">.<\/span><span class=\"nf\">sourceBranch<\/span><span class=\"w\">\n<\/span><span class=\"nv\">$changeSet<\/span><span class=\"w\"> <\/span><span class=\"o\">=<\/span><span class=\"w\"> <\/span><span class=\"nv\">$info<\/span><span class=\"o\">.<\/span><span class=\"n\">value<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">]<\/span><span class=\"o\">.<\/span><span class=\"nf\">sourceVersion<\/span><\/code><\/pre><\/figure>\n\n<p>Note: Had we been using <code class=\"language-plaintext highlighter-rouge\">Invoke-WebRequest<\/code> instead, we could have used <code class=\"language-plaintext highlighter-rouge\">ConvertFrom-Json<\/code> to manually create\na PSObject from the resulting JSON-formatted string. Further note, that <code class=\"language-plaintext highlighter-rouge\">ConvertFrom-Json<\/code> has issues with\nJSON strings that are not on a single line (like formatted\/indented). So it is probably best to just use the\nnewer <code class=\"language-plaintext highlighter-rouge\">Invoke-RestMethod<\/code> instead of <code class=\"language-plaintext highlighter-rouge\">Invoke-WebRequest | ConvertFrom-Json<\/code>.<\/p>\n\n<h2 id=\"getting-the-sources\">Getting the sources<\/h2>\n\n<p>Now we need to get (\u201ccheckout\u201d) the sources for the build we found. Using TFVC this is not\nas easy as it could be, because we first need to create a workspace where we can get them\ninto. We cannot use the workspace that the agent has created for the currently, running build,\nbecause that is already in use.<\/p>\n\n<p>A typical agent source directory would be <code class=\"language-plaintext highlighter-rouge\">C:\\agent\\&lt;name&gt;\\_work\\1\\s<\/code> which the agent has\nmapped to a workspace named, say, <code class=\"language-plaintext highlighter-rouge\">ws_1_24<\/code>.<\/p>\n\n<p>For the sources we want to get, we\u2019ll need to create a directory that is outside of the above\n(because you cannot have nested workspaces) and choose a workspace name that is still unique.<\/p>\n\n<p>We choose a directory that will be <code class=\"language-plaintext highlighter-rouge\">C:\\agent\\&lt;name&gt;\\_work\\1\\_src<\/code>. This is still within the\nagent\u2019s working directory (<code class=\"language-plaintext highlighter-rouge\">C:\\agent\\&lt;name&gt;\\_work\\1<\/code>) and thus cannot interfere with others,\nbut is yet outside of the agent\u2019s workspace directory. As the workspace name we simply use\nthe name that the agent uses, plus a suffix: <code class=\"language-plaintext highlighter-rouge\">ws_1_24_src<\/code>. So also this will be unique.<\/p>\n\n<p>Finally, we do similar as the \u201cGet Sources\u201d task would do (you can check what it does by\nsetting the <code class=\"language-plaintext highlighter-rouge\">system.debug<\/code> variable to <code class=\"language-plaintext highlighter-rouge\">true<\/code> and checking your build log):<\/p>\n\n<ul>\n  <li>Create the working directory (here <code class=\"language-plaintext highlighter-rouge\">C:\\agent\\&lt;name&gt;\\_work\\1\\_src<\/code>) if it does not yet exist.<\/li>\n  <li>Check if the workspace (here <code class=\"language-plaintext highlighter-rouge\">ws_1_24_rc<\/code>) already exists.\n    <ul>\n      <li>If it exists, delete it.<\/li>\n    <\/ul>\n  <\/li>\n  <li>Create the workspace<\/li>\n  <li>Unmap the <code class=\"language-plaintext highlighter-rouge\">$\/<\/code> from the workspace, just in case. We don\u2019t want to accidentally get\nthe complete repository if something is messed up.<\/li>\n  <li>Map the \u201cbranch\u201d repository path from the previous step to the working directory.<\/li>\n  <li>Get the working directory.<\/li>\n<\/ul>\n\n<p>Before we show the actual steps, let\u2019s first talk about properly invoking <code class=\"language-plaintext highlighter-rouge\">tf.exe<\/code>,\nwhich will perform all of the above tasks for us.<\/p>\n\n<p>The first thing to know would be where <code class=\"language-plaintext highlighter-rouge\">tf.exe<\/code> is actually located, that is, what\nthe full path to the executable is. Luckily, we don\u2019t have to! TFS already did that\nduring the \u201cGet Sources\u201d step. Look at the log output carefully:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>2018-09-11T13:49:30.9421255Z ##[section]Starting: Get Sources\n2018-09-11T13:49:31.0827667Z Prepending Path environment variable with directory containing 'tf.exe'.\n...\n<\/code><\/pre><\/div><\/div>\n\n<p>So basically, for the rest of our build we can simply invoke <code class=\"language-plaintext highlighter-rouge\">tf.exe<\/code> without any path\ninformation. (In case you\u2019re curious, it is actually located in <code class=\"language-plaintext highlighter-rouge\">F:\\agent\\&lt;name&gt;\\externals\\vstsom<\/code>,\nbut I would consider this an implementation detail).<\/p>\n\n<p>Now that we know how to invoke it in principal, there are some things to take care of\nwhen doing so. Above we have already seen that <code class=\"language-plaintext highlighter-rouge\">\/loginType:OAuth<\/code> is required to use\nthe agent\u2019s access token to authenticate. Also, you should pass the <code class=\"language-plaintext highlighter-rouge\">\/noprompt<\/code> option,\nto make sure no interactive questions are asked by the tool - which makes little sense\nin an automated build.<\/p>\n\n<p>For the following, we have build the following helper functions:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-powershell\" data-lang=\"powershell\"><span class=\"kr\">function<\/span><span class=\"w\"> <\/span><span class=\"nf\">WriteCommand<\/span><span class=\"p\">(<\/span><span class=\"nv\">$commandLine<\/span><span class=\"p\">)<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n    <\/span><span class=\"n\">Write-Host<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"##[command]<\/span><span class=\"nv\">$commandLine<\/span><span class=\"s2\">\"<\/span><span class=\"w\">\n<\/span><span class=\"p\">}<\/span><span class=\"w\">\n\n<\/span><span class=\"kr\">function<\/span><span class=\"w\"> <\/span><span class=\"nf\">GetTfCommandLine<\/span><span class=\"p\">(<\/span><span class=\"nv\">$arguments<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"nv\">$needCollection<\/span><span class=\"w\"> <\/span><span class=\"o\">=<\/span><span class=\"w\"> <\/span><span class=\"bp\">$true<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"nv\">$needLogin<\/span><span class=\"w\"> <\/span><span class=\"o\">=<\/span><span class=\"w\"> <\/span><span class=\"bp\">$true<\/span><span class=\"p\">)<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n    <\/span><span class=\"nv\">$tfc<\/span><span class=\"w\"> <\/span><span class=\"o\">=<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"tf.exe vc <\/span><span class=\"nv\">$arguments<\/span><span class=\"s2\">\"<\/span><span class=\"w\">\n    <\/span><span class=\"kr\">if<\/span><span class=\"w\"> <\/span><span class=\"p\">(<\/span><span class=\"nv\">$needCollection<\/span><span class=\"p\">)<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n        <\/span><span class=\"nv\">$tfc<\/span><span class=\"w\"> <\/span><span class=\"o\">+=<\/span><span class=\"w\"> <\/span><span class=\"s2\">\" \/collection:<\/span><span class=\"nv\">$<\/span><span class=\"nn\">env<\/span><span class=\"p\">:<\/span><span class=\"nv\">SYSTEM_TEAMFOUNDATIONCOLLECTIONURI<\/span><span class=\"s2\">\"<\/span><span class=\"w\">\n    <\/span><span class=\"p\">}<\/span><span class=\"w\">\n    <\/span><span class=\"kr\">if<\/span><span class=\"w\"> <\/span><span class=\"p\">(<\/span><span class=\"nv\">$needLogin<\/span><span class=\"p\">)<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n        <\/span><span class=\"nv\">$tfc<\/span><span class=\"w\"> <\/span><span class=\"o\">+=<\/span><span class=\"w\"> <\/span><span class=\"s2\">\" \/loginType:OAuth <\/span><span class=\"se\">`\"<\/span><span class=\"s2\">\/login:.,<\/span><span class=\"nv\">$<\/span><span class=\"nn\">env<\/span><span class=\"p\">:<\/span><span class=\"nv\">SYSTEM_ACCESSTOKEN<\/span><span class=\"se\">`\"<\/span><span class=\"s2\">\"<\/span><span class=\"w\">\n    <\/span><span class=\"p\">}<\/span><span class=\"w\">\n    <\/span><span class=\"nv\">$tfc<\/span><span class=\"w\"> <\/span><span class=\"o\">+=<\/span><span class=\"w\"> <\/span><span class=\"s2\">\" \/noprompt\"<\/span><span class=\"w\">\n    <\/span><span class=\"kr\">return<\/span><span class=\"w\"> <\/span><span class=\"nv\">$tfc<\/span><span class=\"w\">\n<\/span><span class=\"p\">}<\/span><span class=\"w\">\n\n<\/span><span class=\"kr\">function<\/span><span class=\"w\"> <\/span><span class=\"nf\">RunTfCommand<\/span><span class=\"p\">(<\/span><span class=\"nv\">$message<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"nv\">$arguments<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"nv\">$needCollection<\/span><span class=\"w\"> <\/span><span class=\"o\">=<\/span><span class=\"w\"> <\/span><span class=\"bp\">$true<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"nv\">$needLogin<\/span><span class=\"w\"> <\/span><span class=\"o\">=<\/span><span class=\"w\"> <\/span><span class=\"bp\">$true<\/span><span class=\"p\">)<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n    <\/span><span class=\"n\">Write-Host<\/span><span class=\"w\"> <\/span><span class=\"nv\">$message<\/span><span class=\"w\">\n    <\/span><span class=\"nv\">$tfc<\/span><span class=\"w\"> <\/span><span class=\"o\">=<\/span><span class=\"w\"> <\/span><span class=\"n\">GetTfCommandLine<\/span><span class=\"w\"> <\/span><span class=\"nv\">$arguments<\/span><span class=\"w\"> <\/span><span class=\"nv\">$needCollection<\/span><span class=\"w\"> <\/span><span class=\"nv\">$needLogin<\/span><span class=\"w\">\n    <\/span><span class=\"n\">WriteCommand<\/span><span class=\"w\"> <\/span><span class=\"nv\">$tfc<\/span><span class=\"w\">\n    <\/span><span class=\"n\">Invoke-Expression<\/span><span class=\"w\"> <\/span><span class=\"nv\">$tfc<\/span><span class=\"w\">\n    <\/span><span class=\"kr\">if<\/span><span class=\"w\"> <\/span><span class=\"p\">(<\/span><span class=\"nv\">$LASTEXITCODE<\/span><span class=\"w\"> <\/span><span class=\"o\">-ne<\/span><span class=\"w\"> <\/span><span class=\"mi\">0<\/span><span class=\"p\">)<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n        <\/span><span class=\"kr\">throw<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"<\/span><span class=\"nv\">$message<\/span><span class=\"s2\">. Failed with exit code <\/span><span class=\"nv\">$LASTEXITCODE<\/span><span class=\"s2\">.\"<\/span><span class=\"w\">\n    <\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><span class=\"p\">}<\/span><\/code><\/pre><\/figure>\n\n<p>Note the use of the <code class=\"language-plaintext highlighter-rouge\">Write-Host \"##[command]...\"<\/code> call to log the command for diagnostics.\nIt is one of a couple of <a href=\"https:\/\/github.com\/Microsoft\/azure-pipelines-tasks\/blob\/master\/docs\/authoring\/commands.md\">logging commands<\/a>\nthat TFS supports. This one has the benefit of automatically hiding sensitive information from the output (here the system access token).\nFor example, instead of logging<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>tf.exe vc get ... \/login:.,&lt;tokenstring&gt;\n<\/code><\/pre><\/div><\/div>\n\n<p>it will automatically log only<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>tf.exe vc get ... \/login:.,*******\n<\/code><\/pre><\/div><\/div>\n\n<p>to build console and log.<\/p>\n\n<p>OK, let\u2019s look at the steps layed out above in code:<\/p>\n\n<blockquote>\n  <p>Create the working directory (here <code class=\"language-plaintext highlighter-rouge\">C:\\agent\\&lt;name&gt;\\_work\\1\\_src<\/code>) if it does not yet exist.<\/p>\n<\/blockquote>\n\n<figure class=\"highlight\"><pre><code class=\"language-powershell\" data-lang=\"powershell\"><span class=\"nv\">$WorkingDir<\/span><span class=\"w\"> <\/span><span class=\"o\">=<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"n\">System.IO.Path<\/span><span class=\"p\">]::<\/span><span class=\"n\">GetFullPath<\/span><span class=\"p\">(<\/span><span class=\"nv\">$<\/span><span class=\"nn\">env<\/span><span class=\"p\">:<\/span><span class=\"nv\">BUILD_SOURCESDIRECTORY<\/span><span class=\"w\"> <\/span><span class=\"o\">+<\/span><span class=\"w\"> <\/span><span class=\"s1\">'\\..\\_src'<\/span><span class=\"p\">)<\/span><span class=\"w\">\n\n<\/span><span class=\"kr\">if<\/span><span class=\"w\"> <\/span><span class=\"p\">(<\/span><span class=\"o\">!<\/span><span class=\"p\">(<\/span><span class=\"n\">Test-Path<\/span><span class=\"w\"> <\/span><span class=\"nt\">-Path<\/span><span class=\"w\"> <\/span><span class=\"nv\">$WorkingDir<\/span><span class=\"w\"> <\/span><span class=\"nt\">-PathType<\/span><span class=\"w\"> <\/span><span class=\"nx\">Container<\/span><span class=\"p\">))<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n    <\/span><span class=\"n\">Write-Host<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"Creating working directory '<\/span><span class=\"nv\">$WorkingDir<\/span><span class=\"s2\">'\"<\/span><span class=\"w\">\n    <\/span><span class=\"n\">mkdir<\/span><span class=\"w\"> <\/span><span class=\"nv\">$WorkingDir<\/span><span class=\"w\">\n<\/span><span class=\"p\">}<\/span><span class=\"w\"> <\/span><span class=\"kr\">else<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n    <\/span><span class=\"n\">Write-Host<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"Cleaning working directory '<\/span><span class=\"nv\">$WorkingDir<\/span><span class=\"s2\">'\"<\/span><span class=\"w\">\n    <\/span><span class=\"n\">del<\/span><span class=\"w\"> <\/span><span class=\"nt\">-Force<\/span><span class=\"w\"> <\/span><span class=\"nt\">-Recurse<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"<\/span><span class=\"nv\">$WorkingDir<\/span><span class=\"s2\">\\*.*\"<\/span><span class=\"w\">\n<\/span><span class=\"p\">}<\/span><\/code><\/pre><\/figure>\n\n<blockquote>\n  <p>Check if the workspace (here <code class=\"language-plaintext highlighter-rouge\">ws_1_24_rc<\/code>) already exists. If it exists, delete it.<\/p>\n<\/blockquote>\n\n<figure class=\"highlight\"><pre><code class=\"language-powershell\" data-lang=\"powershell\"><span class=\"nv\">$WorkspaceName<\/span><span class=\"w\"> <\/span><span class=\"o\">=<\/span><span class=\"w\"> <\/span><span class=\"nv\">$<\/span><span class=\"nn\">env<\/span><span class=\"p\">:<\/span><span class=\"nv\">BUILD_REPOSITORY_TFVC_WORKSPACE<\/span><span class=\"w\"> <\/span><span class=\"o\">+<\/span><span class=\"w\"> <\/span><span class=\"s1\">'_src'<\/span><span class=\"w\">\n\n<\/span><span class=\"n\">Push-Location<\/span><span class=\"w\"> <\/span><span class=\"nv\">$WorkingDir<\/span><span class=\"w\">\n\n<\/span><span class=\"n\">Write-Host<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"Looking for existing workspace '<\/span><span class=\"nv\">$WorkspaceName<\/span><span class=\"s2\">'\"<\/span><span class=\"w\">\n<\/span><span class=\"nv\">$getWorkspaces<\/span><span class=\"w\"> <\/span><span class=\"o\">=<\/span><span class=\"w\"> <\/span><span class=\"n\">GetTfCommandLine<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"workspaces \/format:XML\"<\/span><span class=\"w\">\n<\/span><span class=\"n\">WriteCommand<\/span><span class=\"w\"> <\/span><span class=\"nv\">$getWorkspaces<\/span><span class=\"w\">\n<\/span><span class=\"nv\">$workspaces<\/span><span class=\"w\"> <\/span><span class=\"o\">=<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"n\">xml<\/span><span class=\"p\">](<\/span><span class=\"n\">Invoke-Expression<\/span><span class=\"w\"> <\/span><span class=\"nv\">$getWorkspaces<\/span><span class=\"p\">)<\/span><span class=\"w\">\n<\/span><span class=\"nv\">$existingWorkspace<\/span><span class=\"w\"> <\/span><span class=\"o\">=<\/span><span class=\"w\"> <\/span><span class=\"nv\">$workspaces<\/span><span class=\"o\">.<\/span><span class=\"nf\">workspaces<\/span><span class=\"o\">.<\/span><span class=\"nf\">workspace<\/span><span class=\"w\"> <\/span><span class=\"o\">|<\/span><span class=\"w\"> <\/span><span class=\"n\">Where-Object<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\"> <\/span><span class=\"bp\">$_<\/span><span class=\"o\">.<\/span><span class=\"nf\">name<\/span><span class=\"w\"> <\/span><span class=\"o\">-eq<\/span><span class=\"w\"> <\/span><span class=\"nv\">$WorkspaceName<\/span><span class=\"w\"> <\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><span class=\"kr\">if<\/span><span class=\"w\"> <\/span><span class=\"p\">(<\/span><span class=\"nv\">$existingWorkspace<\/span><span class=\"p\">)<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n    <\/span><span class=\"n\">RunTfCommand<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"Removing existing workspace '<\/span><span class=\"nv\">$WorkspaceName<\/span><span class=\"s2\">'\"<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"workspace \/delete <\/span><span class=\"se\">`\"<\/span><span class=\"si\">$(<\/span><span class=\"nv\">$WorkspaceName<\/span><span class=\"si\">)<\/span><span class=\"s2\">;<\/span><span class=\"si\">$(<\/span><span class=\"nv\">$existingWorkspace<\/span><span class=\"o\">.<\/span><span class=\"nf\">owner<\/span><span class=\"si\">)<\/span><span class=\"se\">`\"<\/span><span class=\"s2\">\"<\/span><span class=\"w\">\n<\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><span class=\"n\">RunTfCommand<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"Creating workspace '<\/span><span class=\"nv\">$WorkspaceName<\/span><span class=\"s2\">'\"<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"workspace \/new \/location:local \/permission:Public <\/span><span class=\"nv\">$WorkspaceName<\/span><span class=\"s2\">\"<\/span><\/code><\/pre><\/figure>\n\n<p>A couple of things are going on here.<\/p>\n\n<ul>\n  <li>We create the name of our workspace using the agent\u2019s workspace name, plus our suffix <code class=\"language-plaintext highlighter-rouge\">_src<\/code>.<\/li>\n  <li>We need to change to the working directory. As most of the following commands operate\non this implicitly.<\/li>\n  <li>We execute the <code class=\"language-plaintext highlighter-rouge\">tf vc workspaces \/format:XML<\/code> command. So in PowerShell we can cast the result to XML\nand then operate on the result as an object.<\/li>\n  <li>If our workspace already exists, we delete it using the <code class=\"language-plaintext highlighter-rouge\">tf vc workspace \/delete<\/code> command.<\/li>\n  <li>Finally, we (re)create the workspace using the <code class=\"language-plaintext highlighter-rouge\">tf vc workspace \/new<\/code> command.<\/li>\n<\/ul>\n\n<blockquote>\n  <p>Unmap the <code class=\"language-plaintext highlighter-rouge\">$\/<\/code> from the workspace, just in case. We don\u2019t want to accidentally get the complete repository if something is messed up.\nMap the \u201cbranch\u201d repository path from the previous step to the working directory.<\/p>\n<\/blockquote>\n\n<figure class=\"highlight\"><pre><code class=\"language-powershell\" data-lang=\"powershell\"><span class=\"n\">RunTfCommand<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"Unmapping workfolder\"<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"workfold \/unmap \/workspace:<\/span><span class=\"nv\">$WorkspaceName<\/span><span class=\"s2\"> <\/span><span class=\"err\">$<\/span><span class=\"s2\">\/\"<\/span><span class=\"w\">\n<\/span><span class=\"n\">RunTfCommand<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"Mapping workfolder\"<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"workfold \/map <\/span><span class=\"se\">`\"<\/span><span class=\"nv\">$branch<\/span><span class=\"se\">`\"<\/span><span class=\"s2\"> <\/span><span class=\"se\">`\"<\/span><span class=\"nv\">$WorkingDir<\/span><span class=\"se\">`\"<\/span><span class=\"s2\"> \/workspace:<\/span><span class=\"nv\">$WorkspaceName<\/span><span class=\"s2\">\"<\/span><\/code><\/pre><\/figure>\n\n<blockquote>\n  <p>Get the working directory.<\/p>\n<\/blockquote>\n\n<figure class=\"highlight\"><pre><code class=\"language-powershell\" data-lang=\"powershell\"><span class=\"n\">RunTfCommand<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"Getting sources\"<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"get \/version:C<\/span><span class=\"nv\">$changeSet<\/span><span class=\"s2\"> \/recursive <\/span><span class=\"se\">`\"<\/span><span class=\"nv\">$WorkingDir<\/span><span class=\"se\">`\"<\/span><span class=\"s2\"> \/overwrite\"<\/span><span class=\"w\"> <\/span><span class=\"bp\">$false<\/span><\/code><\/pre><\/figure>\n\n<h2 id=\"where-are-we-now\">Where are we now<\/h2>\n\n<p>At this point the build process has gotten the sources in the version (i.e. change set)\nthat has been used for the latest successful build at this time.<\/p>\n\n<p>Now, we can invoke the generation of the actual documentation using these sources. What actually\nhappens here depends on your build scripts, that you have for generation your documentation\nanyway.<\/p>\n\n<p>You should put the above PowerShell code into a script, say <code class=\"language-plaintext highlighter-rouge\">Prepare.ps1<\/code>, put it into your repository\n(at a place different than the sources you want to get later on) and invoke it as a step in the build pipe\nline.<\/p>\n\n<p>For example, your build pipeline could look like this:<\/p>\n\n<ol>\n  <li>\u201cGet Sources\u201d - this will fetch the <code class=\"language-plaintext highlighter-rouge\">$\/MyProject\/Tools\/<\/code> folder, which contains the <code class=\"language-plaintext highlighter-rouge\">Prepare.ps1<\/code> script\nwith the code from above.<\/li>\n  <li>\u201cRun PowerShell\u201d - Create a task that will run the <code class=\"language-plaintext highlighter-rouge\">$(build.sourcesDirectory)\\Prepare.ps1<\/code> script;\nin real live you might probably want to pass some of the things we \u201chardcoded\u201d above (like the name\nof the build definition to find the newest build of), as build process variables.<\/li>\n  <li>Run whatever task\/step you want based on the sources that are now in <code class=\"language-plaintext highlighter-rouge\">$(build.sourcesDirectory)\\..\\_src<\/code>.<\/li>\n<\/ol>","author":{"name":{}},"category":[{"@attributes":{"term":"tfs"}},{"@attributes":{"term":"tf.exe"}},{"@attributes":{"term":"azure"}},{"@attributes":{"term":"devops"}},{"@attributes":{"term":"tfsbuild"}},{"@attributes":{"term":"rest"}},{"@attributes":{"term":"api"}}],"summary":"We have a build that just generates the API documentation for a software. It does so using docfx.exe, which does not require the binaries, but only the source code."},{"title":"Additional Badges for TFS Builds","link":{"@attributes":{"href":"https:\/\/cklutz.github.io\/2017\/09\/16\/additional-badges-for-tfs-builds\/","rel":"alternate","type":"text\/html","title":"Additional Badges for TFS Builds"}},"published":"2017-09-16T09:30:00+00:00","updated":"2017-09-16T09:30:00+00:00","id":"https:\/\/cklutz.github.io\/2017\/09\/16\/additional-badges-for-tfs-builds","content":"<p>TFS Build V.Next supports build badges that you can easily embed into your web packages\nto display the status of your latest build.<\/p>\n\n<p>The one badge that comes out of the box is the build status, for example <img src=\"https:\/\/cklutz.github.io\/images\/buildstatus_badge.svg\" alt=\"buildstatus\" \/>.<\/p>\n\n<p>It would be nice if there would be additional information, for example for <a href=\"https:\/\/cklutz.github.io\/2017\/09\/06\/a-tfs-build-task-for-opencover\/\">code coverage<\/a>, the time a build finished, or the latest build ID it has.<\/p>\n\n<p>TFS is not extensible in this regard, but what is doable, is to write a standalone service that can\nproduce such badges, but gathers the necessary information by connecting itself to TFS (or VSTS) using\nthe REST API to get the necessary information.<\/p>\n\n<p>I created such a service using ASP.NET Core 2.0. It is available <a href=\"https:\/\/github.com\/cklutz\/TfsInfoService\/\">here<\/a>,\nincluding further information on how to set it up, or generate badges.<\/p>\n\n<p>Here are some example badges you can get with this service:<\/p>\n\n<table>\n  <thead>\n    <tr>\n      <th style=\"text-align: left\">Type<\/th>\n      <th style=\"text-align: right\">Sample<\/th>\n    <\/tr>\n  <\/thead>\n  <tbody>\n    <tr>\n      <td style=\"text-align: left\">buildnumber<\/td>\n      <td style=\"text-align: right\"><img src=\"https:\/\/cklutz.github.io\/images\/buildnumber_badge.svg\" alt=\"buildnumber\" \/><\/td>\n    <\/tr>\n    <tr>\n      <td style=\"text-align: left\">duration<\/td>\n      <td style=\"text-align: right\"><img src=\"https:\/\/cklutz.github.io\/images\/duration_badge.svg\" alt=\"duration\" \/><\/td>\n    <\/tr>\n    <tr>\n      <td style=\"text-align: left\">finishdate<\/td>\n      <td style=\"text-align: right\"><img src=\"https:\/\/cklutz.github.io\/images\/finishdate_badge.svg\" alt=\"finishdate\" \/><\/td>\n    <\/tr>\n    <tr>\n      <td style=\"text-align: left\">coverage<\/td>\n      <td style=\"text-align: right\"><img src=\"https:\/\/cklutz.github.io\/images\/coverage_badge.svg\" alt=\"coverage\" \/><\/td>\n    <\/tr>\n    <tr>\n      <td style=\"text-align: left\">best-coverage<\/td>\n      <td style=\"text-align: right\"><img src=\"https:\/\/cklutz.github.io\/images\/best-coverage_badge.svg\" alt=\"best-coverage\" \/><\/td>\n    <\/tr>\n    <tr>\n      <td style=\"text-align: left\">custom<\/td>\n      <td style=\"text-align: right\"><img src=\"https:\/\/cklutz.github.io\/images\/custom_sample_badge.svg\" alt=\"custom\" \/><\/td>\n    <\/tr>\n  <\/tbody>\n<\/table>","author":{"name":{}},"category":[{"@attributes":{"term":"tfs"}},{"@attributes":{"term":"build"}},{"@attributes":{"term":".net"}}],"summary":"TFS Build V.Next supports build badges that you can easily embed into your web packages to display the status of your latest build."},{"title":"A TFS build task for OpenCover","link":{"@attributes":{"href":"https:\/\/cklutz.github.io\/2017\/09\/06\/a-tfs-build-task-for-opencover\/","rel":"alternate","type":"text\/html","title":"A TFS build task for OpenCover"}},"published":"2017-09-06T09:30:00+00:00","updated":"2017-09-06T09:30:00+00:00","id":"https:\/\/cklutz.github.io\/2017\/09\/06\/a-tfs-build-task-for-opencover","content":"<p>TFS on premise or Visual Studio Team Services (VSTS) - I use them interchangeably from now on - provide a build task to run VSTest. This task optionally allows one to enable code coverage. However, this feature requires one to have a Visual Studio Enterprise license \/ installation on the build agent.<\/p>\n\n<p>Luckily, as so many times, there are open source alternatives available. I\u2019m sure their are more, but I\u2019m going with <a href=\"https:\/\/github.com\/OpenCover\/opencover\">OpenCover<\/a>. Mostly, because I have prior experience with it.<\/p>\n\n<p>As OpenCover works, it starts the actual test runner (<code class=\"language-plaintext highlighter-rouge\">vstest.console.exe<\/code> for VSTest) and by doing that it can sort of hook into the actual test code being called and thus figure out coverage information - now, that is a pretty non technical explanation here, but you could find out more looking at OpenCover\u2019s source or docs. The important point here is that, you have to run your test runner from OpenCover.<\/p>\n\n<p>Why is that an issue? Well, because a potential build task for OpenCover would have to no only provide the necessary options to configure and run OpenCover itself, but would have to sort of replicate all the options and features that the VSTest build task already has.<\/p>\n\n<p>If you look at the <a href=\"https:\/\/github.com\/Microsoft\/vsts-tasks\/tree\/master\/Tasks\/VsTest\">code<\/a> there is a <em>lot<\/em> that is done there.<\/p>\n\n<p>This is the main reason why I refrained from create such a build task for some time now. However, circumstances caused me to change my mind and a give it try.<\/p>\n\n<p>At work we wanted to integrate OpenCover into our build process, but preferably not run tests twice (once with the stock test task and then once again with the OpenCover task just to gather coverage information). That meant, that OpenCover task would have to emulate as much as possible from the VSTest task. I finally settled on the features of the <a href=\"https:\/\/github.com\/Microsoft\/vsts-tasks\/tree\/releases\/m109\/Tasks\/VsTest\">version<\/a> task that was shipped with TFS 2017 Update 1. The code is a lot easier (as in smaller) as for the 2.x version of the task. <del>As said, the OpenCover task emulates most of the VSTest 1.x settings, currently excluding the \u201cRun in parallel\u201d feature - but work is not done yet and I might add it in the time coming.<\/del> <strong>UPDATE:<\/strong> 7th Sept: Parallel execution of test cases is now supported.<\/p>\n\n<p>You can find the results (i.e. code, etc.) right <a href=\"https:\/\/github.com\/cklutz\/my-vsts-tasks\">here<\/a>, along with instructions on how to upload it into your environment. (Many thanks to Mi\u0142osz Piechocki and his informative <a href=\"https:\/\/codewithstyle.info\/setting-up-coverage-reports-on-vsts-with-opencover\/\">article<\/a>, that gave a good starting point, integrating OpenCover using a TFS PowerShell task with a script.)<\/p>\n\n<p>The configuration settings look somewhat like this (depending on the exact TFS version or iteration of Visual Studio Team Services):<\/p>\n\n<p><img src=\"https:\/\/cklutz.github.io\/images\/opencovertask_options.png\" alt=\"OpenCoverTask Options\" \/><\/p>\n\n<p>The result in the build summary looks like this:<\/p>\n\n<p><img src=\"https:\/\/cklutz.github.io\/images\/opencover_summary_view.png\" alt=\"OpenCoverTask Summary\" \/><\/p>\n\n<p>Additionally, you can download a ZIP file containing a report generated by <a href=\"https:\/\/github.com\/danielpalme\/ReportGenerator\">ReportGenerator<\/a> during the run.<\/p>","author":{"name":{}},"category":[{"@attributes":{"term":"tfs"}},{"@attributes":{"term":"build"}},{"@attributes":{"term":"opencover"}},{"@attributes":{"term":".net"}}],"summary":"TFS on premise or Visual Studio Team Services (VSTS) - I use them interchangeably from now on - provide a build task to run VSTest. This task optionally allows one to enable code coverage. However, this feature requires one to have a Visual Studio Enterprise license \/ installation on the build agent."}]}