やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

Raspbian stretch dotnet コマンドで .NET Core コンソールアプリ プロジェクトを作成&ビルドし実行できた

 MonoDevelopではできないけど、dotnetコマンドでなら実行ファイルを作成できた。

前回

対象環境

参考

 2018-12-17時点では、Raspbian(ARM32)でビルドする情報が全然なかった。

手順

 事前に.NET Core 2.2.101をインストールしておくこと。

 一気にやりたいなら以下のコマンドをコピペして実行する。

mkdir -p /tmp/work/HelloDotNetCoreConsole
cd /tmp/work/HelloDotNetCoreConsole
dotnet new console
dotnet restore
dotnet publish -r linux-arm
/tmp/work/DotNetProjects/HelloDotNetCoreConsole/bin/Debug/netcoreapp2.2/linux-arm/HelloDotNetCoreConsole

 以下、コマンドを一つずつ見ていく。

1. 任意のパスに任意のディレクトリを作成する(プロジェクト名)

$ cd /tmp/work
$ mkdir -p HelloDotNetCoreConsole

2. プロジェクトを作成する

$ dotnet new console

 以下のような出力がされ、.csprojなどのファイルが作成される。

The template "Console Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on /tmp/work/DotNetProjects/HelloDotNetCoreConsole/HelloDotNetCoreConsole.csproj...
  Restoring packages for /tmp/work/DotNetProjects/HelloDotNetCoreConsole/HelloDotNetCoreConsole.csproj...
  Generating MSBuild file /tmp/work/DotNetProjects/HelloDotNetCoreConsole/obj/HelloDotNetCoreConsole.csproj.nuget.g.props.
  Generating MSBuild file /tmp/work/DotNetProjects/HelloDotNetCoreConsole/obj/HelloDotNetCoreConsole.csproj.nuget.g.targets.
  Restore completed in 3.81 sec for /tmp/work/DotNetProjects/HelloDotNetCoreConsole/HelloDotNetCoreConsole.csproj.

Restore succeeded.

3. リストアする

$ dotnet restore
  Restore completed in 545.73 ms for /tmp/work/DotNetProjects/HelloDotNetCoreConsole/HelloDotNetCoreConsole.csproj.

 ところでリストアって何?

4. バイナリを生成する

$ dotnet publish -r linux-arm

 以下のような応答をし、実行ファイルなどを生成する。

Microsoft (R) Build Engine version 15.9.20+g88f5fadfbe for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restoring packages for /tmp/work/DotNetProjects/HelloDotNetCoreConsole/HelloDotNetCoreConsole.csproj...
  Installing runtime.linux-arm.Microsoft.NETCore.DotNetAppHost 2.2.0.
  Installing runtime.linux-arm.Microsoft.NETCore.DotNetHostResolver 2.2.0.
  Installing runtime.linux-arm.Microsoft.NETCore.DotNetHostPolicy 2.2.0.
  Installing runtime.linux-arm.Microsoft.NETCore.App 2.2.0.
  Restore completed in 1.04 min for /tmp/work/DotNetProjects/HelloDotNetCoreConsole/HelloDotNetCoreConsole.csproj.
  HelloDotNetCoreConsole -> /tmp/work/DotNetProjects/HelloDotNetCoreConsole/bin/Debug/netcoreapp2.2/linux-arm/HelloDotNetCoreConsole.dll
  HelloDotNetCoreConsole -> /tmp/work/DotNetProjects/HelloDotNetCoreConsole/bin/Debug/netcoreapp2.2/linux-arm/publish/

5. 実行する

$ /tmp/work/DotNetProjects/HelloDotNetCoreConsole/bin/Debug/netcoreapp2.2/linux-arm/HelloDotNetCoreConsole

 コンソールアプリの処理が実行される。

Hello World!

ソースコード

 ソリューションファイル(.sln)は作成されないようだ。

HelloDotNetCoreConsole.csproj

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

</Project>

Program.cs

using System;

namespace HelloDotNetCoreConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

ファイルサイズ

 完了した時点でファイル一式のサイズはなんと約66.5MBだった。こんなに重いHelloWorldはお前が初めてだ。

MonoDevelopで起動するもエラー

 作成したファイル一式をコピーし、MonoDevelop.csprojを開いてみた。.slnファイルが作成された。実行するも、前回同様、以下のエラー。

/tmp/work/Projects/HelloDotNetCoreConsole/HelloDotNetCoreConsole.csproj: Error: NETSDK1061: The project was restored using Microsoft.NETCore.App version 1.0.0, but with current settings, version 2.2.0 would be used instead. To resolve this issue, make sure the same settings are used for restore and for subsequent operations such as build or publish. Typically this issue can occur if the RuntimeIdentifier property is set during build or publish but not during restore. For more information, see https://aka.ms/dotnet-runtime-patch-selection. (HelloDotNetCoreConsole)

f:id:ytyaru:20181217095055p:plain

所感

 raspbianでは.NET Coreのビルドができないという情報はもう古い。ばっちりできた。でもMonoDevelopではできない……。

 なぜMonoDevelopでやるとエラーになるの? restoreだのpublishだのという呪文のせいか? でもそこはdotnetコマンドのパスをMonoDevelopで設定したので、うまいことやってくれるのでは?