投げ銭

★当サイトへの投げ銭(PayPal)★

LINK


(無償、寄付歓迎) logo
世界中で使われるISO標準オフィスソフト(MSオフィス互換)

★LibreOfficeの導入事例★
詳細について

人気の投稿(1ヶ月間)

Ad

Ad

投げ銭

★当サイトへの投げ銭(PayPal)★

2017年8月31日木曜日

【Windows 8.1 64bit】.NET Core 2.0 SDKと、Visual Studio 2017 Communityをセットアップし、MVC WEBアプリの作成と動作ができるようにした【.NET Core 2.0】【VS 2017 Community】

Windows 8.1 に、「.NET Core 2.0 SDK」と、「Visual Studio 2017 Community」 を導入し、
MVC方式のWEBアプリ(.NET Core 2.0)を作成、動作させられるようにした。

次の方法で、できた。



■最初に、Windowsのアップデートを行った。


■.NET Core 2.0 SDK のインストールを行った。

(注意)プラットフォームに応じたインストーラーを選択すること。

私の場合、下記リンクから、SDKを選択して、「Windows (x64) Installer」をダウンロードした。
(このとき、dotnet-sdk-2.0.0-win-x64.exe が得られた。)

https://www.microsoft.com/net/download/core

ダウンロードしたexeファイルを実行し、ダイアログに従い、導入を完了させた。


■Visual Studio 2017 Community のインストールを行った。

下記リンクから、「Visual Studio Community 2017」を選択し、ダウンロードした。
(このとき、vs_community__638787920.1504115583.exe が得られた。)

https://www.visualstudio.com/ja/downloads/

ダウンロードしたexeファイルを実行し、ダイアログに従い、以下を留意して導入を完了させた。


○バージョンの選択を行った。
Visual Studio Community 2017 Preview を選択した。

○そして、次の項目を指定した。(重要)
・ASP.NET と Web 開発
・Visual Studio 拡張機能の開発
・.NET Core クロスプラットフォームの開発

これらを指定しない場合、WEBアプリの動作時にエラーが多数発生した。


(注意)
ネットワーク経由でのインストールになるので、時間をそれなりに要した。

以上

ASP.NET MVCプログラミング入門 (マイクロソフト関連書)

新品価格
¥3,456から
(2018/5/21 15:43時点)



<参考>
・Hello World From .NET Core 2 And VS 2017 Preview And Difference In csproj
< http://www.c-sharpcorner.com/article/hello-world-from-net-core-2-and-vs-2017-preview-and-difference-in-csproj/ > 2017年8月31日

【Linux CentOS 7】VS2017で作成したサンプル.NET Core 2.0 MVC WEBアプリをLinuxでそのまま実行させる【.NET Core 2.0】

(更新 2019/1/26)外部から80番ポートに接続する設定



Windows上のVisual Studio 2017 Community で作成した「MVC WEBアプリ(.net core 2.0)」は、
.NET Core 2.0 SDK導入済みのLinuxマシン上に、プログラムデータフォルダごと持って来て、
dotnet runコマンドを実行するだけで、そのまま動作させることができた。



■(準備).NET Core 2.0 などのセットアップ方法について

Windowsマシンへの「.NET Core 2.0 SDK」、「VS 2017 Community」 の導入は次のようにして行った。
http://akira-arets.blogspot.jp/2017/08/windows81-install-dotnetcore2sdk-vs2017.html

Linuxマシンへの.NET Core 2.0 SDK 導入は次のようにして行った。
http://akira-arets.blogspot.jp/2017/08/linux-centos7-netcore2-helloworld.html



■まず、Windows上Visual Studio 2017 Communityで、サンプルMVC WEBアプリを作成した。

Windows上のVS 2017 communityで、WEBアプリ(.net core 2.0)テンプレートから、
テスト用WEBアプリを作成し、保存した。
ところで、これは、MVC WEBアプリ版のHello Worldのようなものである。
これをベースにして自分で、コントローラー、アクション、ビューを追加していけた。

デフォルトで、以下の場所に保存された。
C:\Users\myname\source\repos\testWebApplication1\

このフォルダにある、同名「testWebApplication1」を、zipで圧縮し、適当な方法で、
.NET Core 2.0 SDKを導入済みのLinuxマシンにコピーし解凍した。



<以下は、このLinuxマシン(.NET Core 2.0 SDK導入済み)での操作である。>

■次に、VS 2017 Communityで作成したテスト用WEBアプリをLinux上で実行した。

$ cd testWebApplication1
$ ls
Controllers  Program.cs  Startup.cs  appsettings.Development.json  bin         bundleconfig.json  testWebApplication1.csproj       wwwroot
Models       Properties  Views       appsettings.json              bower.json  obj                testWebApplication1.csproj.user

