共用方式為


Nano Server Developer Experience and Tools

This blog post is now obsolete. Please go to the new blog post here: https://blogs.technet.com/b/nanoserver/archive/2016/04/27/developing-native-apps-on-nano-server.aspx

 

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 amazing Visual Studio Debugger team, the Nano Server developer experience includes full remote debugging from Visual Studio!

To automate some of the mundane tasks, we also included 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 RTM version of Visual Studio (the version released on July 29). 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 and Windows SDK 10…” 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 :)).

    Use the blog and downloadable PowerShell script to build your Nano Server VHD, 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 “Hello, Nano Server!” app.

  1. Create a new project and, under Visual C++, choose “NanoServerApplication”. 

  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.

  4. In Solution Explorer, double-click on NanoServerApplication.cpp. In _tmain, add printf(“Hello, 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         

 

Now that “Hello, Nano Server!” 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 not available on Nano Server yet, but this is the best practice, listed here for completeness)

  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@

Comments

  • Anonymous
    August 19, 2015
    Created my first app on Nano Server! Thanks Refaat.
  • Anonymous
    August 31, 2015
    @Aditi: Thanks for sharing! Please keep the feedback coming
  • Anonymous
    September 07, 2015
    Any idea when we'll get the C# project templates promised?
  • Anonymous
    September 16, 2015
    In the project properties for setting the target platform all I see is Windows 8.1, not 10. I'm running VS2015 Enterprise RTM. Any ideas what's wrong? I'm hoping to build a VC++ application compiled for OneCore.
  • Anonymous
    September 25, 2015
    The comment has been removed
  • Anonymous
    November 16, 2015
    Over the coming weeks, we’ll be publishing more on Windows Server 2016 and the key capabilities
  • Anonymous
    November 19, 2015
    Introduce Windows Server Apps (WSAs), based on APPX, to package and install an application on Nano Server.
  • Anonymous
    November 25, 2015
    Everything worked perfectly. Quick question: Is there any way to get console output in the debugger? I can get the exit code and see state of code no problem while debugging, but I'd like to see the output as well. Is this possible?

    Additional note, you really should add a step to the blog to automatically copy the files post-build up to the nano server. It's pretty simple to do, just add a post-build event. This is what mine looks like with my paths, servername, and credentials. You could also store the creds to disk in order to supress the cred popup on each build:

    C:WINDOWSSystem32WindowsPowerShellv1.0powershell.exe -noprofile -command copy -ToSession (new-pssession nano -credential (get-credential toenuffAdministrator)) 'C:usersTomeDocumentsVisual Studio 2015Projectsnanoworldx64Debugnanoworld.*' c:nano
  • Anonymous
    December 13, 2015
    We need an update for Visual Studio 2015 update 1. Because :

    PS C:tempMoexNanoSoftwareDebug> Install-NanoServerDebugger $s
    You have Visual Studio 2015 Update 1 or higher installed. The cmdlets you're using are obsolete. Please refer to the Nano Server blog for details.
  • Anonymous
    December 16, 2015
    @Igor: i'm working on it. Sorry for the inconvenience
  • Anonymous
    December 21, 2015
    Running the Nano Server test EXE from VS 2015 U1 yields no output via the Nano PS session.

    Any special procedure.

    Note: I am just running the Hello Nano project outlined above.

    Thanks,

    Rick
  • Anonymous
    December 23, 2015
    @Igor and @Rick: I just posted a new blog post which addresses Visual Studio 2015 Update 1 and above:http://blogs.technet.com/b/nanoserver/archive/2015/12/24/nano-server-developer-experience-visual-studio-2015-update-1-and-above.aspx
    Thank you for your patience
    ref@
  • Anonymous
    March 22, 2016
    I am not getting simple output of printf("Hellon"). exe file returns without printing anything. Any hint where I am going wrong?