Nano Server上のASP.NET Core

ASP.NET Coreの操作に関する5つの記事の最初の紹介:IISを使用してNano ServerにASP.NET Coreアプリケーションを展開するためのガイド。







ASP.NET Coreに関する記事の最初のシリーズ



1. Nano Server上のASP.NET Core 。

2. アプリケーションのWebサービスフロントエンドを作成します 。

3. ASP.NET Core MVCおよびVisual Studioを使用した最初のWeb APIの作成 。

4. Visual Studioを使用して、WebアプリケーションをAzure Application Serviceにデプロイします 。

5. Visual Studio Codeを使用した最初のMacアプリケーション 。



はじめに



Nano ServerはWindows Server 2016のインストールオプションであり、サイズがよりコンパクトで、Server Coreおよび完全インストールオプションよりも多くのメンテナンスおよびセキュリティ機能を備えています。 詳細については、Nano Serverインストールオプションの公式ドキュメントを参照してください。 それを始めるには3つの方法があります:



1. Windows Server 2016 Technical Preview 5 ISOファイルをダウンロードして、Nano Serverイメージを作成します。

2.開発者向けにNano Server VHDをダウンロードします。

3. Azure GalleryのNano Serverイメージを使用して、Azureで仮想マシンを作成します。 Azureアカウントを持っていない場合は、30日間の試用期間で無料アカウントを作成できます。



この記事では、Windows Server Technical Preview 5の開発者向けに事前構築済みのNano Server VHDを使用します。



次に、64ビットASP.NET Coreアプリケーションを作成して公開する必要があります。



Nano Serverインスタンスの準備



事前にロードされたVHDファイルを使用して、新しいHyper-V仮想マシンを作成します。 ログインする前に、管理者パスワードを設定する必要があります。 これを行うには、仮想マシンコンソールでF11キーを押します。



パスワードを作成すると、PowerShellを使用してNano Serverをリモートで制御できます。



PowerShellを使用してNano Serverのインスタンスにリモート接続します



TrustedHosts



者TrustedHosts



PowerShellウィンドウを開き、リモートNano ServerインスタンスをTrustedHosts



リストに追加します。



 $nanoServerIpAddress = "10.83.181.14" Set-Item WSMan:\localhost\Client\TrustedHosts "$nanoServerIpAddress" -Concatenate -Force
      
      





注:変数$nanoServerIpAddress



を使用するIPアドレスに置き換えます。



TrustedHosts



インスタンスをTrustedHosts



リストに追加したら、PowerShellを使用してリモート接続します。



 $nanoServerSession = New-PSSession -ComputerName $nanoServerIpAddress -Credential ~\Administrator Enter-PSSession $nanoServerSession
      
      





接続が成功すると、次の形式のコマンドラインが表示されます。



 [10.83.181.14]: PS C:\Users\Administrator\Documents>
      
      





共有ディレクトリを作成する



