New Azure Functions SDK and VSTS - how to build your functions?

With recent release of Visual Studio 2017 15.3 we're given the possibility to use Azure Functions SDK, which is now fully integrated with IDE. This improves development greatly as we no longer have to maintain function.json by ourselves and are able to run function locally using in-built runtime. How about integrating new features into existing CI/CD pipeline? Well, there're some gotchas, fortunately you can easily configure things so everything runs smoothly.

Building a function project

When you create a new function project in VS you're given the options to easily add new functions with a boilerplate code. If you investigate .csproj file(last time I wrote about building Azure Functions using VSTS it was a .funproj file), you'll realize, that it differs greatly compared to legacy .csproj files:

/
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
  </PropertyGroup>
  <ItemGroup>    
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.0" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="Microsoft.CSharp" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>

Considering new structure you can imagine, that it's going to give us some troubles. If you try to build it in VSTS, you'll get following error:

/
Error MSB4066: The attribute "Version" in element <PackageReference> is unrecognized.

Apparently using a default agent queue won't work here - agents don't have proper targets, which are required to build a project(if you build a project locally, you can find those targets in C:\Users\{USER}\.nuget\packages\microsoft.net.sdk.functions\1.0.0\build\netstandard1.0\Microsoft.NET.Sdk.Functions.Build.targets). It'd possible to just import them and run on a default agent, still this looks like a workaround. However, there's an easier solution - during scheduling of your build you can choose a different queue:

Let's try to build our project once more...

/
 Error MSB4041: The default XML namespace of the project must be the MSBuild XML namespace. If the project is authored in the MSBuild 2003 format, please add xmlns="http://schemas.microsoft.com/developer/msbuild/2003" to the <Project> element. If the project has been authored in the old 1.0 or 1.2 format, please convert it to MSBuild 2003 format.

There's still a problem - MSBuild is not able to determine what kind of XML is this. To fix this you can just modify <Project> node to following:

/
<Project Sdk="Microsoft.NET.Sdk" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

Now your project should build correctly:

When working on this solution I found, that some people tried to build new Azure Functions projects using dotnet build step in VSTS - unfortunately this didn't work for me. Let me know if you found another way of integrating new version of .csproj with VSTS!

Azure Table Storage good practices - Intra-partition secondary index pattern

Searching for records in Table Storage is super fast when using Partition and Row Keys. For most scenarios such setup will be sufficient - you either don't need additional properties to filter with or you're interested in large portions of data, which will be processed in your code. However, sometimes you'd like to make it possible to perform quick filtering using custom column. This is where intra-partition secondary index pattern helps.

The problem

The reason why queries using PKs and RKs are so fast lies behind the fact, that Table Storage automatically indexes entities using those columns. This is how it was designed and developed - in reality most scenarios are covered with this setup. On the other hand, we'd like to enable ourselves to create a table, which will keep superb performance and still allow querying other columns(like FirstName, City or Street in Employee table). Of course it's possible to perform partition scans and filter proper values in your code, yet additional overhead could be something, you cannot accept. We have to design a table in such way we'll somehow incorporate additional filters into internal design of Table Storage.

The solution

The solution here(as most solutions I present) is pretty simple. If we know, that indexes are created using PKs and RKs, we have to add additional values to them. This will allow us to take advantage of the indexing feature and let avoid additional overhead during filtering values. Let's consider following example:

/
PK	| RK	| FirstName	| LastName
employee	1	John	Doe
employee	2	Jane Doe

If we'd like to filter retrieved records using LastName = `Doe` then it'd force us to do it on our side, possible fetching more records than we need and lowering performance of our application. Now let's redesign it a little bit:

/
PK	| RK	| FirstName	| LastName
employee	1	John	Doe
employee	2	Jane Doe
employee	lastname_Doe	John	Doe
employee	lastname_Doe	Jane Doe

Now we can perform following filtering on this table:

/
$filter=(PartitionKey eq 'employee') and (RowKey eq 'lastname_Doe')

Retrieving only those records we need. 

Summary

As you can see small changes in Table Storage design can result in significant performance improvements. Some consideration here should be focused on possible duplication of data, which has to be handled on your side. If performance is important for you, this is a small price for the overall "level-up" in areas you care the most.