テスト用WEBアプリを実行した。
(もしここでroot権限で動作させた場合、このアプリでは次回以降もroot権限が必要になった。)
$ dotnet run
/home/myname/testWebApplication1/testWebApplication1/Properties/launchSettings.json からの起動設定を使用中...
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using '/root/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[58]
      Creating key {b2f7073b-fd26-478d-8895-6e7484676238} with creation date 2017-08-30 16:53:28Z, activation date 2017-08-30 16:53:28Z, and expiration date 2017-11-28 16:53:28Z.
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
      No XML encryptor configured. Key {b2f7073b-fd26-478d-8895-6e7484676238} may be persisted to storage in unencrypted form.
info: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[39]
      Writing data to file '/root/.aspnet/DataProtection-Keys/key-b2f7073b-fd26-478d-8895-6e7484676238.xml'.
Hosting environment: Development
Content root path: /home/myname/testWebApplication1/testWebApplication1
Now listening on: http://localhost:49293Application started. Press Ctrl+C to shut down.

○動作確認○
ここで、ローカルのブラウザから、上記に指定されたアドレスにアクセスした。
すると、MVCや、BootStrap を活用したサンプルページが表示された。


info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:49293/
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
      Executing action method testWebApplication1.Controllers.HomeController.Index (testWebApplication1) with arguments ((null)) - ModelState is Valid
info: Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ViewResultExecutor[1]
      Executing ViewResult, running view at path /Views/Home/Index.cshtml.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action testWebApplication1.Controllers.HomeController.Index (testWebApplication1) in 5734.6912ms
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 5997.6831ms 200 text/html; charset=utf-8
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:49293/lib/bootstrap/dist/css/bootstrap.css
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:49293/css/site.css
info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]
      Sending file. Request path: '/css/site.css'. Physical path: '/home/myname/testWebApplication1/testWebApplication1/wwwroot/css/site.css'
info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]
      Sending file. Request path: '/lib/bootstrap/dist/css/bootstrap.css'. Physical path: '/home/myname/testWebApplication1/testWebApplication1/wwwroot/lib/bootstrap/dist/css/bootstrap.css'
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 42.4335ms 200 text/css
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:49293/lib/jquery/dist/jquery.js
info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]
      Sending file. Request path: '/lib/jquery/dist/jquery.js'. Physical path: '/home/myname/testWebApplication1/testWebApplication1/wwwroot/lib/jquery/dist/jquery.js'
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 80.2129ms 200 text/css
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 28.1376ms 200 application/javascript
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:49293/lib/bootstrap/dist/js/bootstrap.js
info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]
      Sending file. Request path: '/lib/bootstrap/dist/js/bootstrap.js'. Physical path: '/home/myname/testWebApplication1/testWebApplication1/wwwroot/lib/bootstrap/dist/js/bootstrap.js'
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 5.6164ms 200 application/javascript
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:49293/js/site.js?v=7mkNbU1tgQL1bUeZe3j2R151hKLhLDKO4BBaR-iqCy0
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:49293/images/banner1.svg
info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]
      Sending file. Request path: '/js/site.js'. Physical path: '/home/myname/testWebApplication1/testWebApplication1/wwwroot/js/site.js'
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 9.1577ms 200 application/javascript
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:49293/images/banner2.svg
info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]
      Sending file. Request path: '/images/banner2.svg'. Physical path: '/home/myname/testWebApplication1/testWebApplication1/wwwroot/images/banner2.svg'
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 2.9037ms 200 image/svg+xml
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:49293/images/banner3.svg
info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]
      Sending file. Request path: '/images/banner3.svg'. Physical path: '/home/myname/testWebApplication1/testWebApplication1/wwwroot/images/banner3.svg'
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 8.067ms 200 image/svg+xml
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:49293/images/banner4.svg
info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]
      Sending file. Request path: '/images/banner4.svg'. Physical path: '/home/myname/testWebApplication1/testWebApplication1/wwwroot/images/banner4.svg'
info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]
      Sending file. Request path: '/images/banner1.svg'. Physical path: '/home/myname/testWebApplication1/testWebApplication1/wwwroot/images/banner1.svg'
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 29.3217ms 200 image/svg+xml
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 6.9818ms 200 image/svg+xml
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:49293/lib/bootstrap/dist/fonts/glyphicons-halflings-regular.woff2
info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]
      Sending file. Request path: '/lib/bootstrap/dist/fonts/glyphicons-halflings-regular.woff2'. Physical path: '/home/myname/testWebApplication1/testWebApplication1/wwwroot/lib/bootstrap/dist/fonts/glyphicons-halflings-regular.woff2'
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 0.7479ms 200 font/woff2
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:49293/favicon.ico
info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]
      Sending file. Request path: '/favicon.ico'. Physical path: '/home/myname/testWebApplication1/testWebApplication1/wwwroot/favicon.ico'
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 0.7905ms 200 image/x-icon

○「CTRL+C」を押して、WEBアプリを終了した○
^CApplication is shutting down...


■httpサーバのポートを80にし、外部のマシンからの接続を受け付ける方法