Nano Serverインスタンスに共有ディレクトリを作成して、公開されたアプリケーションをそこにコピーします。 リモートセッションで、次のコマンドを実行します。



 mkdir C:\PublishedApps\AspNetCoreSampleForNano netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes net share AspNetCoreSampleForNano=c:\PublishedApps\AspNetCoreSampleForNano /GRANT:EVERYONE`,FULL
      
      





これらのコマンドを実行した後、ホストコンピューターのエクスプローラーでアドレス\\<nanoserver-ip-address>\AspNetCoreSampleForNano



を入力して、共有ディレクトリを開くことができます。



ファイアウォールで開くポート



リモートセッションで次のコマンドを実行して、ファイアウォールのポートを開きます。



 New-NetFirewallRule -Name "AspNet5 IIS" -DisplayName "Allow HTTP on TCP/8000" -Protocol TCP -LocalPort 8000 -Action Allow -Enabled True
      
      





IISのインストール



PowerShellコレクションでNanoServerPackage



プロバイダーを選択して追加します。 プロバイダーをインストールしてインポートすると、Windowsパッケージをインストールできるようになります。



PowerShellで次のコマンドを実行します。



 Install-PackageProvider NanoServerPackage Import-PackageProvider NanoServerPackage Install-NanoServerPackage -Name Microsoft-NanoServer-Storage-Package Install-NanoServerPackage -Name Microsoft-NanoServer-IIS-Package
      
      





コンポーネント>Microsoft-NanoServer-Storage-Package



インストール後、再起動が必要です。 これは一時的な手順であり、今後は必要ありません。



IISが正しくインストールされているかどうかをすばやく確認するには、 http://<nanoserver-ip-address>/



ます-開始ページが表示されます。 IISをインストールすると、ポート80を使用するDefault Web Site



というDefault Web Site



サイトが作成されます。



ASP.NET Core Module(ANCM)のインストール



ASP.NET Coreモジュールは、ASP.NET Core HTTPリスナーを管理し、管理するプロセスに要求を転送するIIS 7.5+モジュールです。 現在、IIS用のASP.NET Coreモジュールのインストールは手動です。 通常のオペレーティングシステム(Nano Serverではない)を搭載したデバイスに.NET Core Windows Server Hostingパッケージをインストールする必要があります。 通常のオペレーティングシステムを搭載したコンピューターにパッケージをインストールした後、以前に作成した共有ディレクトリに次のファイルをコピーする必要があります。



通常のオペレーティングシステム(Nano Serverではない)を搭載したコンピューターで、次のコピーコマンドを実行します。



 copy C:\windows\system32\inetsrv\aspnetcore.dll ``\\<nanoserver-ip-address>\AspNetCoreSampleForNano`` copy C:\windows\system32\inetsrv\config\schema\aspnetcore_schema.xml ``\\<nanoserver-ip-address>\AspNetCoreSampleForNano``
      
      





Nano Server仮想マシンで、前に作成した共有ディレクトリから適切な場所に次のファイルをコピーする必要があります。 次のコピーコマンドを実行します。



 copy C:\PublishedApps\AspNetCoreSampleForNano\aspnetcore.dll C:\windows\system32\inetsrv\ copy C:\PublishedApps\AspNetCoreSampleForNano\aspnetcore_schema.xml C:\windows\system32\inetsrv\config\schema\
      
      





リモートセッションで次のスクリプトを実行します。



 # Backup existing applicationHost.config copy C:\Windows\System32\inetsrv\config\applicationHost.config C:\Windows\System32\inetsrv\config\applicationHost_BeforeInstallingANCM.config Import-Module IISAdministration # Initialize variables $aspNetCoreHandlerFilePath="C:\windows\system32\inetsrv\aspnetcore.dll" Reset-IISServerManager -confirm:$false $sm = Get-IISServerManager # Add AppSettings section $sm.GetApplicationHostConfiguration().RootSectionGroup.Sections.Add("appSettings") # Set Allow for handlers section $appHostconfig = $sm.GetApplicationHostConfiguration() $section = $appHostconfig.GetSection("system.webServer/handlers") $section.OverrideMode="Allow" # Add aspNetCore section to system.webServer $sectionaspNetCore = $appHostConfig.RootSectionGroup.SectionGroups["system.webServer"].Sections.Add("aspNetCore") $sectionaspNetCore.OverrideModeDefault = "Allow" $sm.CommitChanges() # Configure globalModule Reset-IISServerManager -confirm:$false $globalModules = Get-IISConfigSection "system.webServer/globalModules" | Get-IISConfigCollection New-IISConfigCollectionElement $globalModules -ConfigAttribute @{"name"="AspNetCoreModule";"image"=$aspNetCoreHandlerFilePath} # Configure module $modules = Get-IISConfigSection "system.webServer/modules" | Get-IISConfigCollection New-IISConfigCollectionElement $modules -ConfigAttribute @{"name"="AspNetCoreModule"} # Backup existing applicationHost.config copy C:\Windows\System32\inetsrv\config\applicationHost.config C:\Windows\System32\inetsrv\config\applicationHost_AfterInstallingANCM.config
      
      





注:前の手順を完了した後、共有ディレクトリからaspnetcore.dll



およびaspnetcore_schema.xml



削除しaspnetcore_schema.xml



。



.NET Core Frameworkをインストールする



ポータブルアプリケーションを公開した場合は、.NET Coreプラットフォームをターゲットコンピューターにインストールする必要があります。 リモートPowershellセッションで次のスクリプトを実行して、Nano Server仮想マシンに.NET Frameworkをインストールします。



 $SourcePath = "https://go.microsoft.com/fwlink/?LinkID=809115" $DestinationPath = "C:\dotnet" $EditionId = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name 'EditionID').EditionId if (($EditionId -eq "ServerStandardNano") -or ($EditionId -eq "ServerDataCenterNano") -or ($EditionId -eq "NanoServer") -or ($EditionId -eq "ServerTuva")) { $TempPath = [System.IO.Path]::GetTempFileName() if (($SourcePath -as [System.URI]).AbsoluteURI -ne $null) { $handler = New-Object System.Net.Http.HttpClientHandler $client = New-Object System.Net.Http.HttpClient($handler) $client.Timeout = New-Object System.TimeSpan(0, 30, 0) $cancelTokenSource = [System.Threading.CancellationTokenSource]::new() $responseMsg = $client.GetAsync([System.Uri]::new($SourcePath), $cancelTokenSource.Token) $responseMsg.Wait() if (!$responseMsg.IsCanceled) { $response = $responseMsg.Result if ($response.IsSuccessStatusCode) { $downloadedFileStream = [System.IO.FileStream]::new($TempPath, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write) $copyStreamOp = $response.Content.CopyToAsync($downloadedFileStream) $copyStreamOp.Wait() $downloadedFileStream.Close() if ($copyStreamOp.Exception -ne $null) { throw $copyStreamOp.Exception } } } } else { throw "Cannot copy from $SourcePath" } [System.IO.Compression.ZipFile]::ExtractToDirectory($TempPath, $DestinationPath) Remove-Item $Temp</code> Path }
      
      





アプリケーションを公開



公開アプリケーションを共有ディレクトリにコピーします。 dotnet.exe



ファイルがdotnet.exe



ディレクトリを示すために、 web.config



を変更する必要がある場合があります。 別の方法は、 dotnet.exe



ファイルを同じディレクトリにコピーするdotnet.exe



です。



dotnet.exe



ファイルdotnet.exe



同じディレクトリにコピーされない状況でのweb.config



の例:



 <?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" /> </handlers> <aspNetCore processPath="C:\dotnet\dotnet.exe" arguments=".\AspNetCoreSampleForNano.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="true" /> </system.webServer> </configuration>
      
      





リモートセッションで次のコマンドを実行して、公開アプリケーション用にIISで新しいWebサイトを作成します。 このスクリプトでは、簡単にするためにDefaultAppPool



を使用しています。 アプリケーションプールの操作の詳細については、 アプリケーションプールの記事を参照してください。



 Import-module IISAdministration New-IISSite -Name "AspNetCore" -PhysicalPath c:\PublishedApps\AspNetCoreSampleForNano -BindingInformation "*:8000:"
      
      





Nano Serverの.NET Core CLIの既知の問題とその回避方法



Nano Server Technical Preview 5を.NET Core CLIで使用している場合、DLLファイルをc:\windows\system32\forwarders



ディレクトリからc:\Program Files\dotnet\shared\Microsoft.NETCore.App\1.0.0\



ディレクトリにコピーする必要があります。 .NET Core c:\dotnet



バイナリディレクトリ.NET Core c:\dotnet



(この例では)。 これは、新しいバージョンで修正されたバグが原因です。



dotnet publish



コマンドを使用する場合、DLLファイルもc:\windows\system32\forwarders



ディレクトリから公開ディレクトリにコピーします。



Nano Server Technical Preview 5システムが更新または変更されている場合、DLLファイルも更新される可能性があるため、この手順を繰り返します。



アプリケーションの起動



公開されたWebアプリケーションは、 http://<nanoserver-ip-address>:8000



ブラウザーで使用できる必要があります。 「ログの作成と C:\PublishedApps\AspNetCoreSampleForNano\logs



」セクションで説明されているようにログが構成されている場合、すべてのログはC:\PublishedApps\AspNetCoreSampleForNano\logs



利用できます。



この記事では、Windows Server Technical Preview 5で利用可能なNano Serverインストールオプションの予備リリースを使用します。仮想ハードディスクイメージ上のソフトウェアは、内部デモおよびテスト目的にのみ使用できます。 このソフトウェアは、実稼働環境での使用を目的としていません。 試用版の有効期限はこちらで確認できます 。



All Articles