Monthly Archives: February 2018

Embedding files and/or templates within templates using Velcocity

We can embed a file or other templates within Velocity templates – this is obviously useful for reusing snippets of code/text or whatever in more than file. We still need to create the context mappings for all the template file that form out overall template – in other words we supply the mappings the container template and all of the embedded templates.

To embed a template we simply use the #include or #parse directives. #include is used to pull in a template or file without transforming it using the template engine, i.e. any variables etc. will not be transformed or rendered by Velocity whereas #parse will transform any Velocity variables or code.

Taking the template (template.txt.vm) from my previous post, which looked like this

Hello $name,

This is a $template_name template.

we might break this template into the following two templates

salutation.txt.vm

Hello $name,

and template.txt.vm becomes

#parse("templates/salutation.txt.vm")

This is a $template_name template.

This will result in the combined and transformed template.

Using the Velocity template engine

Velocity is a template engine written for Java. It allows us to take files (or strings) with embedded Velocity Template Language (VTL) code and transform the files (or strings) based upon supplied values.

Let’s create a simple template file (by default we use the extension .vm and a good convention is to append it to the original extension file type, so you know what type of file you’re transforming) then we’ll use Velocity to transform the file.

We’ll create a standard Maven layout project

  • From IntelliJ create a new project and select Maven
  • In the src/test folder add a new directory names resources
  • Mark the directory as test resources
  • Add a folder to resources named templates
  • Add the file template.txt.vm

My template.vm looks like this

Hello $name,

This is a $template_name template.

As you’ve probably guessed the $ prefix denotes a variable which we will supply to Velocity.

The first thing we need to do is create the Velocity engine and then set it up to allow us to load our files from the resources folder, here’s the code for this

VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.setProperty(
   "resource.loader", 
   "file");
