Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Retargeting .NET Code

Retargeting .NET Code

This item in japanese

Between its age and popularity, the .NET platform has spread to numerous platforms and devices since it was introduced.  Developers looking to migrate their project from one platform to another will often run into the roadblock of having to determine which APIs their code uses versus what is provided by the target platform they desire.  Microsoft has provided the .NET Portability Analyzer (NPA) an effort to make this migration process easier.

Microsoft's Taylor Southwick and Connie Yau were part of a Channel 9 discussion hosted by Seth Juarez that let Southwick and Yai talk about what the NPA can do and how developers can put it to use in their own projects.  NPA is designed to help developers cope with the issues caused by switching from one platform to another.  The tool is not restricted in the choices of source and target platforms, meaning that any combination of migrations can be used.  (For example, migrating from Windows Phone to Silverlight or from Xamarin.Android to .NET Core.)

When executed against your source project, the NPA will identify which APIs are being used and what may need to be changed to move to the new target platform.  The current list of supported platforms includes:

  • Every version of .NET framwork on the desktop
  • Silverlight
  • Xamarin
  • Mono 3.3
  • Windows 8
  • Windows Phone
  • .NET Core

Do note that while an offline analysis mode is available, by default information about the code base being analyzed is sent to Microsoft via web service.  This analysis produces a list of APIs which are fed to a web service (Portability Service) that will then determine what is being targeted and what APIs are in those targets.  From this analysis a report is produced that returns to the user with information on the “portability index”, which includes details on the percentage of the APIs that you are using that are not available in another platform.  It doesn't tell you the number of instances those APIs are used-- just whether it is used or not.  (The score is the same whether an API is used once or a 100 times.)

The reason that a web service is used as part of the process is two-fold.  Microsoft wants to provide an easy way for it to remain updated and also so Microsoft can learn what APIs are being used by customers.  This information will be used to help determine what APIs Microsoft should migrate next from the desktop to the ‘next’ version—in .NET Core, ASP.NET, etc.

For those with privacy concerns or development environments that do not have Internet access, an offline mode is also available and NPA can be recompiled to support this.  Microsoft has tried to reduce the information being sent to their servers over the web service.  As part of this streamlining process the tool attempts to determine what is a Microsoft API versus what is the customer’s so that only Microsoft API data is transmitted.  It is also possible to view what is being sent to Microsoft through a command line option.  In general the following information being sent to Microsoft:

  • APIs being used
  • Assemblies being referenced
  • Name of your code’s assemblies

The tool provides three different report formats:

  • HTML Report – general overview of how portable your library is
  • Excel spreadsheet – useful to provide filterable data of what is being used
  • JSON file format available – useful for incorporating this into a separate program tool for actionable items

The source code for NPA is available on GitHub as part of Microsoft’s open source presence.  Related to the command line tool, there is an extension to provide the capabilities of NPA from within Visual Studio.  It is available in the extension gallery and is available for both VS2013 and VS2015.

A benefit of using the tool from within Visual Studio is that in addition to the reports it also enables portability issues to be identified within your code as editor messages, so that each offending call can be followed to its exact location in your code.  Suggestions on how to correct the code are provided when possible, and navigating to the specific code instance is a time saver.  (Note that currently the Visual Studio extension is limited to HTML reports.)

Rate this Article