動作中の dotnet run コマンドは、CTRL+C で止めておく。
プロジェクト内の次のファイルに下記のように1行追加して、
dotnet run コマンドで起動しなおす。

# cat Program.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace test
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }
        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseUrls(urls: "http://0.0.0.0:80")
                .Build();
    }
}



以上


新品価格
¥3,456から
(2018/5/21 15:43時点)


<参考>
・Setting ASP.NET host address in .NET Core 2
< https://www.billbogaiv.com/posts/setting-aspnet-host-address-in-net-core-2 > 2019年1月26日

・ASP.NET CoreのWebApplicationを外部公開
< https://qiita.com/husky774rr/items/256609e9d126653274f0 > 2019年1月26日

【Linux CentOS 7】.NET Core 2.0 SDK をインストールしてHelloWorldを表示するコンソールプログラムを実行した【.NET Core 2.0】

CentOS 7 64bitに、.NET Core 2.0 SDK を導入し、サンプルプログラムを実行した。
「参考」のリンクにあるページ通りに作業を行った。


■システムの更新

# yum update
# uname -a
Linux localhost.localdomain 3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux


■専用リポジトリを利用するための設定

# rpm --import https://packages.microsoft.com/keys/microsoft.asc

○repoファイルの生成
# sh -c 'echo -e "[packages-microsoft-com-prod]\nname=packages-microsoft-com-prod \nbaseurl=https://packages.microsoft.com/yumrepos/microsoft-rhel7.3-prod\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/dotnetdev.repo'

# cat /etc/yum.repos.d/dotnetdev.repo
[packages-microsoft-com-prod]
name=packages-microsoft-com-prod
baseurl=https://packages.microsoft.com/yumrepos/microsoft-rhel7.3-prod
enabled=1
gpgcheck=1
gpgkey=https://packages.microsoft.com/keys/microsoft.asc


■パッケージの導入(1)

# yum install libunwind libicu -y
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.tsukuba.wide.ad.jp
 * epel: ftp.riken.jp
 * extras: ftp.tsukuba.wide.ad.jp
 * ius: mirrors.tuna.tsinghua.edu.cn
 * nux-dextop: li.nux.ro
 * updates: ftp.tsukuba.wide.ad.jp
パッケージ libicu-50.1.2-15.el7.x86_64 はインストール済みか最新バージョンです
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ libunwind.x86_64 2:1.1-5.el7_2.2 を インストール
--> 依存性解決を終了しました。
依存性を解決しました
========================================================================================================================================================================
 Package                                 アーキテクチャー                     バージョン                                       リポジトリー                        容量
========================================================================================================================================================================
インストール中:
 libunwind                               x86_64                               2:1.1-5.el7_2.2                                  base                                56 k
トランザクションの要約
========================================================================================================================================================================
インストール  1 パッケージ
総ダウンロード容量: 56 k
インストール容量: 146 k
Downloading packages:
libunwind-1.1-5.el7_2.2.x86_64.rpm                                                                                                               |  56 kB  00:00:00  
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  インストール中          : 2:libunwind-1.1-5.el7_2.2.x86_64                                                                                                        1/1
  検証中                  : 2:libunwind-1.1-5.el7_2.2.x86_64                                                                                                        1/1
インストール:
  libunwind.x86_64 2:1.1-5.el7_2.2                                                                                                                                    
完了しました!


■パッケージの導入(2)

yum install dotnet-sdk-2.0.0
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.tsukuba.wide.ad.jp
 * epel: ftp.riken.jp
 * extras: ftp.tsukuba.wide.ad.jp
 * ius: mirrors.tuna.tsinghua.edu.cn
 * nux-dextop: li.nux.ro
 * updates: ftp.tsukuba.wide.ad.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ dotnet-sdk-2.0.0.x86_64 0:2.0.0-1 を インストール
--> 依存性の処理をしています: dotnet-runtime-2.0.0 >= 2.0.0 のパッケージ: dotnet-sdk-2.0.0-2.0.0-1.x86_64
--> 依存性の処理をしています: aspnetcore-store-2.0.0 >= 2.0.0 のパッケージ: dotnet-sdk-2.0.0-2.0.0-1.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ aspnetcore-store-2.0.0.x86_64 0:2.0.0-1 を インストール
---> パッケージ dotnet-runtime-2.0.0.x86_64 0:2.0.0-1 を インストール
--> 依存性の処理をしています: dotnet-hostfxr-2.0.0 のパッケージ: dotnet-runtime-2.0.0-2.0.0-1.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ dotnet-hostfxr-2.0.0.x86_64 0:2.0.0-1 を インストール
--> 依存性の処理をしています: dotnet-host >= 2.0.0 のパッケージ: dotnet-hostfxr-2.0.0-2.0.0-1.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ dotnet-host.x86_64 0:2.0.0-1 を インストール
--> 依存性解決を終了しました。
依存性を解決しました
========================================================================================================================================================================
 Package                                       アーキテクチャー              バージョン                        リポジトリー                                        容量