velocityEngine.setProperty(
   "file.resource.loader.class", 
   "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
velocityEngine.setProperty(
   "file.resource.loader.path", 
   "src/test/resources");

Next up we create a VelocityContext which we use to supply our mappings to the variable names used within the template file. We do not include the $ when supplying the variable name. We simply supply key value pairs, where the key is a String which represents the variable name and the value is what we want to replace it with (this is an Object which can be more complex than just a String). For this example our value will just be a String, so we have the following

VelocityContext velocityContext = new VelocityContext();
velocityContext.put("name", "PutridParrot");
velocityContext.put("template_name", "MyPutridParrotTemplate");

Note: If we do not add a context mapping then the result will be the original $variable_name, unchanged.

Finally, we want to actually “merge” (as Velocity calls the process) our context against the template and at the same time we’re going to convert the resultant “merged” template into a string

StringWriter writer = new StringWriter();
Template template = velocityEngine.getTemplate("templates/template.txt.vm");
template.merge(velocityContext, writer);

// to prove it worked...
System.out.println(writer.toString());

Running the above code we will get the output (as you would expect)

Hello PutridParrot,

This is a MyPutridParrotTemplate template.

Taking things a little further

As mentioned previously the value stored within the context is an Object, so we can store an array of values if we want.

Let’s assume we have the following template file

Hello $name,

My templates

#foreach( $t in $template_name)
    * $t
#end

As you can see the VTL supports foreach loops, so if we change the code that assigns the template_name to the context to look like this

ArrayList<String> list = new ArrayList<>();
list.add("Putrid");
list.add("Parrot");

velocityContext.put("template_name", list);

Our output will now be

Hello PutridParrot,

My templates

    * Putrid
    * Parrot

Take a look at the VTL reference for more options.

And finally

Finally for this post, it’s not unusual to have a string that represents the template, for example, maybe we get our templates from a web service or just load from another file location into memory.

Here’s the full code for this

String templateString = "Hello $name,\n\nThis is a $template_name template.";

RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
StringReader reader = new StringReader(templateString);
SimpleNode node = runtimeServices.parse(reader, "Velocity Template");

VelocityContext velocityContext = new VelocityContext();
velocityContext.put("name", "PutridParrot");
velocityContext.put("template_name", "MyPutridParrotTemplate");

StringWriter writer = new StringWriter();
Template template = new Template();
template.setRuntimeServices(runtimeServices);
template.setData(node);
template.initDocument();

template.merge(velocityContext, writer);

// to prove it worked...
System.out.println(writer.toString());

The key differences are how we create the SimpleNode and then create the Template.

Duplicate step definitions in Gherkin & Cucumber (using IntelliJ)

Continuing from my last post Gherkin & Cucumber in Java (using IntelliJ).

Let’s assume you have a project set-up as per my previous post, delete any existing feature files and let’s create two very simple and very similar feature files

Feature: Calculator Add

  Scenario: Add two numbers
    Given Two input values, 1 and 2
    When I add the two values
    Then I expect the result 3
Feature: Calculator Subtract

  Scenario: Subtract two numbers
    Given Two input values, 1 and 2
    When I subtract the two values
    Then I expect the result -1

When we start creating our step definition files we might start with the Add feature and create something like AddStepdefs.java which includes the following code

package com.putridparrot;

import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class AddStepdefs {
    @Given("^Two input values, (-?\\d+) and (-?\\d+)$")
    public void twoInputValuesAnd(int a, int b) throws Throwable {
    }

    @When("^I add the two values$")
    public void iAddTheTwoValues() throws Throwable {
    }

    @Then("^I expect the result (-?\\d+)$")
    public void iExpectTheResult(int r) throws Throwable {
    }
}

Next we might start creating the step definitions file for the Subtract feature and notice, IntelliJ shows one step as already existing (i.e. it’s not got a highlight background colour). If you press the CTRL key and move the mouse over the step which appear to exist already (i.e. Two input values, 1 and 2) a hyperlink line will appear, click this and it will take you to the previously added step in the AddStepdefs file.

What’s happening here is that whilst we might define separate classes per feature (which may well seem a logical way to write our test code/step definitions), Cucumber is actually matching to methods based upon the RegEx within the annotations, i.e. Given Two input values, 1 and 2 maps to

@Given("^Two input values, (-?\\d+) and (-?\\d+)$")
public void twoInputValuesAnd(int a, int b) throws Throwable {
}

Cucumber doesn’t actually take notice of the Given/When/Then annotations for matching the method to the line of Gherkin code.

Let us assume that we simply copy the missing step into the SubtractStepdefs.java file, we now have duplicate step definitions according to Cucumber, which is ofcourse correct if we think that each step is in essence globally scoped by Cucumber.

Or to put it another way, Cucumber will search through all the packages within the “Glue” package(s) to locate matching RegEx’s. If it finds more than one matching RegEx we get a duplicate step error. Here’s the (truncated) error that Cucumber will display for us when we try to run all features.

Exception in thread "main" cucumber.runtime.DuplicateStepDefinitionException: Duplicate step definitions in com.putridparrot.SubtractStepdefs.twoInputValuesAnd(int,int)

Handling duplicate step definitions

So how do we resolve a situation where we want to run all features and we have duplicate steps?

The easiest solution is, ensure you never have duplicate steps unless you intended to reuse the same step definition code – the general idea is to think of the language used to define a step in Gherkin as a specific task and another step using the same text is really the same task (at least when associated with the same package).

Ofcourse we can have duplicate steps in different packages, we just need to ensure we run features against on that package using the Glue option, i.e. do not reference all packages but just point to the one’s specific to the features.

If we therefore have the code like this in AddStepdefs and not in SubtractStepdefs

public class AddStepdefs {
   private int value1;
   private int value2;

   @Given("^Two input values, (-?\\d+) and (-?\\d+)$")
   public void twoInputValuesAnd(int a, int b) throws Throwable {
      value1 = a;
      value2 = b;  
   }
   // other code removed
}

the we have created another problem. The instance variables, value1 and value2 or stored in AddStepdefs and hence we’d also have similar variables stored in SubtractStepdefs (for use with the subtract scenario) however, the twoInputValuesAnd will never change the instance variables in the SubtractStepdefs for obvious reasons…

So how do we share instance data across our step definition files?

As programmers we might see a Scenario as analogous to a class but you can see that to ensure we adhere to the DRY principle we’d actually be better off creating a single class with all step definitions across all scenarios. This ofcourse is also not ideal because once we start to rack up a large number of scenarios, our class will become large and unwieldy.

So what we really want to do is create an instance of some shared state and have Cucumber pass this to each step definition class.

This is where cucumber-picocontainer comes in. If we add the following to the pom.xml

<dependency>
   <groupId>info.cukes</groupId>
   <artifactId>cucumber-picocontainer</artifactId>
   <version>1.2.5</version>
   <scope>test</scope>
</dependency>

and can “context” like this

public class ScenarioContext {
    private int value1;
    private int value2;
    private int result;

    public int getValue1() {
        return value1;
    }

    public void setValue1(int value) {
        this.value1 = value;
    }

    public int getValue2() {
        return value2;
    }

    public void setValue2(int value) {
        this.value2 = value;
    }

    public int getResult() {
        return result;
    }

    public void setResult(int value) {
        this.result = value;
    }
}

We can now change our step definitions files to look like this, first the AddStepdefs file

public class AddStepdefs {

    private ScenarioContext context;

    public AddStepdefs(ScenarioContext context) {
        this.context = context;
    }

    @Given("^Two input values, (-?\\d+) and (-?\\d+)$")
    public void twoInputValuesAnd(int a, int b) throws Throwable {
        context.setValue1(a);
        context.setValue2(b);
    }

    @When("^I add the two values$")
    public void iAddTheTwoValues() throws Throwable {
        Calculator calculator = new Calculator();

        context.setResult(calculator.add(context.getValue1(), context.getValue2()));
    }

    @Then("^I expect the result (-?\\d+)$")
    public void iExpectTheResult(int r) throws Throwable {
        Assert.assertEquals(r, context.getResult());
    }
}

and the SubtractStepdefs without any duplicated steps could look like this

public class SubtractStepdefs {

    private ScenarioContext context;

    public SubtractStepdefs(ScenarioContext context) {
        this.context = context;
    }

    @When("^I subtract the two values$")
    public void iSubtracTheTwoValues() throws Throwable {
        Calculator calculator = new Calculator();

        context.setResult(calculator.subtract(context.getValue1(), context.getValue2()));
    }
}

When a feature file is run, Cucumber will get an instance of our ScenarioContext passed to each class by the picocontainer which will them be used by the class methods.

Gherkin & Cucumber in Java (using IntelliJ)

This post will be based around implementing features etc. using IntelliJ, so your experience with tooling may differ from those experiences outlined in this post.

If you’re coming from Specflow and C# to IntelliJ and Java

I’ve used Gherkin previously when using Specflow in Visual Studio and with C#. The concepts, syntax etc. are exactly the same, however be aware that the generated code isn’t just different in terms of the language used (i.e. C# and Java for example) but also in the RegEx generated. As an example if your Gherkin has numbers then Specflow generated code allows for negatives, whereas the code generated within the IntelliJ/Java systems is a little stricter with it’s RegEx and negatives need to be added to the RegEx.

Creating our demo project

  • File | New Project
  • Select Maven project type
  • Enter the GroupId and ArtifactId

Now open the pom.xml and add the following

<dependencies>
   <dependency>
      <groupId>info.cukes</groupId>
      <artifactId>cucumber-java</artifactId>
      <version>1.2.5</version>
      <scope>test</scope>
   </dependency>
   <dependency>
      <groupId>info.cukes</groupId>
      <artifactId>cucumber-jvm</artifactId>
      <version>1.2.5</version>
      <type>pom</type>
   </dependency>
   <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
   </dependency>
</dependencies>

Creating our code

Let’s create a simple little class to run our tests against. Within src/java add a new class, mine’s named com.putridparrot.Calculator. Here’s the code that’s stored in src/main/java/com.putridparrot package

package com.putridparrot;

public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}

