Pseudo random

April 28th, 2017

I have started developing a lib for creating pseudo randomised data. (yes, I know all randomised data on a computer is pseudo randomised, but in my case I want to control the seeding)

The lib, basically, is the ordinary dotnet random class with constructor with some customised constructors to make it easier to add a known seed. It also contains methods for randomising strings with a prefix, a certain length, int, long, decimal, guid, enum and other stuff that is often used.

The seeding is taken from the test method’s name.
This will give the same values for every test run of that method.

Create a webapi

February 12th, 2017

Prerequisite

See Compile Aspnet core 1.1 outside Docker container but run within

Create a controller

Create a file TodoController.cs file in the same folder as HomeController.cs file. In a real application we should probably put it in a folder “api” or similar.

Fill it with the text below (which is a refinement of https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api ):

[Update:There is now a

1
dotnet new -t webapi

or similar so you don’t have to copypaste the code below. I plan to write about it as soon as I can.]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
namespace WebApplication.Controllers
{
    // Setting the Route attribute is important.
    // Otherwise we don't get the restish behaviour we're looking for.
    // I cannot say exactly why this is so.
    [Route("api/[controller]")]
    public class TodoController : Controller
    {
        private static List _data =
        new List{
            new Item{
                Id = 1,
                Name = "One"
            },
            new Item{
                Id = 2,
                Name = "Two"
            },
        };
        [HttpGet]
        public IEnumerable<item> GetAll()
        {
            return _data;
        }
        [HttpGet("{id}", Name = "GetTodo")]
        public string Get(int id)
        {
            return _data.Single(d => d.Id == id).Name;
        }
        [HttpPut("{id}")]
        public IActionResult Put(int id, [FromBody] string name)
        {
            GetItem(id).Name = name;
            return new NoContentResult();
        }
        [HttpPost]
        public IActionResult Post(string name)
        {
            var item = new Item { Id = _data.Count() + 1, Name = name };
            _data.Add(item);
            return CreatedAtRoute("GetTodo", new { id = item.Id }, item);
        }
        private Item GetItem(int id)
        {
            return _data.Single(d => d.Id == id);
        }
    }
    public class Item
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}
</item>

Compile:

1
dotnet build

And start the web server with the application:

1
dotnet bin/Debug/netcoreapp1.1/MyWs.dll

Call the controller

Start another terminal.

First check what we have.

1
curl localhost:5000/api/Todo

Then check one item.

1
curl localhost:5000/api/Todo/2

Create a new.

1
2
curl --data "Name=Three" localhost:5000/api/Todo/
curl localhost:5000/api/Todo/3

Update an existing according to Stack overflow.

1
2
3
4
5
curl -H "Content-Type: application/json" \
-X PUT \
--data '"Tva"' \
localhost:5000/api/todo/2
curl localhost:5000/api/Todo/2

Image processing in dotnet core

February 6th, 2017

There is no native image processing lib for dotnet core but someone at Microsoft has been kind enough to list 4 alternatives here.

Overload capslock keyboard key in windows

January 24th, 2017

We all have this centrally keyboard key that is seldom used. I am thinking of caps lock.

I wanted to use it as an alternative key, not as ctrl or anything; but as something totally different that does not invade existing short cuts.

Rewiring the capslock is harder than it looks at first glance.
I didn’t find a Good solution but at least one that works; for now.

Autohotkey

Autohotkey aka AHK is for writing macros for keyboard, mouse(?) and joystick. The language is awful but don’t let that scare you away.

I have mapped CapsLock to be used with j, k, l and ; to navigate.
Capslock by itself is Esc.

Source code

Regular header stuff in the file:

1
2
3
4
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.

Make Capslock not work as capslock but also make it work as Esc:
(I believe some magic is involved.)

1
2
3
SetCapsLockState AlwaysOff
!CapsLock::CapsLock
CapsLock::Send {Esc}

Map the personal stuff. In my case navigation:

