Developing Native Apps on Nano Server
Nano Server represents a great opportunity for applications to get the most of available resources and deliver customers an agile environment which supports a DevOps management model. In order for applications to do this, they need to remove references to components not delivered in Nano Server (this typically means APIs which bring in the GUI or 32 bit references).
To make developing and porting apps to Nano Server easy, we added “decorations” to the Windows header files to include the right APIs, and created a Visual Studio template which leverages this header decoration work, and automates the inclusion of the right headers and libraries. You can enjoy full IntelliSense and error squiggles support.
Thanks to the Visual Studio Debugger team, the Nano Server developer experience includes full remote debugging from Visual Studio!
To automate some of the mundane tasks, we’re also including a small PowerShell module to make it easy to install the debugger binaries on the Nano Server machine, as well as start & stop the debugger.
- On your development machine, you should be running the latest version of PowerShell, i.e. Windows 10, a preview release of Windows Server 2016, or the latest version of the Windows Management Framework.
- Make sure your development machine is running Hyper-V, if you’re going to run Nano Server in a VM. Otherwise, make sure that your development machine has network access to your Nano Server machine.
- Install the Update 2 version of Visual Studio 2015. Here’s a link to download the free community version of Visual Studio. Make sure to choose “Custom” install, and under “Universal Windows App Development Tools”, ensure that “Tools (1.2) and Windows 10 SDK” is checked.
- Download the zip file attached to this blog and unzip it:
- Double-click on NanoServerProjectExtensions.vsix and follow instructions to install it.
- Import the PowerShell module (NanoServerDebugger.psm1) by opening an elevated PowerShell or ISE session, use Import-Module and pass the full path to the .psm1 file. Don’t close the PowerShell session! If you do, you would have to import the module every time you open the PowerShell session (unless you use a PowerShell profile of course J).
- Follow the instructions in Getting Started with Nano Server to build your Nano Server VHD using New-NanoServerImage, and then use it to boot on a physical machine or in a VM.
Writing your First App
Now you’re ready to develop your first Nano Server app. Let’s write and debug a simple app.
- Create a new project and, under Visual C++, choose “NanoServerApplication”. Call your project “NanoApp1”.
- In the debug target drop down, select x64 as the target platform
- In the Project Properties dialog, under “General”, make sure that the “Target Platform Version” is 10 (typically 10 point something).
- In Solution Explorer, double-click on NanoServerApplication.cpp. In _tmain, add printf(“Hello from Nano Server!\n”); Press Ctrl+F5 to make sure everything works locally.
- From an elevated PowerShell or ISE console (import the PowerShell module again if you closed the previous console), create a session to the Nano Server machine or VM, using the following commands (for more information, consult the Nano Server Getting Started guide):
- $ip = “ip address to the Nano Server machine”
- Ensure that TrustedHosts is set to the Nano Server IP address. Otherwise: Set-Item wsman:\localhost\client\TrustedHosts “ip address to the Nano Server machine”
- $s = New-PSSession –ComputerName $ip –Credential ~\Administrator
- Install-NanoServerDebugger –session $s
- Copy the .exe and the .pdb files from the app you just wrote to Nano Server, using the same session object:
- Enter-PSSession $s
- Md c:\NanoApp1 # for example
- Copy-Item –ToSession $s –path “path to the .exe and .pdb files” –Destination c:\NanoApp1. Remember to do this every time you build a new .exe! (Alternatively, you can refer to the local pdb file, but make sure to change the project properties to reflect that).
- In the project properties dialog, under Configuration Properties \ Debugging:
- Change the “Debugger to launch” to: “Remote Windows Debugger”.
- Remote Command = C:\NanoApp1\NanoApp1.exe (full path to the Nano app executable)
- Working Directory = C:\NanoApp1 (path to the Nano app executable folder)
- Remote Server Name = IP address of the Nano Server machine
- Connection = “Remote with no authentication”
- In the open PowerShell console, start the debugger using Start-NanoServerDebugger –Session $s
- Now you’re ready to debug your app. Set a breakpoint and press F5 and you’re good to go! Use locals, watches … pretty much everything you’re used to using in a debugging session.
- To stop the debugger, use: Stop-NanoServerDebugger –Session $s
Note that the cmdlet you used (Install-NanoServerDebugger) also copied the Visual C++ runtime binaries to your Nano Server machine. If you want to run your app on another Nano Server machine, either:
- · Download the Visual C++ redistributable package from here, install it on your development machine, and copy the binaries over to Nano Server.
- · Include the required contents of the Visual C++ redistributable package that your app is dependent on as part of your app install.
Now that our simple app is working, try your hand at a WMI provider, such as this MI API sample (which also creates a PowerShell cmdlet along with the provider), or Querying a Service's Configuration.
At any rate, you’ll want to make sure that your application can participate in a DevOps management model. This entails two things:
- Ensure your application can be fully configured using PowerShell Desired State Configuration (DSC). Writing a DSC provider ensures that customers can choose the DevOps deployment tool of their choice and ensure that your application will “just work”. (Note that DSC is now available on Nano Server starting with Technical Preview 4)
- Ensure your application can be fully managed remotely via PowerShell. You can write PowerShell cmdlets in native code using a WMI V2 provider, in managed code using .NET Core, or in PowerShell itself.
Please use the Nano Server forum to ask questions. We look forward to your feedback.
Happy coding :)