In src/test create a folder named resources and right mouse click on this folder and select Mark Directory As | Test Resources Root.

Note: the resources folder is not a requirement, more a convention.

Next, add a folder named features and within this folder create a new file named add.feature, this is a really simple example feature

Note: this post is not about writing good feature’s so this example is extremely basic.

Feature: Calculator
  
  Scenario Outline: add two numbers
    Given Two input values, <first> and <second>
    When I add the two values
    Then I expect the result <result>

  Examples:
    | first | second | result |
    | 1     | 12     | 13     |
    | -1    | 6      | 5      |

Select the end of each the Given, When and Then line and press alt+enter for each and select create steps definition. Name the file AddStepDef (mine’s created in the src/test/java/com/putridparrot folder/package) and set the file type to Java when you create your first step definition, then subsequently add step definitions to this same file.

Note: At the time of writing, selecting create all step definitions, does not seem to work as expected and simply creates a single step, not yet sure why.

Here’s what my generated steps looked like

package com.putridparrot;

import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class AddStepDefs {
    @Given("^Two input values, <first> and <second>$")
    public void twoInputValuesFirstAndSecond() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    @When("^I add the two values$")
    public void iAddTheTwoValues() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    @Then("^I expect the result <result>$")
    public void iExpectTheResultResult() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }
}