========================================================================================================================================================================
インストール中:
 dotnet-sdk-2.0.0                              x86_64                        2.0.0-1                           packages-microsoft-com-prod                         85 M
依存性関連でのインストールをします:
 aspnetcore-store-2.0.0                        x86_64                        2.0.0-1                           packages-microsoft-com-prod                         24 M
 dotnet-host                                   x86_64                        2.0.0-1                           packages-microsoft-com-prod                         42 k
 dotnet-hostfxr-2.0.0                          x86_64                        2.0.0-1                           packages-microsoft-com-prod                        181 k
 dotnet-runtime-2.0.0                          x86_64                        2.0.0-1                           packages-microsoft-com-prod                         24 M
トランザクションの要約
========================================================================================================================================================================
インストール  1 パッケージ (+4 個の依存関係のパッケージ)
総ダウンロード容量: 133 M
インストール容量: 133 M
Is this ok [y/d/N]: y
Downloading packages:
(1/5): dotnet-host-2.0.0-rhel.7-x64.rpm                                                                                                          |  42 kB  00:00:00  
(2/5): dotnet-hostfxr-2.0.0-rhel.7-x64.rpm                                                                                                       | 181 kB  00:00:00  
(3/5): aspnetcore-store-2.0.0-rhel.7-x64.rpm                                                                                                     |  24 MB  00:00:38  
(4/5): dotnet-runtime-2.0.0-rhel.7-x64.rpm                                                                                                       |  24 MB  00:00:46  
(5/5): dotnet-sdk-2.0.0-rhel-x64.rpm                                                                                                             |  85 MB  00:01:45  
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
合計                                                                                                                                    949 kB/s | 133 MB  00:02:24  
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  インストール中          : aspnetcore-store-2.0.0-2.0.0-1.x86_64                                                                                                   1/5
  インストール中          : dotnet-host-2.0.0-1.x86_64                                                                                                              2/5
Creating dotnet host symbolic link: /usr/bin/dotnet
  インストール中          : dotnet-hostfxr-2.0.0-2.0.0-1.x86_64                                                                                                     3/5
  インストール中          : dotnet-runtime-2.0.0-2.0.0-1.x86_64                                                                                                     4/5
  インストール中          : dotnet-sdk-2.0.0-2.0.0-1.x86_64                                                                                                         5/5
This software may collect information about you and your use of the software, and send that to Microsoft.
Please visit http://aka.ms/dotnet-cli-eula for more information.
Welcome to .NET Core!
---------------------
Learn more about .NET Core @ https://aka.ms/dotnet-docs. Use dotnet --help to see available commands or go to https://aka.ms/dotnet-cli-docs.
.NET Core Tools Telemetry
--------------
The .NET Core Tools include a telemetry feature that collects usage information. It is important that the .NET Team understands how the tools are being used so that we can improve them.
The data collected is anonymous and will be published in an aggregated form for use by both Microsoft and community engineers under the Creative Commons Attribution License.
The .NET Core Tools telemetry feature is enabled by default. You can opt-out of the telemetry feature by setting an environment variable DOTNET_CLI_TELEMETRY_OPTOUT (for example, 'export' on macOS/Linux, 'set' on Windows) to true (for example, 'true', 1). You can read more about .NET Core tools telemetry at https://aka.ms/dotnet-cli-telemetry.
Installation Note
--------------
A command will be run during the install process that will improve project restore speed and enable offline access. It will take up to a minute to complete.
  検証中                  : dotnet-host-2.0.0-1.x86_64                                                                                                              1/5
  検証中                  : dotnet-sdk-2.0.0-2.0.0-1.x86_64                                                                                                         2/5
  検証中                  : dotnet-hostfxr-2.0.0-2.0.0-1.x86_64                                                                                                     3/5
  検証中                  : aspnetcore-store-2.0.0-2.0.0-1.x86_64                                                                                                   4/5
  検証中                  : dotnet-runtime-2.0.0-2.0.0-1.x86_64                                                                                                     5/5
インストール:
  dotnet-sdk-2.0.0.x86_64 0:2.0.0-1                                                                                                                                  
依存性関連をインストールしました:
  aspnetcore-store-2.0.0.x86_64 0:2.0.0-1      dotnet-host.x86_64 0:2.0.0-1      dotnet-hostfxr-2.0.0.x86_64 0:2.0.0-1      dotnet-runtime-2.0.0.x86_64 0:2.0.0-1  
完了しました!


■テストコンソールアプリケーションの生成と実行

