📜 ⬆️ ⬇️

ASP.NET Core on Nano Server

We present the first of five articles on working with ASP.NET Core: a guide for deploying an ASP.NET Core application to a Nano Server with IIS.



The first cycle of articles on ASP.NET Core


1. ASP.NET Core on Nano Server .
2. Creating the web service front end for the application .
3. Create the first web API using ASP.NET Core MVC and Visual Studio .
4. Deploy a web app in Azure App Service using Visual Studio .
5. Your first application on a Mac using Visual Studio Code .

Introduction


Nano Server is a Windows Server 2016 installation option that has a more compact size and more advanced maintenance and security features compared to Server Core and the full installation option. For more information, see the official documentation for the Nano Server installation option. There are three ways to get started with it:
')
1. Download the Windows Server 2016 Technical Preview 5 ISO file and create a Nano Server image.
2. Download Nano Server VHD for developers.
3. Create a virtual machine in Azure using a Nano Server image from the Azure Gallery. If you do not have an Azure account, you can create a free account with a 30-day trial period.

This material uses a previously created Nano Server VHD for developers from Windows Server Technical Preview 5.

Next, you will need to create and publish a 64-bit ASP.NET Core application.

Preparing an instance of Nano Server


Create a new Hyper-V virtual machine using the preloaded VHD file. Before logging in, you must set an administrator password. To do this, press the F11 key in the virtual machine console.

After creating the password, the Nano Server can be controlled remotely using PowerShell.

Remotely connect to an instance of Nano Server using PowerShell


Open an elevated PowerShell window to add a remote instance of the Nano Server to the TrustedHosts list.

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

Note: Replace the $nanoServerIpAddress variable with the IP address you are using.

After adding the Nano Server instance to the TrustedHosts list, remotely connect to it using PowerShell.

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

In case of successful connection, the following command line will appear:

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

Creating a shared directory


Create a shared folder in the Nano Server instance to copy the published application into it. In a remote session, run the following commands:

 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 

After these commands have been executed, you can open the shared directory by entering the address \\<nanoserver-ip-address>\AspNetCoreSampleForNano in the explorer on the host computer.

Opening a port in a firewall


Run the following commands in the remote session to open the port in the firewall:

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

Installing IIS


Add the NanoServerPackage provider by selecting it in the PowerShell collection. After installing and importing a supplier, you will be able to install Windows packages.

Run the following commands in PowerShell:

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

After installing the component >Microsoft-NanoServer-Storage-Package , a reboot is required. This is a temporary procedure and will not be needed in the future.

To quickly check if IIS services are installed correctly, go to http://<nanoserver-ip-address>/ - the start page should appear. When IIS is installed, a default website is created called Default Web Site , which uses port 80.

Installing ASP.NET Core Module (ANCM)


The ASP.NET Core module is the IIS 7.5+ module, which is responsible for managing the ASP.NET Core HTTP listeners and sending requests to the processes it controls. At the moment, the installation of the ASP.NET Core for IIS module is done manually. You will need to install the .NET Core Windows Server Hosting package on a device with a regular operating system (not a Nano Server). After installing the package on a computer with a normal operating system, you must copy the following files to the shared directory that was created earlier.

On a computer with a normal operating system (not Nano Server), run the following copy commands:

 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`` 

On the Nano Server virtual machine, you must copy the following files from the shared directory that was created earlier to the appropriate location. Run the following copy commands:

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

Run the following script in the remote session:

 # 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 

Note: remove the aspnetcore.dll and aspnetcore_schema.xml from the share directory after the previous step is completed.

Installing the .NET Core Framework


If you published a portable application, the .NET Core platform must be installed on the target computer. Run the following scripts in a remote Powershell session to install the .NET Framework on the Nano Server virtual machine:

 $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 } 

Application Publishing


Copy the published application to the shared directory. You may need to make changes to the web.config to indicate the directory in which the dotnet.exe file is dotnet.exe . Another way is to copy the dotnet.exe file to the same directory.

An example of a web.config in a situation where the dotnet.exe file dotnet.exe not copied to the same directory:

 <?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> 

Run the following commands in a remote session to create a new website in IIS for the published application. This script uses the DefaultAppPool to simplify. For more information on working with the application pool, see the Application Pools article.

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

Known issue with the .NET Core CLI on the Nano Server and how to work around it


If you are using Nano Server Technical Preview 5 c .NET Core CLI, you need to copy the DLL files from the c:\windows\system32\forwarders directory to the c:\Program Files\dotnet\shared\Microsoft.NETCore.App\1.0.0\ and directory to the .NET Core c:\dotnet binary .NET Core c:\dotnet (in this example). This is caused by a bug that is fixed in newer versions.

If you are using the dotnet publish command, also copy the DLL files from the c:\windows\system32\forwarders directory to the publishing directory.

If the Nano Server Technical Preview 5 system has been updated or modified, repeat this procedure, as the DLL files could also be updated.

Application launch


The published web application should be available in a browser at http://<nanoserver-ip-address>:8000 . If logging is configured as described in Creating and forwarding logs , all logs are available in the C:\PublishedApps\AspNetCoreSampleForNano\logs .

This material uses a preliminary release of the Nano Server installation option, which is available in the Windows Server Technical Preview 5. The software on the virtual hard disk image can only be used for internal demonstration and testing. This software is not intended for use in a production environment. The expiration date of the trial version can be found here .

Source: https://habr.com/ru/post/310996/


All Articles