String.CopyToUpper

So I just finished Writing High-Performance .NET Code, which is dry at times, but contains nuggets of wisdom.

One of them was something I should’ve understood, but really only now dawned on me.

Strings are immutable.

But like, really immutable.

This means any operation on them like calling ToUpper, results in the creation of a new string.

This behavior makes sense ofcourse, since they are immutable. You’re even reminded in the docs:

This method does not modify the value of the current instance. Instead, it returns a new string in which all characters in the current instance are converted to uppercase.

https://msdn.microsoft.com/en-us/library/ewdd6aed(v=vs.110).aspx

But intuitively while programming in your IDE, I will easily forget that I’m creating a new string. Unwittingly you could be increasing pressure on the garbage collector, deteriorating application performance, if you don’t pay attention in a hot application loop.

And I understand the string abstraction shouldn’t leak implementation details. But on the other hand, a name that is honest about what you are doing like .CopyToUpper, would save a lot of wasted CPU cycles in applications passed and to come ūüôā

Theme music for this blog post

Advertisements
String.CopyToUpper

.NET HttpClient should be shared

While toying around with Azure Functions for some app idea, I came across some documentation on best practices for using it.

The article continues further into detail on Patterns and Practices HTTP Performance Optimizations where I discovered something about HttpClient I would have never guessed:

[HttpClient is] intended to be instantiated once and reused throughout the lifetime of an application. However, it’s a common misunderstanding that these classes should be acquired only as necessary and released quickly

new HttpClient object is created for each user request. Under heavy load, the web server may exhaust the number of available sockets, resulting in SocketException errors.

The HttpClient class is designed to be shared rather than pooled

Well whaddayaknow, until know I have always designed my applications to keep the HttpClient as short-lived as possible, always wrapping it in a using block to ensure proper disposal.

Turns out this is an anti-pattern, they actually advice you to create a static readonly HttpClient, which you reuse in your app.

Going to apply some improvements right now!

Theme music for this blog post

.NET HttpClient should be shared

GraphQL as a silver bullet

I learned about GraphQL somewhere this year and thought it was genius.

Check out the official site to see what it is, but in short, you call an API and specify in your request the response contract you expect. This sounds lovely for supporting a wide range of use cases with a single API. No upfront rigid API contract, let the clients decide how they want the data.

So in my mind this sounded like the future for providing flexible API’s.

Thankfully the Internet provided some critical thinking, check out this presentation for a guide on when REST still trumps GraphQL:

https://www.infoq.com/presentations/api-rest-graphql

Highlights are your ability to rely on HTTP caching with REST and the self describing nature of HTTP verbs, things you lose with GraphQL.

And so the universally correct answer continues to be “its depends”.

Theme music for this blog post

GraphQL as a silver bullet

Conversational website

I’ve been exploring the topic of chatbots with Designing Bots: Creating Conversational Experiences , Bot Business 101 and Chatbots for eCommerce: Learn how to build a virtual shopping assistant

While researching further online, I came across this developer’s personal website:

https://azumbrunnen.me/

The site is a chatbot that introduces him and allows you to learn more about him and get in touch. That’s a great way to present your work and what you can do, especially if your a bot developer!

Theme music for this blog post

Conversational website

Fix unable to access /.config/git/config in VS2017 as administrator

The GIT integration in Visual Studio 2017 seems to have regressed back to the initial buggy state in Visual Studio 2015.

A new issue I had when starting VS2017 as an administrator was:

Git failed with a fatal error
fatal: unable to access '\/.config/git/config': Invalid argument

This was visible in the output window, if you miss it you’re clueless.

The result is the GIT integration did not work at all and I had to fall back to VS2015 when I needed a UI to to GIT operations (e.g. merging)

You can fix this by setting an environment variable HOME that points to your folder with git repositories.

Theme music for this blog post

Fix unable to access /.config/git/config in VS2017 as administrator

ASP.NET Core does not start up when static file path does not exist

So by default ASP.NET Core only serves static files from wwwroot. Since I’m sold by architecture in vertical slices, I like to have feature folders.

This means I will put the controller, model, view and any static files in a single feature folder. To expose these static files, I just configure:

public void Configure(...)
{
...
  app.UseStaticFiles(new StaticFileOptions
  {
    FileProvider = new PhyiscalPhileProvider(
      Path.Combine(Directory.GetCurrentDirectory(), 
        @"Features\MyFeature\www)),
      Request.Path = new PathString("/MyFeature")
  });
...
}

Now, if the “Features\MyFeature\www” part is an invalid path, you will get a clear exception when you are debugging. Typically however, the static files will be file types that are not included in the build output by default (e.g. js)

So on your machine while you are developing everything is fine. But when you publish, the static files are not included, which results in an empty directory which is then not  included, which ends with the web application not starting up!

In my case, this was on an Azure App Service, which showed a nice custom error page without any clue. Took me some troubleshooting to get the logs that pointed me to the root cause.

Ah well, TheMoreYouKnow.

Theme music for this blog post

ASP.NET Core does not start up when static file path does not exist

VSTS build agent without Visual Studio

Visual Studio Team Services (VSTS) come with hosted agents that allow you to build .NET applications out of the box.

If you want to run an on-premise agent however, and prefer not to install Visual Studio on it, the setup might not be so straightforward. Maarten Balliauw covered this topic perfectly.

This is what got me up and running, after the default on-premise agent installation:

  1. Install the correct .NET SDK
    1. For example a 4.6.2 app would require the .NET Framework 4.6.2 Developer Pack
    2. This installs the required reference assemblies under Program Files, which are used by the build to compile your application
    3. Don’t confuse this with the .NET framework, which is just the runtime to execute a compiled .NET app
  2. MSBuild tools to use msbuild
    1. For Visual Studio 2015 and previous versions, get the Microsoft Build Tools, so for VS2015 install Microsoft Build Tools 2015
    2. For 2017 they changed up the name to Build Tools for Visual Studio 2017 (scroll down and look under Other Tools and Frameworks)
      1. In response to Maarten’s post, Phil Haacked opened a uservoice which resulted in this which attempts to improve things, nice!
  3. MSTest if you use it
    1. The Agents for Microsoft Visual Studio 2015 installs mstest.exe, the description focuses on distributing automated tests to remote machines, don’t worry, it is what you need

So MSBuild uses targets to do its job, which usually come with installations of other software. I got away without installing extra tools, by using these nugets in my project instead:

  1. MSBuild targets for WebApplications
  2. TypeScript MSBuild nuget
    1. Instead of installing the TypeScript tools on the server, this nuget provides everything you need

Then after installing node, I run Grunt from the local node_modules directory of my application, instead of depending on grunt-cli to be installed globally.

Good luck!

Theme music for this blog post

VSTS build agent without Visual Studio