# dotnet new console -o hwapp
.NET Core へようこそ!
---------------------
.NET Core について詳しくは、https://aka.ms/dotnet-docs をご覧ください。dotnet --help を使って使用可能なコマンドを確認するか、https://aka.ms/dotnet-cli-docs をご覧ください。
テレメトリ
--------------
.NET Core ツールは、操作性を向上させるために利用状況データを収集します。データは匿名で、コマンドライン引数が含まれません。データは Microsoft によって収集され、コミュニティと共有されます。
テレメトリを無効にするには、お好きなシェルを使用して、DOTNET_CLI_TELEMETRY_OPTOUT 環境変数を 1 に設定します。
.NET Core ツールのテレメトリについて詳しくは、https://aka.ms/dotnet-cli-telemetry をお読みください。
準備しています...
テンプレート "Console Application" が正常に作成されました。
作成後のアクションを処理しています...
'dotnet restore' を hwapp/hwapp.csproj で実行しています...
  Restoring packages for /home/myname/hwapp/hwapp.csproj...
  Generating MSBuild file /home/myname/hwapp/obj/hwapp.csproj.nuget.g.props.
  Generating MSBuild file /home/myname/hwapp/obj/hwapp.csproj.nuget.g.targets.
  Restore completed in 426.36 ms for /home/myname/hwapp/hwapp.csproj.

正常に復元されました。
# cd hwapp
# ls
Program.cs  hwapp.csproj  obj

以下のように、デフォルトで”Hello World”を表示するコードが書かれていた。
# cat Program.cs
using System;
namespace hwapp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
# cd obj/
# ls
hwapp.csproj.nuget.cache  hwapp.csproj.nuget.g.props  hwapp.csproj.nuget.g.targets  project.assets.json
# cd ..
# pwd
/home/myname/hwapp

プログラムを実行した。(起動するまでに10秒程度要した。)
# dotnet run
Hello World!


以上

ASP.NET MVCプログラミング入門 (マイクロソフト関連書)

新品価格
¥3,456から
(2018/5/21 15:43時点)



<参考>
・Install .NET and build your first app on CentOS or Oracle Linux
< https://www.microsoft.com/net/core#linuxcentos > 2017年8月30日

2017年8月7日月曜日

【Linux CentOS 6.9 64bit】Pythonで簡易的WEBアプリを動作、GETでアクセスしLinuxスクリプトを重複動作をしないように考慮して実行させる。【Python 3.5】

以下の動作環境には、Python3が必要である。
CentOS6.9には、Python3が入っていないので、別途インストールする必要があった。

☆Python3.5をyumで導入する手順

http://akira-arets.blogspot.jp/2017/06/linux-centos68-install-python35-yum.html



■リモートから呼び出したいスクリプトを作成した。

★スクリプトは、重複動作をさけるために、判定ルーティンを設けた。

$ pwd
/home/test/PYTHON3
$ cat testscript.sh
#!/bin/sh

LOGFILE=/home/test/PYTHON3/log

#ここから、重複起動の判定ルーチン

# コマンドラインで指定したパス名 $0 を検索キーにして、既に動作中のプロセスを検索し、最も古いプロセスのIDを取得する。
psnum=`pgrep -fo "$0"`

echo `date` >> $LOGFILE
echo "The Current Script PID is $$" >> $LOGFILE
echo "The COMMAND LINE driven by is $0" >> $LOGFILE
echo "The Firstest PID driven by same COMMAND LINE is $psnum" >> $LOGFILE

# 取得した最古のプロセスIDと、自身のプロセスIDとを比較する。
# 一致しなければ、同一パス名 $0 で呼び出されたプロセスが他に既に存在しているため、二重起動を防止し中断する。

if [ "$$" != "$psnum"  ] ; then
        echo "STOP!  Already This Script working as PID $psnum. and exit." >> $LOGFILE     # 既に同じパスで指定されるスクリプトは起動中である。(二重起動防止のた め中断)
        exit 1
fi

echo "OK!  Starting This Script as PID $$."  >> $LOGFILE             #同じパスで指定されるスクリプトは起動していない。(さらに処理を継続)

#ここまで判定ルーチン


#以下はメインの処理

sleep 70s


■上記スクリプトを呼び出すためのWEBアプリを作成した。

$ pwd
/home/test/PYTHON3

ベースとなるコードは、参考文献(1)のものを利用した。
$ cat webtest-v1.0.py
#!/usr/bin/env python3.5
from http.server import HTTPServer, SimpleHTTPRequestHandler
import subprocess

class testHTTPRequestHandler(SimpleHTTPRequestHandler):

        def do_GET(self):

                body = '<!DOCTYPE html><html><body>welcome</body></html>'

                if self.path == '/test/':
                        body = '<html><body>test test test</body></html>'

                        subprocess.call(['/home/test/PYTHON3/testscript.sh &'], shell=True)

                body = body.encode('utf8')

                self.send_response(200)
                self.send_header('Content-type','text/html; charset=utf-8')
                self.send_header('Content-length',len(body))
                self.end_headers()

                self.wfile.write(body)


if __name__ == '__main__':
        #httpd = HTTPServer(('localhost',8000),testHTTPRequestHandler)
        httpd = HTTPServer(('',8000),testHTTPRequestHandler)
        print('Serving HTTP')
        httpd.serve_forever()


■動作テスト