Sadly these require editing as the placeholders (the <> wrapped text) are not being passed as arguments into our methods. The simplest way to solve this is to change the placeholders to numbers (or the type we expect) delete the methods from the step definition file and then regenerate the step definitions. So here’s my “temporary” change to the feature to get valid steps generated.

Feature: Calculator

  Scenario Outline: add two numbers
    Given Two input values, 1 and 2
    When I add the two values
    Then I expect the result 3

    Examples:
      | first | second | result |
      | 1     | 12     | 13     |
      | -1    | 6      | 5      |

this gives us the following code

package com.putridparrot;

import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class AddStepDefs {


    @Given("^Two input values, (\\d+) and (\\d+)$")
    public void twoInputValuesAnd(int arg0, int arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    @When("^I add the two values$")
    public void iAddTheTwoValues() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }
    
    @Then("^I expect the result (\\d+)$")
    public void iExpectTheResult(int arg0) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

}

Return the feature file back to the version with placeholders.

Before we write our actual test code, let’s run the feature tests. In IntelliJ we can right mouse click on an individual feature file or the folder with the features and select Run feature or Run all features.

As we only have one feature file at the moment, select it, right mouse click and select Run: Feature Calculator, you’ll probably be hit with output in the test window along the lines of

Undefined step: Given… etc.

We need to edit the configuration for the runner and supply the Glue (in my case I have the steps in the package com.putridparrot, so I enter that in the Glue: option). Now running the feature should result in another error

Undefined step: Given Two input values, -1 and 6

As you can see, there’s a problem with the generated code as it expects a method that handles a negative number and none exist, we need to amend our RegEx for all methods to include a possible – sign. Changed all (\\d+) to (-?\\d+) now running the feature will have fixed the error.

Writing our test code

We can now write fairly standard unit testing code into the step definition file (in this case I’m using JUnit), so here’s a simple example

package com.putridparrot;

import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.junit.Assert;

public class AddStepDefs {

    private Calculator calculator;
    private int value1;
    private int value2;
    private int result;

    @Before
    public void before() {
        calculator = new Calculator();
    }

    @Given("^Two input values, (-?\\d+) and (-?\\d+)$")
    public void twoInputValuesAnd(int arg0, int arg1) throws Throwable {
        value1 = arg0;
        value2 = arg1;
    }

    @When("^I add the two values$")
    public void iAddTheTwoValues() throws Throwable {
        result = calculator.add(value1, value2);
    }

    @Then("^I expect the result (-?\\d+)$")
    public void iExpectTheResult(int arg0) throws Throwable {
        Assert.assertEquals(arg0, result);
    }
}

Common mistakes

Glue, glue and more glue – make sure you set up the glue.

.NET’s SecureString

I’m investigating various security based topics at the moment. Whilst I have dabbled in security in the past I do not class myself an expert on the subject, hence these posts may be fairly basic.

Background

I’m doing some coding that requires a level of security around data (specifically strings) which is simply not available using the String class.

The String class has a couple of properties which make it less than secure

  • The string is stored in memory as plain text
  • The disposal of memory is non deterministic and hence can remain in memory long after it’s usage has been completed

