Press "Enter" to skip to content

ASP.NET Core on a Pi running Win10IoT

Since the announcement of Windows 10 IoT for the Raspberry Pi, we felt the absence of a decent webserver. ASP.NET on the Pi would be marvelous, but wasn’t supported. To keep my projects going I started building my own simple web API server and created a nuget package for it (Restup). It is working quit nice, but not nearly as feature complete as ASP.NET. This week a friend of mine pointed out this GitHub page which explained how you could get the dotnet runtime going on a Win10Iot Pi. My first reaction to this post was… can I run ASP.NET Core projects? A few hours later I had it running. It’s quit a hassle to get it going, so I thought it would be a good idea to put it all into one post. Here we go!

The environment I used to get this running:

  • Using Visual Studio 2015 Update 3 (community will do fine)
  • Microsoft .NET Core Tools 14.1.20624.0 (Visual Studio extension)
  • Pi2 (3 will do fine as well) with Windows 10 IoT version 10.0.14393.0

Next is setting up the ASP.NET Core project:

  • Create ASP.NET Core project (use the Web API template)
  • Add a new file named hosting.json and change the content to:
{ "server.urls": "http://*:5100" }
  • Add hosting.json to the publishOptions/include section in project.json
  • Add the following dependencies by editing the project.json:
"System.ComponentModel.TypeConverter": {
"version": "4.3.0",
"exclude": "runtime"
},
"System.ComponentModel.Primitives": {
"version": "4.3.0",
"exclude": "runtime"
},
"System.Collections.Specialized": {
"version": "4.3.0",
"exclude": "runtime"
},
"System.Linq": {
"version": "4.3.0",
"exclude": "runtime"
},
"System.Threading": {
"version": "4.3.0",
"exclude": "runtime"
}
  • Edit the project.cs file and replace the Main method with:
var config = new ConfigurationBuilder()
  .SetBasePath(Directory.GetCurrentDirectory())
  .AddJsonFile("hosting.json", optional: true, reloadOnChange: true)
  .Build();

var host = new WebHostBuilder()
  .UseConfiguration(config)
  .UseKestrel()
  .UseContentRoot(Directory.GetCurrentDirectory())
  .UseIISIntegration()
  .UseStartup<Startup>()
  .Build();

host.Run();
  • Create a publish profile (right mouse on your project –> publish) and use the custom template (which published to a folder)
  • Publish your project
  • Open a explorer and navigate to you publish folder
  • Change the <projectname>.runtimeconfig.json, change the version of runtimeOptions/framework to: 1.2.0-beta-001291-00

Next, we have to prepare the Pi:

  • Copy the published folder to your Pi in any directory you want (e.g. c:\tmp)
  • Download and extract the dotnet for ARM (beta version) in a folder on your Pi (e.g. c:\dotnet)
  • Enter remote powershell to your Pi
  • Add the dotnet folder to the path variable with:
$env:path += ';c:\dotnet'
  • Add firewall exception for port 5100:
netsh advfirewall firewall add rule name="WebAPI test 5100" dir=in action=allow protocol=TCP localport=5100
  • CD into the folder where you copied the published webapp (my case c:\tmp) and run:
dotnet WebApplication1.dll

That’s it, peanuts right :). There are some people at Microsoft working hard to get a better user experience. I couldn’t have done this without the help of others, this GitHub issue was used to get this going. Big thanks to all the participants in that discussion. For now, enjoy you ASP.NET Core application on you Raspberry Pi running Windows 10 IoT by browsing to:

http://yourpihostname:5100/api/values

  • Michael Carey

    Hi Tom,

    This is Kestrel rather then WebListerner. I thought MS were working towards WebListerner for UWP.. no????

    Mike

  • Michael Carey

    Hi Tom,

    I think you mean “.UseStartup ()”

    Mike

    • Tom Kuijsten

      Thanks, some auto formatting issue with wp 🙂

  • Micah Vertal

    Hello Tom,

    Thanks so much for posting this article! I spent hours browsing the internet on how to do something like this, and finally found it. One thing you may want to add is the part on the GitHub page about VS 2017 and .csproj files. What fixed it for me was the fourth answer from the bottom at the GitHub page:

    https://github.com/dotnet/coreclr/issues/9168

    In short, add:

    Analyzers;Build;Compile;ContentFiles;Native

    Analyzers;Build;Compile;ContentFiles;Native

    Analyzers;Build;Compile;ContentFiles;Native

    Analyzers;Build;Compile;ContentFiles;Native

    In between the tags containing other ‘s in the .csproj file.

  • Tom Kuijsten

    Thanks for your comment. I think I already covered those packagereferences in the “Add the following dependencies by editing the project.json:” section, didn’t work for you?

    • Ummar Bhutta

      project.json file is not there in VS2017, We need to edit .csproj files. I was trying to run in VS2017 the other day and was not successful, will give it try again as @micahvertal:disqus described.