○WEBアプリの起動

$ ./webtest-v1.0.py
Serving HTTP


○初期状態の確認

$ pgrep -fo /home/test/PYTHON3/testscript.sh

$ cat log
cat: log: No such file or directory



<テスト 1>

○ブラウザから、http://ipアドレス:8000/test/ を呼び出し
192.168.250.41 - - [07/Aug/2017 04:03:08] "GET /test/ HTTP/1.1" 200 -
192.168.250.41 - - [07/Aug/2017 04:03:08] "GET /favicon.ico HTTP/1.1" 200 -
○ログを確認

$ cat log
Mon Aug 7 04:03:08 JST 2017
The Current Script PID is 18929
The COMMAND LINE driven by is /home/test/PYTHON3/testscript.sh
The Firstest PID driven by same COMMAND LINE is 18929
OK!  Starting This Script as PID 18929.
$ pgrep -fo /home/test/PYTHON3/testscript.sh
18929


初期状態では、まだスクリプトは動作していないので、処理を継続できた。



<テスト 2>

○以上に続けて、直ぐに、ブラウザから、http://ipアドレス:8000/test/ を呼び出し
192.168.250.41 - - [07/Aug/2017 04:03:48] "GET /test/ HTTP/1.1" 200 -
192.168.250.41 - - [07/Aug/2017 04:03:48] "GET /favicon.ico HTTP/1.1" 200 -
○ログを確認

$ cat log
Mon Aug 7 04:03:08 JST 2017
The Current Script PID is 18929
The COMMAND LINE driven by is /home/test/PYTHON3/testscript.sh
The Firstest PID driven by same COMMAND LINE is 18929
OK!  Starting This Script as PID 18929.
Mon Aug 7 04:03:48 JST 2017
The Current Script PID is 18937
The COMMAND LINE driven by is /home/test/PYTHON3/testscript.sh
The Firstest PID driven by same COMMAND LINE is 18929
STOP!  Already This Script working as PID 18929. and exit.
$ pgrep -fo /home/test/PYTHON3/testscript.sh
18929


既に、スクリプトが実行中だったので、スクリプトの実行は中断された 。



<テスト 3>

○70秒以上経過後に、スクリプトが動作していないことを確認

$ pgrep -fo /home/test/PYTHON3/testscript.sh



○ブラウザから、http://ipアドレス:8000/test/ を呼び出し
192.168.250.41 - - [07/Aug/2017 04:05:12] "GET /test/ HTTP/1.1" 200 -
192.168.250.41 - - [07/Aug/2017 04:05:12] "GET /favicon.ico HTTP/1.1" 200 -

○ログを確認

$ cat log
Mon Aug 7 04:03:08 JST 2017
The Current Script PID is 18929
The COMMAND LINE driven by is /home/test/PYTHON3/testscript.sh
The Firstest PID driven by same COMMAND LINE is 18929
OK!  Starting This Script as PID 18929.
Mon Aug 7 04:03:48 JST 2017
The Current Script PID is 18937
The COMMAND LINE driven by is /home/test/PYTHON3/testscript.sh
The Firstest PID driven by same COMMAND LINE is 18929
STOP!  Already This Script working as PID 18929. and exit.
Mon Aug 7 04:05:12 JST 2017
The Current Script PID is 18945
The COMMAND LINE driven by is /home/test/PYTHON3/testscript.sh
The Firstest PID driven by same COMMAND LINE is 18945
OK!  Starting This Script as PID 18945.


$ pgrep -fo /home/test/PYTHON3/testscript.sh
18945


スクリプトが起動していない状態に復帰していたので、スクリプトの処理を継続できだ。

以上で、テスト完了



■WEBアプリを終了した。

CTRL + c で終了

$ ./webtest-v1.0.py
Serving HTTP
192.168.250.41 - - [07/Aug/2017 04:03:08] "GET /test/ HTTP/1.1" 200 -
192.168.250.41 - - [07/Aug/2017 04:03:08] "GET /favicon.ico HTTP/1.1" 200 -
192.168.250.41 - - [07/Aug/2017 04:03:48] "GET /test/ HTTP/1.1" 200 -
192.168.250.41 - - [07/Aug/2017 04:03:48] "GET /favicon.ico HTTP/1.1" 200 -
192.168.250.41 - - [07/Aug/2017 04:05:12] "GET /test/ HTTP/1.1" 200 -
192.168.250.41 - - [07/Aug/2017 04:05:12] "GET /favicon.ico HTTP/1.1" 200 -
^CTraceback (most recent call last):
  File "./webtest-v1.0.py", line 30, in <module>
    httpd.serve_forever()
  File "/usr/lib64/python3.5/socketserver.py", line 232, in serve_forever
    ready = selector.select(poll_interval)
  File "/usr/lib64/python3.5/selectors.py", line 376, in select
    fd_event_list = self._poll.poll(timeout)
KeyboardInterrupt



