C# dependecies aren't being included with publish - c#

I'm trying to publish a web API built in .NET Core to a Ubuntu server but am having difficulty with the dependencies. I am new to doing this in C# and haven't found a concise answer to how dependencies are included with the publish command. I was lead to believe that they were complied into a .dll but I am getting this error when running my app
Error:
An assembly specified in the application dependencies manifest (api.deps.json) was not found:
package: 'Newtonsoft.Json', version: '11.0.2'
path: 'lib/netstandard2.0/Newtonsoft.Json.dll'
Shouldn't that have been included in the "Release" directory after being published?
I'm using .NET Core on a Mac and then publishing to an Ubuntu sever should that affect anything here.
.csproj:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
</ItemGroup>
</Project>

Shouldn't that have been included in the "Release" directory after being published?
No.
When you publish a project, you specify a different directory for the publish output when creating the publish profile.
The Release directory is only for assets that are used during debugging. During development, the NuGet dependencies are not in the Release folder, they are in the NuGet cache. So, you cannot always just copy the Release folder and expect it to run.
After publishing, the entire application (including all dependencies) are output to the publish location. Do note that the publish output doesn't necessarily have to be a folder. For example, depending on the type of project it may be published to IIS with web deploy or to an FTP location.

Related

GLIBC_2.2.5 not defined in file libpthread.so.0 with link time reference

I'm creating a C# app which I have build or as they call it "published" for linux-x64 following this XML in the csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<Configuration>release</Configuration>
<InvariantGlobalization>true</InvariantGlobalization>
<TargetFramework>net5.0</TargetFramework>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
<PublishTrimmed>true</PublishTrimmed>
<PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="mongocsharpdriver" Version="2.11.6" />
<PackageReference Include="MongoDB.Bson" Version="2.11.6" />
<PackageReference Include="MongoDB.Driver" Version="2.11.6" />
<PackageReference Include="MongoDB.Driver.Core" Version="2.11.6" />
<PackageReference Include="PCSC" Version="5.0.0" />
<PackageReference Include="PCSC.Iso7816" Version="5.0.0" />
</ItemGroup>
</Project>
I'm on Linux Ubuntu 20.04 .
I published the application using CLI to the folder bin/release etc etc. It ran just fine when I tested it. Now, for the main branch, I deleted all code. Moved the directory release up in the main directory, and pushed it to GIT.
Whenever you try to run it now, it gives the following error:
./Appname: relocation error: ./Appname: symbol pthread_attr_init version GLIBC_2.2.5 not defined in file libpthread.so.0 with link time reference
As I'm not a native C# developer and still learning, I have absolutely no idea what this is about, except it apparently has something to do with me moving the directory (I quess?)
Does anyone know what this error is or where it comes from, how to fix it, and how to prevent in the future? As I would like to be able to publish an application without errors.
solved
I Found the answer. There is another directory within the build directory called "publish" which contains the working executable. In other words. The exe is in the folder release/net5.0/linux-x64/publish instead of release/net5.0/linux-x64. When I run the executable from that directory, everything works just fine. For other newbies: It is in the folder release because my XML project file has a Configuration field set to release. It is in the folder Debug by default.

How to publish a full self-contained .NetCoreApp as exe?

I'm trying to publish an usable exe-file of my C# .NetCoreApp2.2 console application.
I have written a little application in C# using the Visual Studio Code IDE. I started by typing dotnet new console and opened the project within Visual Studio Code. I installed some third-party-libraries like log4net and DocumentFormat.OpenXml.
I can debug my application within Visual Studio Code - no problems. I can run dotnet run within the project source directory - no problems. I can also run dotnet publish -c Release -r win10-x64 --self-contained true without any problems.
After the publish command I get the expected .exe-file (and a lot of other files?) within the expected subdirectory.
Now to the problem:
As soon as I try to execute the .exe-file I get an unhandled expection.
I do have third-party-libraries referenced, but as I understand it, the self-contained option should take care about that - does it?
I have googled and saw some results describing the publish-process talking about a project.json - file which I do not have. Also people describe that this file will be created using the given dotnet-command. For me it never will be created using that command - any additional flag to set?
This is my .csproj-file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="log4net" Version="*" />
<PackageReference Include="Nunit3TestAdapter" Version="3.13.0" />
<PackageReference Include="NUnit" Version="3.12.0"/>
<PackageReference Include="System.Configuration.ConfigurationManager" Version="*" />
<PackageReference Include="DocumentFormat.OpenXml" Version="2.8.1" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0006" />
</ItemGroup>
</Project>
Might the "*" for version be a problem?
I would expect to get a single .exe-file (which might be huge) that executes and shows the same output as I will get using the command dotnet run within the source-directory of my project.
Any Ideas?

How to precompile views in ASP.NET Core 2.0?