1
2
3
4
CapsLock &amp; j::Send {Left}
CapsLock &amp; k::Send {Right}
CapsLock &amp; `;::Send {Down}
CapsLock &amp; l::Send {Up}

Unfortunately I don’t remember the sources I puzzled together my solution form.

Compile Aspnet core 1.1 outside Docker container but run within

January 14th, 2017

Prerequisite

OSX

Docker

Dotnet core 1.1

A folder named (e.g.) /Users/yourname/Docker/Dandelion
and a folder named (e.g.) /Users/yourname/Docker/Dandelion/MyWeb

Create the web

In MyWeb run

1
2
3
4
dotnet new -t web
dotnet restore
dotnet build
dotnet publish

Now we have a web site compiled and ready to run with [dotnet run].
You can run the app and then [curl localhost] to find out if it is runnable; but if you got no error message earlier all should be ok.

Create an image

Go to the Dandelion folder.

Create a file Dockerfile and paste into it:

1
2
3
FROM microsoft/dotnet
EXPOSE 80
ENV "ASPNETCORE_URLS=http://+:80"

Then, to create the image run

1
docker build -t yourname/dandelion .

Note that the name must be all small caps.

If you want to check that the image is created just execute [docker images] which should show the new image at the top of the list.

Start the container

Go to the Dandelion folder. (you are probably already standing there)

Execute

1
2
3
4
5
docker run -p 80:80 \
-ti --rm \
-v /Users/yourname/Documents/Docker/Dandelion/MyWeb/bin/Debug/netcoreapp1.1/publish:/MyWeb \
yourname/dandelion \
/bin/bash -c 'cd /MyWeb; dotnet MyWeb.dll'

and your web should start.

Verify result

Either open another console and execute

1
curl localhost

or go to localhost in your web browser.

dotnet publish command returns npm error

January 10th, 2017

When trying to

1
dotnet publish

a dotnet core 1.1 project you might receive

1
2
Publishing TheApp for .NETCoreApp,Version=v1.1
No executable found matching command "npm"

The remedy is to install NPM.

If you are running OSX you probably got Homebrew with the installation.
So just

1
brew install node

Then you might get

No executable found matching command “bower”

which means that Bower is missing. According to https://github.com/dotnet/cli/issues/3293 you should install it through npm like so:

1
2
npm install -g bower
npm install -g gulp

Create and run an Aspnet core 1.1 web site in Docker container on OSX

January 7th, 2017

Prerequisite:

Docker and Dotnet Core 1.1 should be installed on the host (=OSX).

You have a folder somewhere, for instance /User/yourname/Documents/Docker/mywebapp.

Create the image

In your folder, create the dockerfile

1
2
3
4
5
6
7
8
FROM microsoft/dotnet
# VOLUME /Documents/Docker/mywebapp
EXPOSE 80
ENV "ASPNETCORE_URLS=http://+:80"
RUN mkdir app
WORKDIR /app
RUN dotnet new -t web
RUN dotnet restore

Then execute

1
docker build -t mynick/myimage .

in a terminal in said folder.

If you now execute

1
docker images

you can se the new image as mynick/myimage.

Create the container and start the web server

In the terminal execute

1
2
3
4
docker run \
-p 80:80 \
-ti --rm mynick/myimage \
/bin/bash -c 'cd /app; dotnet run;'

Behold result

In a terminal execute

1
curl localhost

or in a web browser go to

1
localhost

Run Docker Dotnet core aspnet 1.1 web server with mounted executable on OSX

January 3rd, 2017

Prerequisite

Install docker.

Install Dotnet Core 1.1.

Create web application

Create a directory “theapp”.

Open a terminal and, in the directory, execute:

1
2
3
dotnet new -t web
dotnet restore
dotnet run

Check it works.

Execute, in a terminal:

1
curl localhost

Or is it

1
curl localhost:5000

?

Create container

In a terminal execute:
(after you have have updated “YourRootedPathAndFolder” appropriately.

1
2
3
4
5
6
docker run -p 80:80 \
-e "ASPNETCORE_URLS=http://+:80" \
-ti --rm \
-v /YourRootedPathAndFolder/TheApp:/theapp \
microft/dotnet \
/bin/bash -c 'cd /theapp; dotnet restore; dotnet run'

and when it is finished churning through the long list of modules open a new terminal and execute

1
curl localhost

on your host to receive a smaller waterfall of HTML.

That should be it.

Troubleshooting

If you forget to do the dotnet restore

1
2
3
4
docker run -p 80:80 \
-ti -v /Users/ola/Documents/Docker/loose/TheApp:/theapp \
microsoft/dotnet \
/bin/bash -c 'cd /theapp; dotnet run'

You get something like:

Project theapp (.NETCoreApp,Version=v1.1) was previously compiled. Skipping compilation.

Error: assembly specified in the dependencies manifest was not found — package: ‘Microsoft.AspNetCore.Antiforgery’, version: ‘1.0.1’, path: ‘lib/netstandard1.3/Microsoft.AspNetCore.Antiforgery.dll’

Attach and request

Run

1
docker ps

in a terminal. Note the Container ID.

Then execute, after the b5a…f5 is updated appropriately.

1
docker exec -it b5a8ccd5b1f5 bash

Now you have a shell inside the container and should be able to get a result from:

1
curl localhost:5000

Create a Dotnet aspnet core 1.1 web server in OSX

December 31st, 2016

Install Dotnet core à la https://www.microsoft.com/net/core#macos

Create a folder and to to it.

Open a terminal (one cannot reuse any terminal from before dotnet was installed since the path is updated) and execute:

1
2
3
dotnet new -t web
dotnet restore
dotnet run

Open another terminal and execute:

1
curl localhost:5000

A pile of HTML should scroll into view.

In the first terminal you can see the reaction.

Running Dotnet aspnet 1.1 in a Docker container on OSX without any Windows

December 29th, 2016

Prerequisite

Install Docker on OSX.

Create Dockerfile

Create a file named Dockerfile in a new folder.
Its contents are:

1
2
3
4
5
6
7
8
FROM microsoft/dotnet
# VOLUME /Documents/Docker/dnc
EXPOSE 80
ENV "ASPNETCORE_URLS=http://+:80"
RUN mkdir app
WORKDIR /app
RUN dotnet new -t web
RUN dotnet restore

Create image

1
docker build -t yournick/yourimagename .

Run image

1
2
3
docker run -p 80:80 \
-ti --rm yournick/yourimagename \
/bin/bash -c 'dotnet run'

See output

On the host:

1
curl localhost

and you should se a whole dab of HTMl. You can also watch it in your browser.