<参考>
(1) 露木誠/小田切篤 「15時間でわかる Python 集中講座」 技術評論社 2016年、P.216-223 

・subprocess – プロセスを生成して連携する
< http://ja.pymotw.com/2/subprocess/ > 2017年8月6日

・python上でunixコマンドを実行する
< http://qiita.com/tdrk/items/9b23ad6a58ac4032bb3b > 2017年8月6日

・同じパスで指定されるスクリプトが同時に実行されないようにする方法について
< http://akira-arets.blogspot.jp/2017/06/linux-check-whether-running.html > 2017年8月6日

2017年8月5日土曜日

【Fetchmail+Postfix】POP3で受信したメールの再配送で転送ループエラーが生じバウンスされる問題の理解【Linux CentOS 6】

Fetchmailで、あるPOP3サーバから受信したメールをPostfixで再配送すると、下記のようにmail forwarding loop が発生した。
Aug  4 00:00:00 MyMailServer postfix/pipe[27838]: xxxxxxxx: to=<receiver@example1.jp>, relay=procmail, delay=1.1, delays=0.83/0.07/0/0.15, dsn=5.4.6, status=bounced (mail forwarding loop for receiver@example1.jp)
このエラーのためメールのローカル配信プロセスはここで中断されてしまった。
エラーを発生させたメールサーバはPostfixで、バーチャルドメインを設定し、ローカル配信などを担当しているものである。
さらにprocmailなどで指定した転送処理などもされない。



■問題が発生しないPOP3サーバもあった。

しかし、同じ仕組みでFetchmailで受信しPostfixに処理を任せていたこれ以外のPOP3サーバについては、問題なく配送プロセスを完了できた。

メールヘッダーを比較してみると、Fetchmailが受信しているPOP3メールサーバによって、
「Delivered-To:」ヘッダーの内容のパターンが異なっていることに気がついた。



■Fetchmail周りのメールヘッダーを確認した。

○問題が発生したメールのヘッダー(pop3.test [x.y.z.a]からfetchmailで受信)
Received: from MyMailServer.local (localhost [127.0.0.1])
by postfix1.local (Postfix) with ESMTP id xxxxxxxxxx
for <receiver@example1.jp>; Thu, 3 Aug 2017 00:00:00 +0900 (JST)

X-Original-To: receiver@example1.jp
Delivered-To: receiver@example1.jp
Received: from pop3.test [x.y.z.a]
by MyMailServer.local with POP3 (fetchmail-6.3.17)
for <receiver@example1.jp> (single-drop); Thu, 03 Aug 2017 00:00:00 +0900 (JST)
○問題が発生しなかったメールのヘッダー(pop3.example.co.jp [b.c.d.e]からfetchmailで受信)
Received: from MyMailServer.local (localhost [127.0.0.1])
by postfix1.local (Postfix) with ESMTP id xxxxxxxxxx
for <support@example.co.jp>; Sat, 5 Aug 2017 00:00:00 +0900 (JST)

Delivered-To: m19200000-reciver@example.co.jp
Received: from pop3.example.co.jp [b.c.d.e]
by MyMailServer.local with POP3 (fetchmail-6.3.17)
for <receiver@example.co.jp> (single-drop); Sat, 05 Aug 2017 00:00:00 +0900 (JST)

Delivered-Toヘッダーに注目すると、問題が発生しなかったメールの場合、
Delivered-Toヘッダーに記載されているユーザー名に、ハイフンで「m19200000」が繋げられている。
Delivered-Toヘッダーのアドレスと、for <receiver@example.co.jp> に記載されているアドレスは異なるものになっていた

問題の発生しなかったさらに他のPOP3サーバーについても、同じパターンではなかったものの、
Delivered-Toヘッダーのアドレスが、for内のアドレスと一致しないようになっていた。

Fetchmailの設定は共通であるため、この差は、受信元POP3サーバに拠るものだと考えた。
あるいは、受信元POP3サーバが付加したものだと考えられる。



■原因らしいDelivered-Toヘッダーを記載しないようにした。

問題の発生するPOP3サーバからの受信に際して、
メールヘッダーにDelivered-Toヘッダーを記載しないようにした。

Fetchmailには、Delivered-Toヘッダーを記載しないオプションがある。
設定ファイルの次の箇所に「dropdelivered」というオプションを挿入した。
(省略)
username "receiver@example1.jp" password "thisispwd" dropdelivered is "receiver@example1.jp" here
(省略)

○オプションなし
Received: from MyMailServer.local (localhost [127.0.0.1])
by postfix1.local (Postfix) with ESMTP id xxxxxxxxxx
for <receiver@example1.jp>; Thu, 3 Aug 2017 00:00:00 +0900 (JST)

X-Original-To: receiver@example1.jp
Delivered-To: receiver@example1.jp
Received: from pop3.test [x.y.z.a]
by MyMailServer.local with POP3 (fetchmail-6.3.17)
for <receiver@example1.jp> (single-drop); Thu, 03 Aug 2017 00:00:00 +0900 (JST)

