How to export file with nuget? - c#

I have a nuget package that uses the Apose.PDF package which I have a license for. The license is put in a separate file called Aspose.Total.lic and is located in the same folder The folder structure is like this.
Project
-PDFReader.cs
-Aspose.Total.lic
The PDFReader.cs has the following code to read the license:
static PDFReader()
{
var license = new License();
license.SetLicense("Aspose.Total.lic");
}
And all this works fine locally. But when I export my code to a Nuget package and use the package from another program, I get exceptions that it cannot find "Aspose.Total.lic" Copying the contents of the file and putting it as argument for SetLicense does not work, it expects a file. Now the question is, how and maybe where do I export the file when packing a nuget package? This is my nuspec file (some code is abbreviated):
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/10/nuspec.xsd">
<metadata>
<id>XYZ</id>
<version>1.3.7</version>
<summary />
<dependencies>
<dependency id="Aspose.PDF" version="18.10.0" />
</dependencies>
<frameworkAssemblies>
<frameworkAssembly assemblyName="System" />
<frameworkAssembly assemblyName="System.Data" />
</frameworkAssemblies>
</metadata>
<files>
<file src="XYZ\bin\Release\XYZ.dll" target="lib\net47\XYZ.dll" />
<file src="XYZ\Aspose.Total.lic" target="lib\Aspose.Total.lic" />
</files>
</package>
My guess here is the the target location is wrong for the file.