Obviously if we were to store a password in a string we can use tools such as WinDbg to dump memory and ultimately locate all instances of the password strings. Sure it might take a bit of time, but ultimately we’d find the password as plain text.

A solution?

TL;DR SecureString is only really secure until you need to get at the string, this isn’t a solution to 100% security but may help in certain situations, not least for situations where you want to pass a string between methods.

SecureString is a .NET class which is totally unrelated to the String class and it’s aim is to fix the security issues which exist for the String class, so

  • The SecureString may be encrypted within memory, on the MS website it suggests this is dependent upon the underlying OS
  • We can dispose of a SecureString in a deterministic way, i.e. calling the Dispose method or worse case garbage collected

To store some characters in a SecureString we have to append characters, like this

public static SecureString ToSecureString(string data)
{
   var secure = new SecureString();
   foreach (var c in data)
   {
      secure.AppendChar(c);
   }
   secure.MakeReadOnly();
   return secure;
}

We can also assign characters via a constructor override which takes a char* and length argument.

To turn a SecureString back into a String we can use

public static string ToString(SecureString secureString)
{
   var ptr = Marshal.SecureStringToBSTR(secureString);
   try
   {
      return Marshal.PtrToStringBSTR(ptr);
   }
   finally
   {
      Marshal.ZeroFreeBSTR(ptr);
   }
}

If, you come from a COM and C++ background you’ll recognize the BSTR’s.

In this code we get a pointer to the unmanaged BSTR from the SecureString then we use the pointer to create a String object. In the finally clause we zero out and release the memory that the BSTR used, ensuring that our password (or whatever) is both cleared and then removed from memory.

You might now be questioning what use the SecureString really is, if we ultimately convert whatever it’s storing too and from a String.

Ofcourse what you really need to do is store data which might be around for a while in the SecureString and preferably rarely convert them to Strings.

Secure input

Control-wise, WPF contains a SecurePassword property on the PasswordBox to allow input to be turned into a SecureString. Hence we can use this to take user input and store “secure” from input until the point we need to use it.

See also

Comparing secure strings in .NET

F# currying in a little more depth

Unlike Scala, for example, F# does not use an alternate syntax to differentiate a “curry-able” function from a “non-curry-able” function. I was therefore interested in what the F# compiler does to when supplied with a function like the following

let add a b c = a + b + c

Does it really generate something like the following code?

let add a = 
    let add1 b = 
        let add2 c = 
            a + b + c
        add2
    add1

Obviously this wouldn’t be great as the code requires closures and sub-functions to be created and so would be less performant in situations where we do not require the currying capabilities.

Thankfully the F# compiler takes care of everything for us so that if we have code in our application which solely uses the add function, like this

let r = add 1 2 3

then the compiler simply generates the following, this is taken from ILSpy from the compiled code and obviously reproduced in C# which I think demonstrates pretty clearly what happens.

public static int add(int a, int b, int c)
{
   return a + b + c;
}

// therefore our call to the 
// function becomes
int r = Program.add(1, 2, 3);

Now, what happens if we start currying our functions, with code like this

let r = add 1 2 
let r1 = r 3

Here the compiler still creates the add method as before (as one would expect) but it also creates the following internal class (I’ve removed attributes to make it all a little cleaner)

internal sealed class r@9
{
   public int a;
   public int b;

   internal r@9(int a, int b)
      : this()
   {
      this.a = a;
      this.b = b;
   }

   public override int Invoke(int c)
   {
      return Program.add(this.a, this.b, c);
   }
}

// now our calling 
// code looks like this
int a = 1;
int b = 2;
FSharpFunc r2 = new r@9(a, b);
int r = (int)r2.Invoke((!0)3);

So in conclusion, if we’re not using currying on our functions then we get a “standard” function and once we start currying only then do we get the equivalent of sub-functions being created.

More ASP.NET Core with C#

In the previous post I looked into the F# Giraffe library which makes writing all sorts of web request/response code pretty simple. This in turn is built on top of the existing functionality within ASP.NET Core.

