UWP Class Library to Multi-Architecture NuGet Package
Here's an overview of the UWP AppServices class library, and creating a NuGet package for UWP class libraies.
Join the DZone community and get the full member experience.Join For Free
like i already announced in my last blog post about uwp appservices, i am going to show you how to create a nuget package for uwp class libraries. i am going to go through the whole process, and provide you the steps i found as easiest (it may be the case that there are other ways for the single steps, too). i am continuing with my appservices sample to show you all the steps i did.
the first step is to download the latest nuget.exe . this command line application will do all the work that is needed to create the package. once downloaded, let’s do some modifications to our project.
it is good practice to put all the nuget stuff into a folder in your project. that’s what we’re doing now, adding a folder named nuget and put the nuget.exe file in it (copy and paste it in file explorer). the next step we would do now is to open the package manager console in visual studio and call the nuget.exe with the parameter ‘spec’ to create a .nuspec file. the.xml formatted .nuspec file describes the structure of the nuget package.
because we need a multi-architecture package for our universal class library, i prefer another approach. i created a sample .nuspec file that already describes part of the structure that we need. after pasting the file in, change the file name to match “[projectname].nuspec”. to add the file to your project in visual studio, click on the ‘ show all files’ button on top of the solution explorer window. now you will see the previously added nuget folder and the .nuspec file. right click on the renamed .nuspec file and select ‘include in project’:
inside the .nuspec file
let’s have a look into the .nuspec file. the first part is the ‘metadata’ part, which describes the file’s properties:
<metadata> <id>$title$</id> <version>$version$</version> <title>simple leet appservice handler</title> <authors>$author$</authors> <owners>$owner$</owners> <requirelicenseacceptance>false</requirelicenseacceptance> <description>$description$</description> <copyright>copyright © 2016</copyright> <dependencies> </dependencies> </metadata>
just replace the $-enclosed variables with your data. the more interesting part in this case is the dependencies part. with the universal app platform, microsoft introduced the .netcore package that provides all the apis. it is very hard to maintain the dependencies manually. luckily, there is already a nuget package that helps us to automate this process: nuspec dependency generator .
just add the package to your project and compile it. that’s it, you have all the dependencies in your .nuspec file:
<dependencies> <group targetframework="uap10.0"> <dependency id="system.diagnostics.debug" version="4.0.10" /> <dependency id="system.runtime" version="4.0.20" /> <dependency id="system.runtime.windowsruntime" version="4.0.10" /> <dependency id="system.threading.tasks" version="4.0.10" /> </group> </dependencies>
now that we have the dependencies in place, we’re ready for the next step: creating the package file structure. as we want a multi-architecture package, we need to compile the project for every cpu architecture. this is done pretty simple, just select release mode and build the project for all architectures:
please note that ‘any cpu’ will not work for uwp libraries. to check if we have all files in place, just open the corresponding architectures’ folder in the bin folder of your project:
the next part is to add these folders to the .nuspec file within the ‘files’ tag:
<files> <file src="..\bin\arm\release\sampleappserviceconnector.dll" target="runtimes\win10-arm\lib\uap10.0" /> <file src="..\bin\arm\release\sampleappserviceconnector.pdb" target="runtimes\win10-arm\lib\uap10.0" /> <file src="..\bin\x64\release\sampleappserviceconnector.dll" target="runtimes\win10-x64\lib\uap10.0" /> <file src="..\bin\x64\release\sampleappserviceconnector.pdb" target="runtimes\win10-x64\lib\uap10.0" /> <file src="..\bin\x86\release\sampleappserviceconnector.dll" target="runtimes\win10-x86\lib\uap10.0" /> <file src="..\bin\x86\release\sampleappserviceconnector.pdb" target="runtimes\win10-x86\lib\uap10.0" /> </files>
noticed that we use the ‘runtimes’ folder with the corresponding architecture structure folders? this is how we need to set it up for multi architectural packages. including the .pdb files is always a good practice, even with the missing symbolication in uwp applications (at least for now. i was told from inside microsoft that they are working on this, but it seems to be a very complicated process for .net native compiled applications).
very important: the entry reference
this alone does not work, though. we need a reference that we can use as entry point for our package. to do this, we need to add an additional folder entry to our .nuspec file:
<file src="..\bin\release\sampleappserviceconnector.dll" target="ref\uap10.0" /> <file src="..\bin\release\sampleappserviceconnector.pri" target="ref\uap10.0" />
now that we have added this folder structure to the .nuspec file, the only thing we need to do is to manually mark one of the library .dlls as anycpu (yes, i tried to compile the class lib as anycpu without success). here is how to do it:
- copy the x86-.dll file into the release folder under the bin folder of your project
- copy the x86-.dll and .pri file into the release folder
- open the visual studio developer command prompt (type ‘dev’ into the start menu, it will appear there)
- type: corflags.exe /32bitreq- [path to release folder]\[dll-name].dll
the result should look like this:
if you have additional files like xaml files, you would need to add a new folder in the uap10.0 folder (with the same name that your project has). i’ll update this post and the sample once i have a matching sample at hand.
creating and testing the package!
now we are finally able to pack the nuget package. as we have the developer command prompt already open, lets change the running directory to match the nuget folder in our project. all we then need to do is to run the pack command of the nuget.exe that we already placed in there:
and that’s it. we have our nuget package in place. now let’s test our package, before we are going to upload it to nuget.org. all you need to do is to have a folder for your nuget packages on your machine. i made a folder called ‘testnuget’ on mine, and copied the package into it (which is the same as the nuget push command we’ll see later).
to add this folder as package source, open the ‘options’ menu in visual studio and select ‘package sources’ in the nuget package manager entry. hit the add symbol on top and add your folder:
now if you open the package manager and select your local folder as source, you will be able to install and test your package:
publishing the package to nuget.org (or your nuget server)
the final step is to publish the newly generated package to nuget.org or your own nuget server. as we still have the developer cmd opened, we simply use the following command:
nuget push [path to your package.dll] [nuget.org api key]
another alternative would be to use the nuget.org website to upload the package: https://www.nuget.org/packages/upload (the page is self explanatory).
that’s it, your nuget package is available for all you consider to use it. i also updated the sample of my last blog post on github to include these changes, if you want to play around.
some of you may go a different route for some steps. i have found this a good way that is also kind of memorable (for me). i would love to hear feedback on this and to discuss this, so feel free to leave me a comment.
like always, i hope this post is helpful for some of you. happy coding, everyone!
Published at DZone with permission of Marco Siccardi, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.