Received: from mail.test (localhost [127.0.0.1])
by mail.test (Postfix) with ESMTP id yyyyyyyyyyyy
for <receiver@example1.jp>; Thu, 3 Aug 2017 00:00:00 +0900 (JST)



○オプションあり
Received: from MyMailServer.local (localhost [127.0.0.1])
by postfix1.local (Postfix) with ESMTP id xxxxxxxxxx
for <receiver@example1.jp>; Sat, 5 Aug 2017 00:00:00 +0900 (JST)

X-Original-To: receiver@example1.jp
Received: from pop3.test [x.y.z.a]
by MyMailServer.local with POP3 (fetchmail-6.3.17)
for <receiver@example1.jp> (single-drop); Sat, 05 Aug 2017 00:00:00 +0900 (JST)
Received: from mail.test (localhost [127.0.0.1])
by mail.test (Postfix) with ESMTP id yyyyyyyyyyyy
for <receiver@example1.jp>; Sat, 5 Aug 2017 00:00:00 +0900 (JST)

以上のように、「dropdelivered」というオプションが有効に機能して、
Delivered-Toヘッダーが記載されないようになった。

また、これによって、冒頭の「mail forwarding loop」の問題も解消した



■なぜ「mail forwarding loop」が発生したのか。

下記、参考ページ(†1)によれば、
ISPなどのPOP3サーバが、Delivered-Toラインをメールに付加する。
そして、Fetchmailがこのメールを受信しPostfixに転送すると、Postfixはこのラインを見つけて、
mail forwarding loop error をスローして、ローカル配送を中断する。

しかし、以上の検証では、Delivered-Toラインが付加されていたとしても、そのアドレスが、
for<アドレス> に一致していない場合には、問題が生じないことがわかった。


○次のようにイメージすることができると思う。

ローカル配信を担当するPostfixにしてみれば、
転送されてきたメールは、確かに自分が管理するローカルのユーザー(Aさん)宛のものだとしても、
既にそれには「配送済みマーク(Delivered-To Aさん)」が記載されているので、
いったん届け終わったものがまた外部からやってきたのだと思い首をかしげるわけだろう。
届けたはずなのにまた回ってきたということは「メールループしている!」と考えて
そこで、配送担当のPostfixは、それ以上はAさんに配信しないように中断するわけだ。
再配達防止機能(メールループ阻止機能)である。


つまり、
ローカル配送を担当するPostfixは、Delivered-Toラインがある場合には、
そこに記載されているアドレスと、ローカル配送先アドレス(エンベロープアドレス?)とを比較し、転送ループしているかどうかを判定する。

一致するならば、転送がループしているとしてエラーが発生し、
一致しなければ、問題なく、新たな配信先として配送プロセスが完了する。


<エラーが発生する場合の図>

・【Aさん済印】・・・ヘッダーに配送済み印 「Delivered-To : Aさん」 が記載されたメール

POP3サーバ【Aさん済印】⇒Fetchmail【Aさん済印】(Aさん宛)⇒中継Postfix【Aさん済印】(Aさん宛)⇒ローカル配送Postfix【Aさん済印】(Aさん宛)←←←ここで転送ループエラー発生!!「お届け済みのメールがまた回ってきたよ!」


<Delivered-ToラインをFetchmailのオプションで削除した場合>

・【Aさん済印】・・・ヘッダーに配送済み印 「Delivered-To : Aさん」 が記載されたメール
・【-】・・・ヘッダーからDelivered-To : Aさん が削除されたメール

POP3サーバ【Aさん済印】⇒Fetchmail《済印クリア》【-】(Aさん宛)⇒中継Postfix【-】(Aさん宛)⇒ローカル配送Postfix【-】(Aさん宛)←←←配送プロセス完了、あるいはprocmailなどでさらに処理を継続する。



■まとめ

FetchmailでPOP3サーバから受信したメール(いったん配送済みになったもの)を、さらにメールの配送プロセスに載せる前には、
「配送済みマーク(Delivered-To Aさん)」には注意する必要がある。

新たな配送プロセスの先にあるローカル配信先アドレスと、「配送済みマーク(Delivered-To アドレス)」が一致する場合、
今回の問題のように、再配達防止機能が検知して、中断されてしまう。

一致する場合には、上記の方法でFetchmailにオプションを付けて、
「配送済みマーク(Delivered-To アドレス)」を剥がす必要がある。

一致しないのであれば、問題は発生しない。
そんなDelivered-Toラインがあってもかまわない。



<参考>
(1)Fetchmail and postfix 'mail forwarding loop' - SOLVED
< https://lists.kolab.org/pipermail/users/2005-November/003755.html > 2017年8月5日

(2)Delivered-To【メールヘッダ】
< http://wa3.i-3-i.info/word11114.html > 2017年8月5日

投げ銭

★当サイトへの投げ銭(PayPal)★

Ad

Ad