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
  • If your on VS2015, 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"
}
  • If your on VS2017, edit the .csproj file and add the following dependencies:
<PackageReference Include="System.ComponentModel.TypeConverter" Version="4.3.0">
	<IncludeAssets>Analyzers;Build;Compile;ContentFiles;Native</IncludeAssets>
</PackageReference>
<PackageReference Include="System.ComponentModel.Primitives" Version="4.3.0">
	<IncludeAssets>Analyzers;Build;Compile;ContentFiles;Native</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Collections.Specialized" Version="4.3.0">
	<IncludeAssets>Analyzers;Build;Compile;ContentFiles;Native</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Linq" Version="4.3.0">
	<IncludeAssets>Analyzers;Build;Compile;ContentFiles;Native</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Threading" Version="4.3.0">
	<IncludeAssets>Analyzers;Build;Compile;ContentFiles;Native</IncludeAssets>
</PackageReference>
  • 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 again for you addition, I updated the post.

  • 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.

    • Micah Vertal

      Like @ummarbhutta:disqus said, the ‘project.json’ file is replaced by the ‘.csproj’ file in Visual Studio 2017. Because of this and the xml-like style, the format of adding the dependencies is different. I showed the correct format in my previous comment.

      • Ummar Bhutta

        Thanks @micahvertal:disqus and @tomkuijsten:disqus now at least I can run a basic MVC app on RPi 3. But having said this, I am trying to find an answer that Is ASP.NET Core is production ready for Windows IoT? My concerns are Specifically there is no Official release of .NET Core for ARM, and Generally there seems to be some API(s) which are not supported on ASP.NET Core, the one I found is SignalR, because I need it for my project (there must be others I doubt). If we can’t rely on ASP.NET Core we are left with options like https://github.com/tomkuijsten/restup and https://github.com/ummarbhutta/iotweb/ if we are developing a web based solution on windows IoT?

  • Michael Davidson

    Have you had any luck getting this to run from an IBackgroundTask as a start up task?

    • Tom Kuijsten

      As an UWP app you mean?

    • Tom Kuijsten

      Nope, didn’t try it but I doubt if that would even be possible, since UWP is another framework. Might work of all asp.net core are netstandard 1.6, but I don’t think that’s the case.

  • Shawn Hartke

    Any luck getting ssl to work. I was able to get this tutorial to work just not ssl

    • Tom Kuijsten

      I did try to get it to work. It does startup and it logs “Running webserver on https://etc“, but I can’t get any response on the https while the http does respond. If I get it to work, I’ll post it.

      • Shawn Hartke

        Thanks. That is exactly what I was seeing as well. Then I tried to download a new daily build which actually threw an exception when I would try to access it via https. So then I followed an article that had me try to just build the site as an exe and I got it to work using this method. (But had to switch to Ubuntu server) But the library I wanted to use wouldn’t work on arm 🙂 I can send you the article I finally got to work.

    • Micah Vertal

      Hi there. I was able to get SSL working on my Pi. I kind of modified these instructions to get it to work with ASP.NET Core 2.0.0+. I am currently working on making a tutorial similar to this one (giving credit where credit it due) on my website, Micahz.com. The instructions, when finished, will be located at this link: http://www.micahz.com/articles/windows-10-iot-core/raspberry-pi-3-ws-ssl.

  • Mike

    Bedankt, man! Na uren ploeteren om het om vs2017 aan de praat te krijgen is het eindelijk gelukt! Held <3
    Misschien is het wat ward om te vermelden dat de PackageReference en in de IncludeAssets xml elementen case sensitive zijn. Dat is is niet het geval in je bonvenstaande code snipet!

    • Tom Kuijsten

      Good catch, just updated the post, thanks!