共用方式為


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.

Prerequisites

  1. 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.
  2. 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.
  3. 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.
  4. Download the zip file attached to this blog and unzip it:
    1. Double-click on NanoServerProjectExtensions.vsix and follow instructions to install it.
    2. 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).
  5. 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.

  1. Create a new project and, under Visual C++, choose “NanoServerApplication”.  Call your project “NanoApp1”.
  2. In the debug target drop down, select x64 as the target platform
  3. In the Project Properties dialog, under “General”, make sure that the “Target Platform Version” is 10 (typically 10 point something).
  4. 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.
  5. 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):
    1. $ip = “ip address to the Nano Server machine”
    2. 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
    3. $s = New-PSSession –ComputerName $ip –Credential ~\Administrator
    4. Install-NanoServerDebugger –session $s
  6. Copy the .exe and the .pdb files from the app you just wrote to Nano Server, using the same session object:
    1. Enter-PSSession $s
    2. Md c:\NanoApp1   # for example
    3. Exit-PSSession
    4. 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).
  7. In the project properties dialog, under Configuration Properties \ Debugging:
    1. Change the “Debugger to launch” to: “Remote Windows Debugger”. 
    2. Remote Command = C:\NanoApp1\NanoApp1.exe (full path to the Nano app executable)
    3. Working Directory = C:\NanoApp1 (path to the Nano app executable folder)
    4. Remote Server Name = IP address of the Nano Server machine
    5. Connection = “Remote with no authentication”
  8. In the open PowerShell console, start the debugger using Start-NanoServerDebugger –Session $s
  9. 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.
  10. 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.

-or-

  • ·      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:

  1. 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)
  2. 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 :)

 

ref@

 NanoServerProjectExtensions_20160425

Comments

  • Anonymous
    April 28, 2016
    The NanoServerProjectExtensions_20160425.zip download link is broken.
    • Anonymous
      May 04, 2016
      @Dan, the move to the new WordPress blog format broke a few links. Please try again now.
  • Anonymous
    May 02, 2016
    c# support?
  • Anonymous
    August 03, 2016
    I built a NanoApp1(Release x64). On the Nano Server, it runs without error, but no result. Could you tell me why?Excute:[192.168.1.1]: PS C:\rc> .\NanoServerApplication1.exe[192.168.1.1]: PS C:\rc>
    • Anonymous
      August 10, 2016
      You may be missing the C runtime DLLs. Try copying them to Nano Server.
  • Anonymous
    September 02, 2016
    Hi Refaat,Thank you for the introduction, this is really helpful. Do you know of the possibility to set up a Makefile based Native App for Nano Server?Kind Regards,Simon