Let’s again look at the bare bones code from running up Kestrel adding code to the pipeline

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;

namespace KestrelTest
{
    public class Startup
    {
        public void Configure(
            IApplicationBuilder applicationBuilder,
            IHostingEnvironment hostingEnvironment)
        {
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            WebHost.CreateDefaultBuilder()
                .UseStartup<Startup>()
                .UseKestrel()
                .Build()
                .Run();
        }
    }
}

Within the Configure method we can start adding code to respond to different routes using the Map method, for example add the following to the Configure method

applicationBuilder.Map("/hello", app =>
{
   app.Run(async ctx =>
   {
      var request = 
         ctx.Request.Path.HasValue ? 
            ctx.Request.Path.Value.Substring(1) : 
            String.Empty;
       await ctx.Response.WriteAsync("Hello " + request);
   });
});
applicationBuilder.Map("/error", app =>
{
   app.Run(ctx => 
      Task.FromResult(
         ctx.Response.StatusCode = 404) 
   );
});

Note: The code is somewhat basic, obviously you would probably want to write some better code for extracting partial paths etc. from the routes/maps. However we’ll be covering a better alternative to this sort of code later in the post.

In the above we’ve created the equivalent of two “routes”. The first handles URL’s along the line of localhost:5000/hello/World where the return would become “Hello World”. The second route simply responds with a 404 for the URL localhost:5000/error.

We can also (as you’d expect) handle queries within our URL, so let’s say we expect this format URL, http://localhost:5000/hello?name=World, then we can amend our /hello code to the following

applicationBuilder.Map("/hello", app =>
{
   app.Run(async ctx =>
   {
      await ctx.Response.WriteAsync(
         "Hello " + ctx.Request.Query["name"]);
   });
});

These pieces of code write responses, but we can also insert code into the pipeline which doesn’t write to the response but instead might add debug/logging code. Other examples of usage might include (as per Writing Middleware) changes to the culture for the user response.

Here’s a simple example of such code, this is a little contrived as we’re going to (in essence) redirect calls to /hello path. Just place the following code before the applicationBuilder.Map method in the above

applicationBuilder.Use(async (ctx, next) =>
{
   if (ctx.Request.Path.Value == "/hello")
   {
      ctx.Request.Path = new PathString("/error");
   }
   await next.Invoke();
});

Note: there are redirect/URL rewriting capabilities already in ASP.NET Core, see URL Rewriting Middleware in ASP.NET Core.

Routing

In the previous example we demonstrated using the Map method to route our calls but ASP.NET Core libraries already supply a routing middleware which handles a lot of the standard routing type of functionality we’d expect. We can add the routing services by adding a method to the Startup class like this

public void ConfigureServices(
   IServiceCollection services)
{
   services.AddRouting();
}

Now the Configure method should look like this

public void Configure(
   IApplicationBuilder applicationBuilder,
   IHostingEnvironment hostingEnvironment)
{
   var routes = new RouteBuilder(applicationBuilder);
   routes.MapGet("hello/{name}", ctx =>
   {
      var name = ctx.GetRouteValue("name");
      return ctx.Response.WriteAsync($"Hello {name}");
   });

   applicationBuilder.UseRouter(routes.Build());
}

The RouteBuilder handles the routing in a more helpful/useful manner. Don’t forget to use the line applicationBuilder.UseRouter(routes.Build()); or you’ll find that the routes are not registered and hence your code will never get called.

References

ASP.NET Core MiddlewareRouting in ASP.NET Core

Using Giraffe as a service pipeline in Kestrel

In the previous post we looked at using Kestrel to run an ASP.NET Core application, now we’re going to use Giraffe to build some services.

Giraffe is an F# library to let get started by creating our application code.

  • Create Visual F# | .NET Core | Console App
  • My project is named GiraffeTest
  • Add via NuGet Giraffe and Microsoft.AspNetCore

Replace Program.fs code with the following

open Giraffe
open Microsoft.Extensions.DependencyInjection
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore

let webApp =
    choose [
        GET >=>
            choose [
                routeCif "/hello/%s" (fun name -> text (sprintf "Hello %s" name))
            ]
    ]

