简体   繁体   中英

Consume all source code (non-compiled) from one project into another (PDFSharp)

As the title says, I want to consume all of PDFSharp's source code into my own project. But let me explain why I came to this scenario, so if there is something else I can do, maybe there are other options.

Goal: Compile my project into a single.exe file to use. No installers. Problem: It uses PDFSharp.dll which is causing me issues.

What I am trying to do, is use ILMerge to create the.exe. I've used this successfully in the past for other projects.

The issue I think is that ILMerge is requiring references to other assemblies that PDFSharp uses. The first being Microsoft.ApplicationInsights. So to by-pass this, I installed Microsoft.ApplicationInsights into my project via Nuget. Then removed the actual reference from the project, but referenced the library in my ILMerge command as below:

/lib:"C:\<path to assembly>\Microsoft.ApplicationInsights.2.16.0\lib\net46"

This actually worked. Except, now it asked for another library and I get this error:

Unresolved assembly reference not allowed: GdPicture.NET.11.

This looks like a paid library, perhaps downloading the trial may get me past this. I didn't try yet. I switched gears as I felt I may be trying to reference an endless amount of assemblies.

I then tried to get the PDFSharp source code and I found that version 1.32 here: https://sourceforge.net/projects/pdfsharp/files/pdfsharp/PDFsharp%201.32/

I added a reference to this project within my solution file, so now I have a solution with 2 projects. Great.

I then I tried to link source files into my project. How to do that is here: https://jeremybytes.blogspot.com/2019/07/linking-files-in-visual-studio.html#:~:text=To%20link%20files%2C%20use%20the,CLICK%20THE%20%22Add%22%20BUTTON . This seems to work, but every file I add requires another file, which references another file etc. It seemed endless. So that led me to the idea of just consuming the entire source code into my project and I haven't seen a good way to do that yet. I can't add a reference to the project as it just references the compiled dll which again, iLMerge can't combine.

I've also tried updating the tag within the.csproj file of PDFSharp to "module" to create a .netmodule file. This creates the file in the obj directory but throws an error:

\PDFsharp\code\PdfSharp\obj\Release\PdfSharp.netmodule' is not an assembly

Any help is appreciated. thanks.

UPDATE: I reversed everything and added the PdfSharp reference - back to where I was and changed my project to module and built which created a .netmodule file. Then used the assembly linker to create a.exe from that file. That worked using this command from VS Dev prompt.

al MyModule.netmodule /target:exe /out:MyProgram.exe /main:MyNamespace.MyClass.Main

This created the.exe, but when run without any other supporting files produces a file not found error:

System.IO.FileNotFoundException: Could not load file or assembly 'MyModule.netmodule' or one of its dependencies. The system cannot find the file specified. 

Which is interesting since the module should be inside the exe right?

I have this working now, so I just wanted to place my results here since it is already posted.

My initial problem was that I mistakenly thought the PDFSharp.dll was causing the issue, but it was actually another group of 3rd Party dlls I was referencing.

I tried for hours to get iLMerge to work with the only success being it would kick out a single.exe file but it would have runtime errors.

Errors that I encountered:

Error: Unresolved assembly reference not allowed: Custom.Assembly.

Solution: Reference the assembly if possible. If you have many, you can reference a folder with the /lib:"C:\folderpath" switch.

Error: Unresolved assembly reference not allowed: ADotNetFramework.dll.

Solution: You can reference the desired.Net Framework path where iLMerge will search for missing references. Example: /targetplatform:"v4,C:<Path To Framework>.NETFramework\v4.8"

Error: The assembly 'xyz.dll' was not merged in correctly. It is still listed as an external reference in the target assembly.

Solution: You can get past this error with the /closed switch. However, I don't think I should even have gotten this error because 'xyz.dll' was a referenced dll to be combined.

Also - use the /log switch, it is extremely helpful in seeing exactly what iLMerge is doing and figuring out your issue. Example: /log:mylog.txt

This allowed me to see that iLMerge was finding duplicate namespaces, in the 3rd Party assemblies and automatically renaming them. Here is an example from my log:

Merging assembly 'My.Assembly.Name' into target assembly. Duplicate type name: modifying name of the type '<>f__AnonymousType0 2' (from assembly 'My.Assembly.Name') to 'My.Assembly.Name.<>f__AnonymousType0 2' Duplicate type name: modifying name of the type '<>f__AnonymousType1 2' (from assembly 'My.Assembly.Name') to 'My.Assembly.Name.<>f__AnonymousType1 2' Duplicate type name: modifying name of the type '' (from assembly 'My.Assembly.Name') to 'My.Assembly.Name.

Finally - the solution that I found was not to use iLMerge. I found this Answer: https://stackoverflow.com/a/40786196/2596309 which used Costura.Fody I installed the nuget package with:

Install-Package Costura.Fody -Version 4.1.0

Cleaned and built my solution and it created a single.exe file that I tested and it worked. Literally, I put 3 days of work into this and the solution took 3 minutes...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM