Windows Core IoT – ASP DotNet Core Razor Pages & SQLite & Web Service on a Raspberry PI3

May 28, 2018

Building a Web Server App using ASP .Net Core using a SQLite Database and a Web Service starts here using this example.

https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/intro?view=aspnetcore-2.1

I gave it a little twist in order to get it to use SQLite and add a Web Service to it.

This means you need to follow page 1 of 8 from this Tutorial.

But make the necessary change mentioned below.

Requirements are that you have Dotnet Core installed.

And have the DB using EF Tools installed from NuGet

Creating the  .Net Razor App

1. Create a new blank RAZOR project

Create a new folder in your PC and run the command to create a new RAZOR project

image

Command : dotnet New Razor

This will create a new blank Razor project

2. Install the DB using EF Tools

Go to Nuget and look for this package

image

I used the version 2.1.0-rc1-final

Run this command :

dotnet add package Microsoft.EntityFrameworkCore.Tools –version 2.1.0-rc1-final

3. Add more packages needed

dotnet add package Microsoft.EntityFrameworkCore.Tools.DotNet –version 2.1.0-preview1-final

dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design –Version 2.0.0

dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Utils –Version 2.0.0

3. Add missing other packages

correct the project .Json package references

4. Add the missing code like to generate the DB

Or generate the code based on an existing DB using Scaffolding

Example : Create a Models folder. In the Models folder, create a class file named Student.cs

5. Create the xxxxContext DB context

In the Data folder create SchoolContext.cs

In Startup.cs add the missing references (Using xxx)

add the DB Context refence : services.AddEntityFrameworkSqlite().AddDbContext<SchoolContext>(); // Added

In the Data folder, create a new class file named DbInitializer.cs

6. Generate the View pages based on the model

Courses :

dotnet aspnet-codegenerator razorpage -m Course -dc SchoolContext -udl -outDir Pages\Courses –referenceScriptLibraries

Instructor :

dotnet aspnet-codegenerator razorpage -m Instructor -dc SchoolContext -udl -outDir Pages\Instructors –referenceScriptLibraries

7. Correct the ERRORS

When running the command in step 6 you will get ERRORS.

Globally change _context.Student to _context.Students (add an “s” to Student)

image

There are 7 occurrences found and updated. This is a bug and the hope to fix this bug in the next release.


=> TEST THE APP

8. BEFORE publishing add the following string in the program.cs and republish again.

UseURLs(“http://*:5000)

image

9. Publish for RPI3 – ARM

Run this command :

dotnet publish -r win8-arm

10. Copy from Publish to your RPI3

Copy the Files from the PUBLISH folder to your RPI3.

ASPRazor -> C:\CoreIoT\ASPRazor\bin\Debug\netcoreapp2.0\win8-arm\ASPRazor.dll

ASPRazor -> C:\CoreIoT\ASPRazor\bin\Debug\netcoreapp2.0\win8-arm\publish\

Next MAKE SURE your FOLDER STRUCTURE on the RPI3, is exactly the same as it was on your PC before Publishing.

Next COPY the SQLite Database to your RPI3. Because this is NOT included when publishing using DotNet Core to win8Arm

11. Test the .Net Razor App on your RPI3

Connect to PowerShell Remote to your RPI3 and start the app.

image

Let access the database ?

clip_image002

Let’s add some data ?

clip_image004

clip_image006

Haleluja, this is fully functional and cross platform !!

Since you now have an SQLite DB running your can feed it with your RPI3 SENSOR data and read it out using the Web J

Adding a Web Service to the .Net Razor App

Next I will add it to the site to see we can access the data using a web service API.

Again instead of using the full blown Visual Studio we will only use the command line options.

Because we don’t want to install an elephant weight of software !

image

Actually Visual Studio uses all these commands in the background too … Winking smile

Run this command :

dotnet aspnet-codegenerator controller -api -name StudentAPIController -m Student -dc SchoolContext -udl -outDir Models\API –referenceScriptLibraries -f

image

see the result

image

You also have the option to generate XML instead of JSON

image

Check the result !!

image

This is how you you select ID 5

ID = 5

clip_image002[5]

It contains GET / POST / PUT / DELETE methods !

Accessing the Web Service using PowerShell

image

CLS

$URL = "http://localhost:5000/api/StudentAPI"

$APIService = Invoke-RestMethod -Uri $URL

$APIService | select ID, lastName, firstMidName 

Enjoy !

Advertisements

Windows Core IoT – SQLite on a Raspberry PI3

April 15, 2018

As shown in the previous post you can run use Dotnet Core to compile apps for Windows IoT on a RPI3

See here how to get started with Dotnet Core

On the net you can find how to use Dotnet Core and SQLite :

https://developersoapbox.com/connecting-to-a-sqlite-database-using-net-core/

But the example uses the Dotnet Core v1.0 Framework and run on Windows.

I will show here how to use the latest Dotnet Core v2.0 Framework and publish it for Windows ARM on RPI3 Winking smile

Run command Dotnet new console –n Sqlite –o C:\DotNetCoreApps\Sqlite

And check the result here :

image

Copy the program.cs as backup, and copy the program code from the site above into the program.cs file.

Change the namespace to SQLite (see-n SQLite in first command)

clip_image002

Change the sqlite.csproj file content by adding a few lines

clip_image002

Add these line :

image

<Project Sdk=”Microsoft.NET.Sdk”>

  <PropertyGroup>
     <OutputType>Exe</OutputType>
     <TargetFramework>netcoreapp2.0</TargetFramework>
     <RuntimeIdentifiers>win8-arm;ubuntu.14.04-arm;ubuntu.16.04-arm</RuntimeIdentifiers>
   </PropertyGroup>
   <ItemGroup>
<PackageReference Include=”Microsoft.EntityFrameworkCore.Sqlite” Version=”2.0.2″ />
<PackageReference Include=”SQLitePCLRaw.bundle_green” Version=”1.1.10″ />

  </ItemGroup>
   <ItemGroup>
     <DotNetCliToolReference Include=”Microsoft.EntityFrameworkCore.Tools.DotNet” Version=”2.0.0″ />
   </ItemGroup>
</Project>

Run command : dotnet Restore

image

As you can see it will use some Nuget packages as included in the sqlite.csproj file

Next run this command : dotnet publish -r win8-arm

And copy the files to your RPI3

image

Next open PowerShell do a remote session and run the .\SQLite.exe

image

When the SQLite DB doesn’t exist yet, it will be created automatically.

image

DotNet Core is really a cross-platform language !

It runs flawlessly on a RPI3 – ARM Architecture, by just re-using the same code base we use on a Windows machine Smile

See here for more info on .NET Core Data Access data sources :

https://blogs.msdn.microsoft.com/dotnet/2016/11/09/net-core-data-access/

Enjoy !