type Startup() =
    member this.ConfigureServices (services : IServiceCollection) =
        services.AddGiraffe() |> ignore

    member this.Configure (app : IApplicationBuilder) =
        app.UseGiraffe webApp

[<EntryPoint>]
let main _ =
    WebHost.CreateDefaultBuilder()
        .UseKestrel()
        .UseStartup<Startup>()
        .Build()
        .Run()
    0

In the previous post we saw how to create the Kestrel server, so the code in main is exactly the same (apart from obviously being F#) which creates the server and calls the Startup class to configure our middleware. In this case we add Giraffe to the services and runs the Giraffe webApp HTTP handler.

The webApp HTTP handler is basically our filter and routing function.

Run this code up and navigate to localhost:5000/hello/World and we should get Hello World displayed.

We didn’t actually need the GET handler as by default the GET will be used, but it’s shown here to be a little more explicit in what is being implemented. We can also support POST methods using the same syntax as our GET code.

Extending our routes

Giraffe allows us to declare multiple routes which can include static pages. Let’s start off my adding an index.html page to the “Content root path” as displayed when running the application, in my case this is the folder containing Program.fs. I’m using the following index.html

<html>
    <body>
        Hello World
    </body>
</html>

Now add a new route to the route section of the webApp. i.e.

choose [
    routeCif "/hello/%s" (fun name -> text (sprintf "Hello %s" name))
    route "/" >=> htmlFile "index.html"
]

This has now added a route for localhost:5000/ which returns the contents of the index.html file.

A list of different routes can be seen in the source for Routing.fs.

Below is an example of using some different routes

let helloName name = text (sprintf "Hello %s" name)

let webApp =
    choose [
        GET >=>
            choose [
                // case insensitive using anonymous function
                routeCif "/hello/%s" (fun name -> text (sprintf "Hello %s" name))
                route "/"       >=> htmlFile "index.html" 
                route "/error"  >=> setStatusCode 404
                route "/ping"   >=> text "pong"
                // case sensitive use function
                routef "/hello2/%s" helloName
            ]
    ]

Return from the fish (>=>) operator can be marked as different types of result types.
The functions text, htmlFile and other response writes available here ResponseWriters.fs. These include the usual suspects such as XML and JSON. Giraffe also supports Razor, see the NuGet package Giraffe.Razor.

References

Giraffe GitHub repos.

Kestrel – ASP.NET core web server

Kestrel is a .NET Core cross platform web server that can be used to host web sites, web/REST services etc.

Note: This code covers .NET core 2.0 and ASP.NET core 2.0.1

Take a look at Introduction to Kestrel web server implementation in ASP.NET Core for a great post about using Kestrel along with IIS, Nginx etc.

Getting Started

Let’s get started and build a very basic application running Kestrel.

  • Create a Visual C# | .NET Core | Console App (.NET Core) application
  • My project is named KestrelTest
  • Use NuGet to add the package Microsoft.AspNetCore

Now let’s create the most basic and most useless web server by writing the following code

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;

namespace KestrelTest
{
    public class Startup
    {
        public void Configure(IApplicationBuilder applicationBuilder,
            IHostingEnvironment hostingEnvironment)
        {
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            WebHost.CreateDefaultBuilder()
                .UseStartup<Startup>()
                .UseKestrel()
                .Build()
                .Run();
        }
    }
}

In Main we create the webserver and we supply the Startup application which we’d use to configure our services. Minimally we need the Configure method in the Startup object and notice we’re not adhering to any specific interface or type.

We could pass the args to the CreateDefaultBuilder if we want to start the application with certain arguments and/or use the UseKestrel override to create options for our server, but we’re aiming to just create the bare essentials of a Kestrel based web server in this post.

Now we can run this code and we’ll get, by default, a web server running and exposing port 5000 on localhost. However the server does nothing else, no static pages, nothing.

So let’s now add the NuGet package Microsoft.AspNetCore.StaticFiles and change our Configure method to look like this

public void Configure(
   IApplicationBuilder applicationBuilder,
   IHostingEnvironment hostingEnvironment)
{
   applicationBuilder.UseWelcomePage();
}

Now run the application and using your preferred web browser, navigate to localhost:5000 and you should see a ASP.NET Core Welcome Page.

Again this is of little use but does show that everything is working.

Serving up some static content

See Work with static files in ASP.NET Core for more in depth information.

Let’s now use Kestrel to serve up some static pages. Change the Configure method to look like this

public void Configure(
   IApplicationBuilder applicationBuilder,
   IHostingEnvironment hostingEnvironment)
{
   applicationBuilder.UseStaticFiles();
}

Now in your project folder (i.e. the folder with your Program.cs file) which should be the same as the Content root path shown when you run the application. Here you can add a wwwroot folder and place an HTML page in the folder, i.e. here’s a simply index.html file

<html>
    <body>
        Hello World
    </body>
</html>

Make sure you set the file to have the “Copy to Output Directory” option as “Copy if Newer” in the properties window in Visual Studio.

Run the application again and navigate to localhost:5000/index.html and you should see the text Hello World.

Middleware

As we’ve seen in this post, the design of Kestrel is to supply a default web server application which we can then add our middleware code to, turning the application into a static web server or adding ASP.NET MVC into the mix etc. We can implement our own middle ware (see ASP.NET Core Middleware) which can be used to handle requests via a middleware pipeline.

I’m not going to go into any real depth in terms of developing my own middleware as we can already use various libraries for this, but just to demonstrate how we can intercept a request, change the Configure method to look like this

public void Configure(
   IApplicationBuilder applicationBuilder,
   IHostingEnvironment hostingEnvironment)
{
   applicationBuilder.Run(async context =>
   {
      await context.Response.WriteAsync("***Hello World***");
   });
}

Now if we run up the application and navigate to localhost:5000 we should see ***Hello World*** returned.

References

Introduction to Kestrel web server implementation in ASP.NET Core
BenchmarksASP.NET Core Web Servers: Kestrel vs IIS Feature Comparison and Why You Need Both

Implicit parameters in Scala

Implicit parameters allow us to write code where one or more parameters can be supplied automatically by Scala in a similar way to dependency injection works.

For example, let’s assume we have a Calculator class (below) which we want to supply to functions/methods in our application

class Calculator {
  def add(a : Int, b : Int) = a + b;
  def subtract(a : Int, b : Int) = a - b;
}

In this instance I’m going create the equivalent of a singleton to the class, the val name doesn’t really matter in the code I’m demonstrating as Scala will simply locate the matching type within the MyImplicits object.

object MyImplicits {
  implicit val calculator = new Calculator();
}

To use this singleton instance we simply import the MyImplicits._ into our code. Here’s an example of using such code. Notice how the calls to the add and subtract functions in the main method do not need to supply the implicit parameter. This is supplied by Scala for us.

object Main {

  import MyImplicits._

  def add(implicit c : Calculator) : Unit = {
    println(c.add(1, 2));
  }

  def subtract(a : Int, b : Int)(implicit c : Calculator) : Unit = {
    println(c.subtract(a, b));
  }

  def main(args: Array[String]) : Unit = {

    val i = add
    val s = subtract(10, 2)
  }
}

Of course, using such code means we could supply another instance of the Calculator class to either of the add or subtract methods if we wished or change the code to work with traits and implementations which would obviously allow us to import different implementations of the trait. Here’s the code from above but using a trait for the implicit argument

trait Calc {
  def add(a : Int, b : Int) : Int;
  def subtract(a : Int, b : Int) : Int;
}


class Calculator extends Calc {
  override def add(a : Int, b : Int) = a + b;
  override def subtract(a : Int, b : Int) = a - b;
}

object MyImplicits {
  implicit val calculator = new Calculator();
}

object Main {

  import MyImplicits._

  def add(implicit c : Calc) : Unit = {
    println(c.add(1, 2));
  }

  def subtract(a : Int, b : Int)(implicit c : Calc) : Unit = {
    println(c.subtract(a, b));
  }

  def main(args: Array[String]) : Unit = {

    val i = add;
    val s = subtract(10, 2);
  }
}