I try to embed the dll of a class library into my exe.
I use visual studio 2019 and .net 5.
I created two projects in one solution
one is class library (dll), and the second is console application
both targeted for .net core 5. I selected the console application as startup project.
the class library contain only public static hello function which print out hello.
I referenced the project of the class library into the console application then in the console application i only called the ClassNamespace.library.hello function.
when I compile it, it workes fine.
then I installed costura.fody as described in their readme, i added the to the console project by:
PM> Install-Package Fody
PM> Install-Package Costura.Fody
Then I FodyWeavers.xml into project folder
<Weavers>
<Costura/>
</Weavers>
After that i rebuilt the project, and it built, and the exe is running, but when I delete the .dll from the output directory the .exe isn't running.
This can be accomplished without any additional package.
Since NET 5 you have to set two options.
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<!-- To publish just a single *.exe file -->
<PublishSingleFile>true</PublishSingleFile>
<!-- Specify for which runtime you want to publish -->
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
<!-- Since NET 5 specify this if you want to also pack all external *.dll to your file -->
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<!-- Add trimming for a smaller file size if possible--->
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
With setting IncludeNativeLibrariesForSelfExtract to false
With setting IncludeNativeLibrariesForSelfExtract to true
Documentation for publish single file
Documentation for trimming
Related
I recently tried using VS Code instead of Visual Studio 2019 Community and just ran into my first problem. If you want to add a reference manually in VS 2019 you can simply right click on 'References' and click on 'Add Reference'. I am now using VS Code to work on my C# Project and my next Step requires the System.Drawing.Common DLL.
The problem is - There is no 'Reference' folder in VS Code, so I don't know what to do. I've installed the C# extension for VS Code but there is still no option to add install packages or import references manually.
Can anyone help me? I'm desperate.
If you use .NET Core, the easiest way is to use the .NET Core CLI alongside VS Code.
You can use the command dotnet add package System.Drawing.Common in the folder which contains your project and its .csproj file.
You can also manually edit the .csproj file and add the following block :
<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
</ItemGroup>
The package will be loaded when building or running your app from VS Code or the .NET Core CLI.
If you need to add standalone DLL, do it like this.
1- Modify the *.csproj-file and under </PropertyGroup> tag, add your reference like this.
<ItemGroup>
<Reference Include="DllrefrenceFile">
<HintPath>..\Dlls\DllrefrenceFile.dll</HintPath>
</Reference>
</ItemGroup>
2- Move the dll to the root of your project.
3- using console/terminal go to the root directory of your project and execute dotnet restore to import all the references.
4- execute dotnet run
If you need to add package to your project, you can install the NuGet Package Manager extension for vscode and after that install your packages to your project like this.
Press Ctrl+Shift+P
Then select "Nuget Package Manager:Add Package"
Enter package name Ex: EntityFramework
select package name and version
I am upgrading an Asp.Net Website project to .net framework 4.8 from 4.7. After upgrading, the project builds successfully from visual studio and also running without any issue on local machine. When same project is published using MSBuild I am getting following error:
(AspNetMerge target) ->
aspnet_merge : error occurred: An error occurred when merging assemblies: Unresolved assembly reference not allowed: System.Net.Http.
MsBuild command used is:
msbuild.exe "D:\Enterprise\Enterprise.sln" /nologo /nr:false /t:Build /p:DeployOnBuild=true /p:Configuration=Release /p:PublishProfile=VSO
And VSO.pubxml file referenced here has following configuration:
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>FileSystem</WebPublishMethod>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish />
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<ExcludeApp_Data>False</ExcludeApp_Data>
<publishUrl>..\..\Publish\Web</publishUrl>
<DeleteExistingFiles>False</DeleteExistingFiles>
<PrecompileBeforePublish>True</PrecompileBeforePublish>
<EnableUpdateable>False</EnableUpdateable>
<DebugSymbols>False</DebugSymbols>
<WDPMergeOption>MergeAllOutputsToASingleAssembly</WDPMergeOption>
<UseMerge>True</UseMerge>
<SingleAssemblyName>MergedAssembly</SingleAssemblyName>
</PropertyGroup>
</Project>
You can see that <UseMerge> is set to true which will merge output to single assembly.
I tried by disabling merge altogether which fixed the issue, but I want to keep the merge option enabled as it was configured this way in the release pipeline earlier. Any help will be appreciated.
I have solved this issue by installing Microsoft.Aspnet.Merge NuGet package (dont forget to click preview checkbox).
System.Net.Http is a library that brings so much problems to plain developers. There are many unbelievable issues with it after NuGet packages update or after targeting new .NET Framework.
Unfortunately there is no silver bullet for resolve all these issues. It depends on many factors what really caused this error.
What is most likely cause such errors
Most likely this is config file's <dependencyAssemblies> section. After project .NET Framework retargeting there are NuGet packages that can't be resolved using old versions of dependency assemblies.
What you can try
Use following workflow to retarget your projects on new .NET Framework version:
Change .NET Framework version in project/solution
Update NuGet packages (in Package Manager Console: update-package)
Reinstall NuGet packages (in Package Manager Console: update-package -reinstall)
Rebuild project/solution
Check Warnings window for some records like Found conflicts between different versions of the same dependent assembly. Double click on these records and press Yes on each popup window.
Rebuild project/solution again
Now your project correctly retargeted on new .NET Framework version with all dependencies update. And error must dissapear.
I am working inside of a WPF application that I created using a preview version of VS 2019. This WPF app is running .net 3.0 and everything is fine and such. I can debug the application and everything works all nice. I publish it using the Publish util inside VS and I have the settings as such:
Release
netcoreapp3.0
Self-Contained
win-x64
I then added this to my .csproj file
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UseWPF>true</UseWPF>
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>
This will compile and even builds a single executable which is awesome. The problem is when I click to run this new executable nothing happens, no errors, no windows, it just fails to launch.
EDIT:
I compiled it to a regular console window executable and I can now see an error. I have files that need to be copied into the directory like appsettings.json files. They are marked to be copied but because of the single file settings they aren't or are somehow being bundled with the exe.
I have two .Net projects:
One project is is a .Net standard project containing some DTOs; call it the "core library". This is intended to be published as a NuGet package
The other is a .Net framework project that includes the NuGet package of the core library. Call it "the app"
Using TFS we are able to compile the core library, publish the resulting NuGet library and then launch the app's build which restores the NuGet reference to the core library. Everything works as expected.
The problem is that when developing the core library + the app we have frequent compiles of the core and cannot wait for the entire core library build to finish in order to update the app's local reference. Moreover we must push complete commits of the core library (so we cannot push one DTO at the time before testing them in the app).
So I was wondering if exists a way in Visual Studio and Nuget to publish/install the core's Nuget library in the local user's .nuget folder in order to let it be visible automatically to the app's project on the next build.
Can you help me with this?
Starting from the answer of Lance Li-MSFT I made some changes for my needs. In fact, locally I do not have nuget.exe installed but only the .Net Core runtime, so I'm using the dotnet pack command instead of nuget.exe pack and dotnet nuget push instead of nuget.exe push. Moreover before pushing locally the updated NuGet package I delete the old package with the same version, else dotnet nuget push will not overwrite the package.
I accept Lance Li-MSFT's answer as it was the starting point for my solution and works for who have nuget.exe locally.
My solution is below
corelib.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<Target Name="PublishNuGetLocally" AfterTargets="Build">
<Exec Command="dotnet pack $(ProjectPath)"/>
<Exec Command="dotnet nuget delete --source $(UserProfile)\.nuget\packages $(PackageId) $(PackageVersion) --non-interactive" ContinueOnError="WarnAndContinue"/>
<Exec Command="dotnet nuget push --source $(UserProfile)\.nuget\packages $(ProjectDir)bin\$(ConfigurationName)\$(PackageId).$(PackageVersion).nupkg" />
</Target>
[...]
</Project>
As a side note, I did not have to configure the package source in Visual Studio because the global Nuget cache (at the path %UserProfile%\.nuget\packages) is automatically used as the first place where to search for the NuGet packages.
So I was wondering if exists a way in Visual Studio and Nuget to
publish/install the core's Nuget library in the local user's .nuget
folder in order to let it be visible.
Before publishing nuget package(nuget push) to one location locally, we should add the path as package source. So we need to set the path of local packages as package source firstly. The content in nuget.config file is corresponding to the Package Source UI in VS, so you have two ways to do it.
1.In VS,go Tools=>nuget package manager=>package manager settings=>package source, click the green button to define new package source.
2.Or we can find the nuget.config file for current user, see this document. The UI operation in #1 actually help define the source in the nuget.config file in C:\Users\xxx\AppData\Roaming\NuGet, see:
So we can directly edit this file to set our new package source, after that save the file and restart VS, we can see new defined source in UI.
My problem is to automatize the nuget push command to the local feed,
that in my case should be %userprofile%.nuget\packages. I cannot ask
any person in the company to manually copy the core package to the
local feed in order to see the changes in the linked app
After adding the local feed to package source, then you can use nuget push command to publish packages to the feed.
You can define a custom target similar to this in your core library project file(xx.csproj) to automatically pack and push the package automatically:
<Target Name="CustomTarget" AfterTargets="build">
<Exec Command="nuget.exe pack xx.csproj"/>
<Exec Command="nuget.exe push -source xxx path\xxx.nupkg"/>
</Target>
And you can also add Conditions to this target,<Target Name="CustomTarget" AfterTargets="build" Condition="$(Configuration)=='Debug'">. You can control in which configuration VS should run this target to pack and push for you.
In addition:
For .net core library projects, VS have a option to create. the nuget package. You can right-click the project in VS and select Pack button.So you can also define a .bat file for this project to do the nuget push. The process is build the project=>use pack option to easily get nuget package=>run the .bat to automatically push.
Hope it helps.
I have a net core consoleapp project, as follows (VS 2017 style):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
<Version>2.0.0</Version>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<FileVersion>4.0.0.0</FileVersion>
<RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
<RuntimeFrameworkVersion>1.0.4</RuntimeFrameworkVersion>
</PropertyGroup>
</Project>
I can build the project without any issues, I can publish it using dotnet publish -r win10-x64 and it will generate an exe file together with the dll file. My problem is that the exe file has some strange FileVersion and ProductVersion fields (in my case FileVersion = 1.0.1.4500 and ProductVersion 1.0.1.cee57... (some guid)). Also the rest of the file details (name, copyrights) are related to dotnet instead of my own project.
Is there any way I can control the exe details when publishing?
No, the main build output of your project still is a .dll file, the .exe (or linux, mac executables) file is a copied and renamed dotnet.exe (or in case of upcoming 2.0 versions, apphost.exe with the dll name to run embedded).
The exe file is only a helper that boots the runtime and then loads your dll. However, you can try to use binary editing tools like editbin.exe (VS C++ Tools) to modify the file after publishing.
Dotnet core 3.0 added the functionality of versioning the shim exe during publish, like OP was expecting. It should work out of the box now. However that feature does not function if running builds on a Nanoserver based docker environment.
warning NETSDK1074: The application host executable will not be
customized because adding resources requires that the build be
performed on Windows (excluding Nano Server)