I set up my solution according to this article. I left out some of the things because according to this, ASP.NET Core 2.0 precompiles views by default. In the end, I publish it to a folder, which ends successfully, but my precompiledviews.dll is missing. I tried setting it explicitly in .csproj, but no luck.
Edit: Both of the projects inside the solution are just default MVC templates.
I bet you use Self-contained deployment, i.e. publish with command like
dotnet publish --configuration Release --runtime win-x64
that results to executable file with all dependencies including .NET Core binaries.
Razor view compilation and precompilation article contains following warning:
Razor view precompilation is currently unavailable when performing a
self-contained deployment (SCD) in ASP.NET Core 2.0. The feature will
be available for SCDs when 2.1 releases.
So if you want to use precompiled Razor views, you should use Framework-dependent deployment, i.e. publish with the following command:
dotnet publish --configuration Release
In this case Razor views are precompiled (by default) and you will find YourAppName.PrecompiledViews.dll among other application binaries.
UPDATE (for Precompiled Views in Library project)
My original answer relates to usual ASP.NET Core MVC application, however the question is specific to project library holding precompiled views aka Self-Contained UI.
ASP.NET Core precompiles views by default during the publish, however this is not the case for the views stored in library project. There is a github issue devoted to this problem. That discussion is pretty much long, however it ends up with the conclusion that for this moment we still need to use solution with custom targets for Razor Views precompilation. It basically the same approach as described in the article referenced by the question.
I've setup test solution with ChildApplication and main MvcApplication and made precompiled views working both for the build and publish.
Here is csproj for ChildApplication (skipping sections of default ASP.NET Core MVC project):
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
</PropertyGroup>
<!-- ... -->
<Target Name="SetMvcRazorOutputPath">
<PropertyGroup>
<MvcRazorOutputPath>$(OutputPath)</MvcRazorOutputPath>
</PropertyGroup>
</Target>
<Target Name="_MvcRazorPrecompileOnBuild" DependsOnTargets="SetMvcRazorOutputPath;MvcRazorPrecompile" AfterTargets="Build" Condition=" '$(IsCrossTargetingBuild)' != 'true' " />
<Target Name="IncludePrecompiledViewsInPublishOutput" DependsOnTargets="_MvcRazorPrecompileOnBuild" BeforeTargets="PrepareForPublish" Condition=" '$(IsCrossTargetingBuild)' != 'true' ">
<ItemGroup>
<_PrecompiledViewsOutput Include="$(MvcRazorOutputPath)$(MSBuildProjectName).PrecompiledViews.dll" />
<_PrecompiledViewsOutput Include="$(MvcRazorOutputPath)$(MSBuildProjectName).PrecompiledViews.pdb" />
<ContentWithTargetPath Include="#(_PrecompiledViewsOutput->'%(FullPath)')" RelativePath="%(_PrecompiledViewsOutput.Identity)" TargetPath="%(_PrecompiledViewsOutput.Filename)%(_PrecompiledViewsOutput.Extension)" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>
</Target>
Here is csproj for parent MvcApplication:
<!-- ... -->
<ItemGroup>
<ProjectReference Include="..\ChildApplication\ChildApplication.csproj" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy "$(ProjectDir)\..\ChildApplication\bin\$(ConfigurationName)\netcoreapp2.0\ChildApplication.PrecompiledViews.dll" "$(TargetDir)" /Y /I" />
</Target>
<Target Name="AddPayloadsFolder" AfterTargets="Publish">
<Exec Command="xcopy "$(ProjectDir)\..\ChildApplication\bin\$(ConfigurationName)\netcoreapp2.0\ChildApplication.PrecompiledViews.dll" "$(PublishDir)" /Y /I" />
</Target>
Dean North in his original article adds direct reference to assembly with precompiled views.
<ItemGroup>
<Reference Include="DashboardExample.PrecompiledViews">
<HintPath>..\DashboardExample\bin\Debug\netcoreapp1.1\DashboardExample.PrecompiledViews.dll</HintPath>
</Reference>
</ItemGroup>
Such approach isn't perfect because it uses assembly built with specific configuration (Debug here). In my project file above, I use separate targets that copy ChildApplication.PrecompiledViews.dll during the build and publish.
Here is Sample Solution on GitHub with both parent and child projects.
I usually just add these to the .csproj. No need to copy files or any of that mess.
<PropertyGroup>
<TargetFramework>netcoreapp2.2</...
...
<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>
<ItemGroup>
...
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.2.0" />
</ItemGroup>

Self Contained EXE not finding correct path on OS X

So I've made a self-contained release of a project with Visual Studio Code. The EXE is working on ubuntu and windows, but can't get it to work on os-x.
I'm using a simple data.json to store some data. The error that occurs on the os-x release, is that it can't find the correct path to data.json. I get this message:
Could not find file '/Users/User/data.json'.
That is not where I store the release or data.json. I store it in the root-directory of the project. Seems like the application is looking for the json-file in my system root directories. How do I fix this?
My XML csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
<RuntimeIdentifiers>win10-x64;osx.10.11-x64;ubuntu.16.10-x64</RuntimeIdentifiers>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.json" Version="10.0.3" />
</ItemGroup>
</Project>

Include Nuget dependencies in my build output?

I am building a modular .NET core application that can load extensions at runtime using MEF. I have 2 projects, one is a library that I want to be able to load at runtime, then I have my main application that will do the loading.
My library project has some Nuget dependencies. In order to load my library at runtime, I need those Nuget dependencies to be available next to the library at runtime, but building using VS2017 does not include these Nuget DLLs as part of the output.
How do I get Nuget DLLs included when I build my library?
Edit: I have tried dotnet publish and dotnet pack, but both of those make me a nupkg file only containing my DLL and not the nuget DLLs I need with it. Also, I can't load a nupkg file at runtime very easily, which is why I'd like to just get the resulting assemblies themselves on their own.
For what it's worth, this is what my csproj looks like:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<AssemblyName>JSON.plugin</AssemblyName>
<IncludeBuiltProjectOutputGroup>true</IncludeBuiltProjectOutputGroup>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Composition" Version="1.0.31" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\BDDGen.Types\BDDGen.Types.csproj" />
</ItemGroup>
</Project>
In order to make the build process copy all referenced dll files from NuGet packages from the cache folder into the build output, set this property inside a <PropertyGroup>:
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

Categories

Resources