C
C#ā€¢4w ago
Esa

dotnet publish fails due to a build error that doesn't happen when I run just dotnet build

Hi there, I'm having a weird thing going on here. I use Jetbrains Rider, and have a project that uses a class library a colleague made. The error that is happening says that Required member X.Y must be set in the object initializer or attribute constructor. And this is super weird to see as an Error and not a warning, because 1) we never instantiate that class, it's resolved through DI and 2) this works in a regular build. So it's only during the publish part this goes wrong. I don't even know where to begin troubleshooting this. Any advice?
15 Replies
Pobiega
Pobiegaā€¢4w ago
Do you get the same behaviour when doing build/publish via Cli instead of via rider?
Angius
Angiusā€¢4w ago
Is it maybe an AoT publish? Regular run might be setting that required prop via relfections, while AoT might be trying to not use reflection there
Pobiega
Pobiegaā€¢4w ago
Yeah thats my guess too
Esa
Esaā€¢4w ago
I haven't actually tried these things manually I'll give it a try
Pobiega
Pobiegaā€¢4w ago
What publish arguments are you using?
Esa
Esaā€¢4w ago
so now I just tried a plain dotnet publish, and that worked
Pobiega
Pobiegaā€¢4w ago
You could just do a --nobuild or whatever the flag is, if its a "normal" publish
Esa
Esaā€¢4w ago
but the publish I've set up, is like this
No description
Pobiega
Pobiegaā€¢4w ago
$singlefile
MODiX
MODiXā€¢4w ago
dotnet publish -c Release -r <runtime identifier> -p:PublishSingleFile=true Use of -p:PublishSingleFile=true implies --self-contained true. Add --self-contained false to publish as runtime-dependent. -r RID and -p:PublishSingleFile=true can be moved to .csproj as the following properties:
<RuntimeIdentifier>RID</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>RID</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
but to target multiple RIDs, you have to use dotnet publish with the -r option for each RID. You can also add -p:IncludeNativeLibrariesForSelfExtract=true to include native libraries (like Common Language Runtime dlls) in the output executable. You might want to instead publish your application compiled Ahead Of Time to native code, see $nativeaot for examples. Single file publishing | Runtime Identifier (RID) catalog | dotnet publish
Pobiega
Pobiegaā€¢4w ago
Use the above to reproduce that publish from CLI, see if you get the same error
Esa
Esaā€¢4w ago
Hm, what's a runtime identifier in this case?
Pobiega
Pobiegaā€¢4w ago
Win-x64
Esa
Esaā€¢4w ago
dotnet publish -c Release -r win-x64 -p:PublishSingleFile=true This command also worked šŸ¤”
Pobiega
Pobiegaā€¢4w ago
only difference I can see then is the trimming