producing all included exe for c# .net 4.7.2 project - c#

I have a class library project that is referencing .Net Framework 4.7.2 . I need to create an exe which will contain all the references of the project. I will not have access to any code or project references on the VM, where this exe needs to be run. Everything should be included in this one exe.
I have come across this question on SO.
Are solutions such as costura.Fody and ILMerge mentioned in that question addresses the same issue that I have?
( I am new to .Net and C#) and I was in doubt because I am not sure of managed assemblies vs. any other as mentioned in one of the answer in question that I referenced
Can someone help in clearing this or suggesting any other solution, if there is any.

Costura.fody worked pretty much out of the box for .net 4.7.2 framework. When executable was compiled it included everything. One way to know is that the size of your executable will increase . The other way is to use tool like ILSpy to look inside executable

Related

Why would my Win Forms application not include the necessary .dll's for an imported class library? [duplicate]

After consuming .net standard projects in a .net framework(4.6) console application, the dependencies of the .net standard projects are not copied into the output directory. This results in run time error of missing dlls. The "copy local" property is already true for the referenced projects. One possible solution is to add all the dependencies again in the console application, but is not a good idea. Is there a better solution to this?
After going through an article by Scott Hanselman, below solution worked like a charm.
"Using .NET Standard requires you to use PackageReference to eliminate the pain of “lots of packages” as well as properly handle transitive dependencies. While you may be able to use .NET Standard without PackageReference, I wouldn’t recommend it."
Add below line in the first "PropertyGroup" tag of the .net framework console application's ".csproj" file
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
There will not be any need to add again the .net standard projects' nuget dependencies in the console application.
Another answer is to publish the project:
dotnet publish -o path/to/output/folder

How create a .net standard and .net framework self-contained build?

My goal is to create a build output that acts as portable version of my application, containing the non-framework dependencies (nuget + projects) directly as dlls.
This demonstrates the solution and the four projects contained:
You can see the libraries are .net standard 2.0 while a console app (to run / debug some code) is using .net framework 4.7.2 (because we have some .net framework apps in use around here). The arrows in the image show the references that have been set.
Parts of the the libraries (not console app) might be imported into MS sql server in the future; these imports usually pick up all dependencies from the same folder if available (which is the reason for a self-contained output).
Problem A: Running the code on linux/mono
Using jetbrains rider to open the solution, build + restore packages will work without problems.
Running the console app will work to some extend (e.g. loading data from SQL) until it fails when calling some code that makes use of BouncyCastle (dll not found exception).
Looking at the build output shows three .dll files of my lib-projects along with the .exe file of the console-app plus all the .pdb files AND additionally the System.Data.SqlClient.dll.
Seems that might be the reason my sql code worked.
Problem B: Running the code on win10, .net framework / core installed
Using VS / rider made no difference here, opened the solution, restored packages + build without problems. Running the console-app fails earlier than before: this time it was unable to find the Syste.Data.SqlClient.dll.
Checking the build folder shows my three lib-dlls and the .exe including the .pdb files, nothing else.
To my understanding, the files have to be either in GAC or inside the same folder for them to be found. It seems when a .net standard library dependency includes nuget-packages, something is having troubling either loading them into GAC or at least copying them to the build directory (which is outdated I guess).
So even if my approach (having a portable-ish / self-contained version of my app) might be uncommon or even stupid, I would have thought that just running this code on the dev-machine should work fine.
Problem C: Including dependencies in the build output
According to information I found here, the <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> property can be used to have dependencies being copied to the build directory.
And while this seems to copy too many files (tried excluding some according to docs) it does indeed copy the .dll files for a complete package when building the ExchangeIntegration project (.net standard 2.0).
Looking into accomplishing the same for .net framework .csproj projects yielded mixed results. I was playing around with the copy-local setting and stumbled across similar problems when creating nuget packages and using .targets files but had no success.
About the specific libraries used
I'm more interested in learning the right concept to handle this, it's not really about whether or not it makes sense to import Newtonsoft.Json into MS sql server.
I feel I'm fundamentally missing something; maybe the output type library is not correct for my goal or maybe I'm mixing problematic .net versions, although I did a quick check and it seems ok.
How can I get a .net framework (console-app) build that contains all non-framework dependencies (e.g. nuget) AND an separate .net standard (library) build, that also contains all non-framework dependencies?
EDIT: Adding all nuget packages to the top tier project (console-app in my example) seems to work around the problem, code executes without problems. I'm still waiting for a proper solution.
EDIT2: Added mono/msbuild version used under arch: 16.4

.net framework (WPF) dependencies of references

I have a .NET framework WPF project that references a .net standard library that I wrote. This library references io.ports.dll
I found that there is a problem with .net framework referencing .net standard projects and that it would not copy the io.ports.dll to the output folder. The solution here was to add the dependency to my WPF project also which solved the problem.
However I noticed that the two projects reference the io.ports.dll from different locations.
The WPF project references from
project location\packages\System.IO.Ports.4.5.0\lib\net461\System.IO.Ports.dll
The .net standard library io.ports.dll location is
C:\users\.nuget\packages\system.io.ports\4.5.0
The DLLs have different sizes, although they are the same version.
The problem appears when I use advanced installer to create a .msi.
It gets very confused and tries to copy both DLLs and in the end my .net standard library tries to read the wrong DLL.
Anyone have any idea how to solve this?
Pavel answered (now deleted) that it was due to my GUI project using the old packages.json. This solved the original problem. Another problem was that the io.ports dll has a dependency on Microsoft.Win32.Registry.dll which I also had to add to the GUI project.

Saxonica 9.7 HE in C#

I'm trying to get Saxon 9.7 HE up and running in C#, and failing miserably.
As per instructions, I am downloading and running https://sourceforge.net/projects/saxon/files/Saxon-HE/9.7/SaxonHE9-7-0-14N-setup.exe/download
This installs a bunch of files into C:\Program Files\Saxonica\SaxonHE9.7N.
In the bin folder inside this one, the following files are present:
No matter which DLLs I add as references to my project, the "using Saxon.Api;" reference fails to resolve.
Am I using the correct DLLs here? Is it some sort of .Net version mismatch? My class library project is set to .Net 3.5 currently.
Thanks for any help
The minimum supported version of .NET for Saxon is indeed 4.0.
The bug is in the documentation which will be updated shortly: See bug issue: https://saxonica.plan.io/issues/3085
Thanks for reporting the issue.
Some guess work here. But the dependency on .net 4 is most likley from the compiling of the C# wrapper over the saxon java library that was converted using IKVM. IKVM says it only requires .net 2.0, so I think the only place that dependancy can come from is the saxon9he-api.
You should be able to build this yourself, its quite thin.
https://dev.saxonica.com/repos/archive/opensource/latest9.7/hen/csource/api/Saxon.Api/
Otherwise if its a show stopper ask Michael Kay (the author), he's pretty active on stack overflow.
Updated
I was curious so I checked, the .Net 4 dependency seems to come from the saxon9he-api (which i'm fairly certain could be re-compiled under 3.5 with no/minimal changes), the same for the Query and Transform exe's.

How to use wixlibs as file references with support for different platforms?

I just am trying to replicate my distributed C# project structure to WIX setup projects. Now there is the following problem:
LIB: a C# library solution that builds AnyCPU .NET dlls from several C# projects
APP: the dlls from LIB are referenced (as file references) by this main application solution. Additionally there are platform dependent libs included in this application solution, therefore it is important to being able to create setups for the two specific target platforms x86 and x64.
Now I started to create a wixsetup project within the APP solution (which works fine). Then I proceeded with creating a wixlib within the LIB solution that references the LIB .NET dlls into the wixlib.
Now the problem:
The wixlib references the AnyCPU .NET dlls within a DirectoryRef which seems to be platform specificly tagged when creating the wixlib. Therefore I have to go back to the LIB solution, build the project with one platform target, copy the built files (via SVN externals mechanisms) to the APP solution, build this project with the exact same target platform as the wixlib was created with and repeat this procedure for creating the other platform.
It may seem that this is kind of complicated, but doable. Due to the fact that I omitted several other library solutions for which the same problem applies and the fact that all those libraries are used in multiple application solutions and - finally - everything has to run on our build server automatically as well, it is clear that this will not work.
I know of the following solution, though:
Double the .NET dll references within wixlib to assign them to different DirectoryRef INSTALLDIR and INSTALLDIR32 e. g. and to implement those different directory references in the wixsetup.
But this would complicate things as well and is not my preferred solution therefore - if there is an alternative.
If there is no smart alternative, just tell me and I will do things as described in the last paragraph.

Categories