'NuGet content file not found at runtime when running project that uses it
I have a 3 class library solution which has the following dependency diagram
LanguageDetector.NTextCat.DependencyInjection --> LanguageDetector.NTextCat --> LanguageDetector
The project LanguageDetector.NTextCat requires an xml file to work, because it reads from it at runtime. When running tests in that project, all is good, everything works properly.
It reads it with
var factory = new RankedLanguageIdentifierFactory();
var rankedLanguageIdentifier = factory.Load("Wiki82.profile.xml");
which basically it does some
File.OpenRead("Wiki82.profile.xml")
behind the scenes.
I have the following in the LanguageDetector.NTextCat.csproj with a Pack="True"
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>true</IsPackable>
<Title>$(AssemblyName)</Title>
<VersionPrefix>0.6.0</VersionPrefix>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\LanguageDetector\LanguageDetector.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="NTextCat" Version="0.3.65" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Wiki82.profile.xml" Pack="true">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
</Project>
so when I pack it dotnet pack -o foo I can see the 3 nupkg and the Rubiko.LanguageDetector.NTextCat.0.6.0.nupkg has a content folder with the Wiki82.profile.xml file.
The problem is when I use these nuget libraries with my main project. When running tests I can see it fails at runtime with a FileNotFoundException
System.IO.FileNotFoundException: Could not find file '/media/diegosasw/data/src/microservices/my-command-service/test/My.CmdService.FunctionalTests/bin/Debug/net6.0/Wiki82.profile.xml'.
File name: '/media/diegosasw/data/src/microservices/my-command-service/test/My.CmdService.FunctionalTests/bin/Debug/net6.0/Wiki82.profile.xml'
at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)
at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode)
at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
at System.IO.Strategies.FileStreamHelpers.ChooseStrategy(FileStream fileStream, String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize)
at System.IO.File.OpenRead(String path)
at NTextCat.BasicProfileFactoryBase`1.Load(String inputFilePath, Func`2 filterPredicate)
at LanguageDetector.NTextCat.NTextCatLanguageDetector.Detect(String text, CancellationToken cancellationToken)
at My.CmdService.Domain.Models.ChatAggregate.AddAgentMessage(DateTime createdOn, Guid agentId, String message, IImmutableList`1 validationErrorsToSkip, ILanguageDetector languageDetector, IAgentMessageValidator agentMessageValidator) in /media/diegosasw/data/src/microservices/my-command-service/src/My.CmdService.Domain/Models/ChatAggregate.cs:line 205
It is true, the xml file is not in the output folder.
I have tried adding the file as Content and as EmbeddedResource but there's no luck. The file is not copied across from the nuget package's content files to the net6.0/Wiki82.profile.xml
How to do that?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
