@Song Xu (NCS) ,Welcome to Microsoft Q&A, based on my test, I indeed reproduced your problem about your NETSDK1061 error.
why the error only occurred when the main project is using .NET8, while before in .NET6 it did not occur?
Because .NET 8.0 projects are no longer compatible with .NET Core 2.1 libraries. While .NET 6.0 still supports .NET Core 2.1, .NET 8.0 has removed support for older versions, so you'll encounter version-conflict errors when publishing. And based on my test, if I upgrade .NET Core2.1 to .net 6.0, the error will not occur again.
why doing "dotnet restore" manually will solve this error in .NET8
dotnet restore solves this problem because it ensures that the correct version is downloaded and used by restoring the project's dependencies and tools. Specifically, it resolves version mismatches between projects and libraries.
Hope it could you understand the question.
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.