NuGet exposes 3 folder automatically, lib for dlls, tools for powershell scripts and content for other content. (I think - it's been a while)
Try changing :
<files>
<file src="XYZ\bin\Release\XYZ.dll" target="lib\net47\XYZ.dll" />
<file src="XYZ\Aspose.Total.lic" target="lib\Aspose.Total.lic" />
</files>
To :
<files>
<file src="XYZ\bin\Release\XYZ.dll" target="lib\net47\XYZ.dll" />
<file src="XYZ\Aspose.Total.lic" target="content\Aspose.Total.lic" />
</files>

I think you should package that file as content. You can check the documentation how to achive that.

Related

Create Nuget for net461 with content copy to output

I've created a nuget package targeting a .NET Core project.
The idea here is to add xxx.dll as a reference with copy local as false
and copy all DLLs in a subfolder in the output path and all resources files in subfolder\resources
The nuspec targeting NET Core 3 is working fine.
Now I want to do the same to target NET Framework 4.6.1.
But the content files are not added to the project.
This is my nuspec file :
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>xxx.Win.x64.ForIPS11</id>
<version>15.5.3.4</version>
<title>xxx toolkit</title>
<authors>xxx Team</authors>
<owners>xxx</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>xxx 15.5.3.4 x64 Windows to .NETFramework 4.6.1 for IPS 11</description>
<releaseNotes></releaseNotes>
<copyright>2020</copyright>
<tags></tags>
<dependencies>
<group targetFramework=".NETFramework4.6.1" />
</dependencies>
<contentFiles>
<files include="**\subfolder\resources\*.*" buildAction="Content" copyToOutput="true" />
<files include="**\subfolder\*.dll" buildAction="Content" copyToOutput="true" />
</contentFiles>
</metadata>
<files>
<file src="bin\xxx.dll" target="lib\net461" />
<file src="resources\*.*" target="contentFiles\any\any\subfolder\resources" />
<file src="bin\*.dll" target="contentFiles\any\any\subfolder" />
</files>
</package>
Did I use an incompatible tag for .NET Framework target?
Any idea how to get this done?
EDIT : I use packages.config file in VS2017 in the target project
contentFiles is not compatible with packages.config way
This is an alternative :
Nuspec file :
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>xxx.Win.x64.ForIPS11</id>
<version>0.0.0</version>
<title>xxx toolkit</title>
<authors>xxx Team</authors>
<owners>xxx</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>xxx 0.0.0 x64 Windows to .NETFramework 4.6.1</description>
<releaseNotes></releaseNotes>
<copyright>2020</copyright>
<dependencies>
<group targetFramework=".NETFramework4.6.1" />
</dependencies>
<tags></tags>
</metadata>
<files>
<file src="bin\xxx.dll" target="lib\net461" />
<file src="resources\*.*" target="build\subfolder\resources" />
<file src="bin\*.dll" target="build\subfolder" />
<file src="xxx.Win.x64.targets" target="build" />
<file src="install.ps1" target="tools" />
</files>
</package>
xxx.Win.x64.targets to copy subfolder to output
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<NativeLibs Include="$(MSBuildThisFileDirectory)**\*.*" />
<None Include="#(NativeLibs)">
<Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
and install.ps1 to mark copy local false to the reference
param($installPath, $toolsPath, $package, $project)
$asms = $package.AssemblyReferences | %{$_.Name}
foreach ($reference in $project.Object.References)
{
if ($asms -contains $reference.Name + ".dll")
{
$reference.CopyLocal = $false;
}
}

WiX installer includes dll into msi

I'm trying to create an installer using WiX. To includes DLLs into .msi package I tryied two different ways. One of these is:
<DirectoryRef Id="SETTINGSDIR">
<Component Id="CMP_CopySettings" Guid="AC7D1AA1-798B-48F5-AF8D-188B1050D47C" KeyPath="yes">
<CreateFolder />
<File Id="DBA.bat" Source="$(var.SolutionDir)\scr\A\A.WindowsService\bin\$(var.Configuration)\Settings\A_DB clear.bat" Checksum="yes"/>
<File Id="AConfiguration.xml" Source="$(var.SolutionDir)\scr\A\A.WindowsService\bin\$(var.Configuration)\Settings\AConfiguration.xml" Checksum="yes"/>
<File Id="ADB.CE.DEFAULT.sdf" Source="$(var.SolutionDir)\scr\A\A.WindowsService\bin\$(var.Configuration)\Settings\ADB.CE.DEFAULT.sdf" Checksum="yes"/>
<File Id="ADB.CE.sdf" Source="$(var.SolutionDir)\scr\A\A.WindowsService\bin\$(var.Configuration)\Settings\A.CE.sdf" Checksum="yes"/>
<RemoveFile Id="RemoveFileSettings" Name="*" On="uninstall"/>
</Component>
</DirectoryRef>
But as you can easily understand, it's very hard write an xml node for each DLL (6 projects with 200+ DLLs for each one).
The second one is faster, but WiX just creates a link to the folder instead of copy DLLs into msi package
<DirectoryRef Id="SETTINGSDIR">
<Component Id="CMP_CopySettings" Guid="AC7D1AA1-798B-48F5-AF8D-188B1050D47C" KeyPath="yes">
<CreateFolder />
<CopyFile Id="SettingsID" SourceProperty="SETTINGSSOURCEDIRECTORY" DestinationDirectory="SETTINGSDIR" SourceName="*" />
<RemoveFile Id="RemoveFileSettings" Name="*" On="uninstall"/>
</Component>
</DirectoryRef>
Is there a quick solution that can I add at my second way or I have to use heat.exe tool? In this case, can you explain me how to use it? The official documentation is very poor
Thanks
What you want is an harvest tool to do this for you. Luckily it already exists: Heat
In your specific case you might want to use the command heat dir ".\My Files" -gg -g1 -directoryid "YourDirectoryId" -sfrag -template:fragment -out directory.wxs but check what is exactly your need, which harvesting you want to skip etc...
Note the -t <xsl> switch which gives you the total control on how you want to tune the final output.

Nuspec for NuGet package with UWP project

I wont to package one of my DLL in a NuGet package.
This DLL uses Xamarin Forms and will target Android, iOS and UWP (Universal Windows) projects
Here is the section of nuspec file:
<files>
<!--Core-->
<file src="Polux\CBS_CBT.Polux\bin\Release\CBS_CBT.Polux.dll" target="lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\CBS_CBT.Polux.dll" />
<file src="Polux\CBS_CBT.Polux\bin\Release\CBS_CBT.Polux.pdb" target="lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\CBS_CBT.Polux.pdb" />
<file src="Polux\CBS_CBT.Polux\bin\Release\CBS_CBT.Polux.xml" target="lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\CBS_CBT.Polux.xml" />
<!--Xamarin.Android-->
<file src="Polux\CBS_CBT.Polux.Droid\bin\Release\CBS_CBT.Polux.dll" target="lib\MonoAndroid10\CBS_CBT.Polux.dll" />
<file src="Polux\CBS_CBT.Polux.Droid\bin\Release\CBS_CBT.Polux.pdb" target="lib\MonoAndroid10\CBS_CBT.Polux.pdb" />
<file src="Polux\CBS_CBT.Polux.Droid\bin\Release\CBS_CBT.Polux.xml" target="lib\MonoAndroid10\CBS_CBT.Polux.xml" />
<file src="Polux\CBS_CBT.Polux.Droid\bin\Release\CBS_CBT.Polux.Droid.dll" target="lib\MonoAndroid10\CBS_CBT.Polux.Droid.dll" />
<file src="Polux\CBS_CBT.Polux.Droid\bin\Release\CBS_CBT.Polux.Droid.pdb" target="lib\MonoAndroid10\CBS_CBT.Polux.Droid.pdb" />
<file src="Polux\CBS_CBT.Polux.Droid\bin\Release\CBS_CBT.Polux.Droid.xml" target="lib\MonoAndroid10\CBS_CBT.Polux.Droid.xml" />
<!--Xamarin.iOS-->
<file src="Polux\CBS_CBT.Polux.iOS\bin\iPhone\Release\CBS_CBT.Polux.dll" target="lib\Xamarin.iOS10\CBS_CBT.Polux.dll" />
<file src="Polux\CBS_CBT.Polux.iOS\bin\iPhone\Release\CBS_CBT.Polux.pdb" target="lib\Xamarin.iOS10\CBS_CBT.Polux.pdb" />
<file src="Polux\CBS_CBT.Polux.iOS\bin\iPhone\Release\CBS_CBT.Polux.xml" target="lib\Xamarin.iOS10\CBS_CBT.Polux.xml" />
<file src="Polux\CBS_CBT.Polux.iOS\bin\iPhone\Release\CBS_CBT.Polux.iOS.dll" target="lib\Xamarin.iOS10\CBS_CBT.Polux.iOS.dll" />
<file src="Polux\CBS_CBT.Polux.iOS\bin\iPhone\Release\CBS_CBT.Polux.iOS.xml" target="lib\Xamarin.iOS10\CBS_CBT.Polux.iOS.xml" />
<!--uap-->
<file src="Polux\CBS_CBT.Polux.UWP\bin\Release\CBS_CBT.Polux.dll" target="lib\UAP10\CBS_CBT.Polux.dll" />
<file src="Polux\CBS_CBT.Polux.UWP\bin\Release\CBS_CBT.Polux.pdb" target="lib\UAP10\CBS_CBT.Polux.pri" />
<file src="Polux\CBS_CBT.Polux.UWP\bin\Release\CBS_CBT.Polux.xml" target="lib\UAP10\CBS_CBT.Polux.xml" />
<file src="Polux\CBS_CBT.Polux.UWP\bin\Release\CBS_CBT.Polux.UWP.dll" target="lib\UAP10\CBS_CBT.Polux.UWP.dll" />
<file src="Polux\CBS_CBT.Polux.UWP\bin\Release\CBS_CBT.Polux.UWP.pdb" target="lib\UAP10\CBS_CBT.Polux.UWP.pdb" />
<file src="Polux\CBS_CBT.Polux.UWP\bin\Release\CBS_CBT.Polux.UWP.pri" target="lib\UAP10\CBS_CBT.Polux.UWP.pri" />
<file src="Polux\CBS_CBT.Polux.UWP\bin\Release\CBS_CBT.Polux.UWP.xml" target="lib\UAP10\CBS_CBT.Polux.UWP.xml" />
The problem:
When I run the application, only the CBS_CBT.Polux.UWP.dll is copied in the \Debug directory, none of the other files (mainly CBS_CBT.Polx.dll) are copied in the output (\Debug for example) directory. The software then throw:
An exception of type 'System.IO.FileNotFoundException' occurred in
mscorlib.ni.dll but was not handled in user code
Additional information: Could not load file or assembly
'CBS_CBT.Polux, Version=0.3.0.0, Culture=neutral, PublicKeyToken=null'
or one of its dependencies.
When I manually add only "CBS_CBT.Polux.dll" and "CBS_CBT.Polux.UWP.dll" to the references of the UWP project, the software can run without any issue.
Thank you Unni Ravindranathan, after reading the doc, I could correct my code.
The only section I had to change was the "Core" one.
Here is the woring nuspec code:
<!--Core-->
<file src="Polux\CBS_CBT.Polux\bin\Release\CBS_CBT.Polux.dll" target="lib\portable-net45+win8+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\CBS_CBT.Polux.dll" />
<file src="Polux\CBS_CBT.Polux\bin\Release\CBS_CBT.Polux.pdb" target="lib\portable-net45+win8+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\CBS_CBT.Polux.pdb" />
<file src="Polux\CBS_CBT.Polux\bin\Release\CBS_CBT.Polux.xml" target="lib\portable-net45+win8+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\CBS_CBT.Polux.xml" />

Nuget nuspec not working with wildcard

I'm building a nuget package and all is well until I try to use the wildcard to import all dll's in a folder.
This works perfectly
<file src="KL.Ocr.Tesseract/x86/liblept172.dll" target="content\x86"/>
however this does nothing
<file src="KL.Ocr.Tesseract/x86/*.dll" target="content\x86"/>
Any help would be appreciated (and I am intentionally placing the dll's in content instead of lib because thats where they need to be)
<?xml version="1.0"?>
<package >
<metadata>
<id>asdf</id>
<version>1.0.0.0</version>
<title>asdf</title>
<authors>asdf</authors>
<owners>asdf</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>asdf </description>
<copyright>Copyright 2016</copyright>
<tags>asdf</tags>
<frameworkAssemblies>
</frameworkAssemblies>
<dependencies>
</dependencies>
</metadata>
<files>
<file src="KL.Pas.Ocr.targets" target="build"/>
<file src="KL.Ocr.Pas.Host/bin/Debug/KL.Ocr.Pas.Host.dll" target="lib/net45" />
<file src="KL.Ocr.Pas.Host/bin/Debug/KL.Ocr.Pas.Worker.exe" target="lib/net45" />
<file src="KL.Ocr.Pas.Host/bin/Debug/KL.Pas.Ocr.Contracts.dll" target="lib/net45" />
<file src="KL.Ocr.Pas.Host/bin/Debug/KL.Ocr.Tesseract.dll" target="lib/net45" />
<file src="KL.Ocr.Pas.Host/bin/Debug/Tesseract.dll" target="lib/net45" />
<file src="KL.Ocr.Tesseract/x64/*.dll" target="build\x64"/>
<file src="KL.Ocr.Tesseract/x86/*.dll" target="build\x86"/>
<file src="KL.Ocr.Tesseract/tessdata/*.*" target="build\tessdata"/>
</files>
</package>
I am assuming you are generating the NuGet package on Windows.
If you switch to using backslash instead of forward slashes then it will work.
<file src="KL.Ocr.Tesseract\x64\*.dll" target="build\x64"/>
The above works fine. Using forward slashes does not seem to work.

Finding the difference between two XML files

I have built two XML files that map the content of a given folder:
<root>
<folder name="C:\a\b" permision="yes" folderCount="1">
<folders>
<folder name="C:\a\b\c" permision="yes" folderCount="1">
<folders>
<folder name="C:\a\b\c\e" permision="yes" folderCount="0">
<folders/>
<files>
<file name="401-1.htm"/>
<file name="401-2.htm"/>
<file name="401-3.htm"/>
</files>
</folder>
<folder name="C:\a\b\d" permision="yes" folderCount="0">
<folders/>
<files>
<file name="401-4.htm"/>
<file name="401-5.htm"/>
<file name="401-3.htm"/>
</files>
</folder>
</folders>
<files/>
</folder>
</root>
I'd like to know if there is a way to find the difference between the files.
(One file is the old state and the second is the new state, and it's only possible to add files and not remove them. It would be great to remove identical nodes from the new state so only the new files will be left).
I would use LINQ to XML like the project below:
Diff in XML files with LINQ:
http://www.codeproject.com/KB/linq/LinqDiff.aspx
If you'd like to do it in code, you could use Microsoft'ss XML Diff and Patch GUI Tool, and although there isn't a great deal of documentation, there is enough that you should be able to easily diff two XML files in code, in a fairly short space of time. I use it in a couple of projects as part of a series of unit tests which ensure that XML files are being generated correctly.
If you just want to view the differences between the two files, then you could just use any decent diff tool.

Categories

Resources