最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Microsoft.XmlSerializer.Generator nuget package and .NET Framework - Stack Overflow

programmeradmin0浏览0评论

I have a framework project that needs to be build The Project.XmlSerializers.dll. It was using the old style csproj file format and I converted to the new SDK style project. I then added the appropriate nuget and cli tool used to generate the Project.XmlSerializers.dll:

    <ItemGroup>
        <PackageReference Include="Microsoft.XmlSerializer.Generator" Version="9.0.1" />
        <DotNetCliToolReference Include="Microsoft.XmlSerializer.Generator" Version="9.0.1" />
    </ItemGroup>

This works fine and my Project.XmlSerializer.dll is correctly generated in the bin directory.

Note that the target framework is still .NET framework 4.8:

    <TargetFramework>net48</TargetFramework>

Now I dotnet pack MyProject.csproj and create my nupkg file for this library. I check the contents of .nuspec of the my project:

      <group targetFramework=".NETFramework4.8">
        <dependency id="Microsoft.XmlSerializer.Generator" version="9.0.1" exclude="Build,Analyzers" />
      </group>

Ok, that's fine. I thought Microsoft.XmlSerializer.Generator was just a build dependency, but I'll carry on. I also check the lib/net48 folder in the nuget package, and it does indeed contain the MyProject.XmlSerializers.dll.

I create a new 4.8 console application and it uses the classic .csproj style (I can't assume consumers will use the new SDK style project). I then add my nuget package to this new console application. That goes ok. Then I build, and I get a warning:

1>------ Build started: Project: ConsoleApp28, Configuration: Debug Any CPU ------
1>  ConsoleApp28 -> C:\Users\me\source\repos\ConsoleApp28\bin\Debug\ConsoleApp28.exe
1>  Possible reasons for this include:
1>    * You misspelled a built-in dotnet command.
1>    * You intended to execute a .NET program, but dotnet-Microsoft.XmlSerializer.Generator does not exist.
1>  Could not execute because the specified command or file was not found.
1>    * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
1>C:\Users\me\source\repos\ConsoleApp28\packages\Microsoft.XmlSerializer.Generator.9.0.1\build\Microsoft.XmlSerializer.Generator.targets(51,5): warning MSB3073: The command "dotnet Microsoft.XmlSerializer.Generator "obj\Debug\ConsoleApp28.exe" --force --quiet obj\Debug\sgen.rsp" exited with code 1.
1>C:\Users\me\source\repos\ConsoleApp28\packages\Microsoft.XmlSerializer.Generator.9.0.1\build\Microsoft.XmlSerializer.Generator.targets(52,5): warning : SGEN: Failed to generate the serializer for ConsoleApp28.exe. Please follow the instructions at /?linkid=858594 and try again.
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

My bin directory of my new console application does contain the MyProject.XmlSerializers.dll so that's great. But why the warning? I then went away and removed that dependency on Microsoft.XmlSerializer.Generator directly from the .nuspec file and recreated the nuget package, because the Serializers have already been created already. Now I don't get the build warning...

So, is there a way to make this nuget package just a build dependency somehow, without it being included in the nuget package itself? When I look at nuget for this Microsoft.XmlSerializer.Generator package, I can see that not many packages appear to include it: .XmlSerializer.Generator#usedby-body-tab

Yet I am just following the instructions here:

/?linkid=858594

I have a .net framework project that needs to be build The Project.XmlSerializers.dll. It was using the old style csproj file format and I converted to the new SDK style project. I then added the appropriate nuget and cli tool used to generate the Project.XmlSerializers.dll:

    <ItemGroup>
        <PackageReference Include="Microsoft.XmlSerializer.Generator" Version="9.0.1" />
        <DotNetCliToolReference Include="Microsoft.XmlSerializer.Generator" Version="9.0.1" />
    </ItemGroup>

This works fine and my Project.XmlSerializer.dll is correctly generated in the bin directory.

Note that the target framework is still .NET framework 4.8:

    <TargetFramework>net48</TargetFramework>

Now I dotnet pack MyProject.csproj and create my nupkg file for this library. I check the contents of .nuspec of the my project:

      <group targetFramework=".NETFramework4.8">
        <dependency id="Microsoft.XmlSerializer.Generator" version="9.0.1" exclude="Build,Analyzers" />
      </group>

Ok, that's fine. I thought Microsoft.XmlSerializer.Generator was just a build dependency, but I'll carry on. I also check the lib/net48 folder in the nuget package, and it does indeed contain the MyProject.XmlSerializers.dll.

I create a new .net 4.8 console application and it uses the classic .csproj style (I can't assume consumers will use the new SDK style project). I then add my nuget package to this new console application. That goes ok. Then I build, and I get a warning:

1>------ Build started: Project: ConsoleApp28, Configuration: Debug Any CPU ------
1>  ConsoleApp28 -> C:\Users\me\source\repos\ConsoleApp28\bin\Debug\ConsoleApp28.exe
1>  Possible reasons for this include:
1>    * You misspelled a built-in dotnet command.
1>    * You intended to execute a .NET program, but dotnet-Microsoft.XmlSerializer.Generator does not exist.
1>  Could not execute because the specified command or file was not found.
1>    * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
1>C:\Users\me\source\repos\ConsoleApp28\packages\Microsoft.XmlSerializer.Generator.9.0.1\build\Microsoft.XmlSerializer.Generator.targets(51,5): warning MSB3073: The command "dotnet Microsoft.XmlSerializer.Generator "obj\Debug\ConsoleApp28.exe" --force --quiet obj\Debug\sgen.rsp" exited with code 1.
1>C:\Users\me\source\repos\ConsoleApp28\packages\Microsoft.XmlSerializer.Generator.9.0.1\build\Microsoft.XmlSerializer.Generator.targets(52,5): warning : SGEN: Failed to generate the serializer for ConsoleApp28.exe. Please follow the instructions at https://go.microsoft.com/fwlink/?linkid=858594 and try again.
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

My bin directory of my new console application does contain the MyProject.XmlSerializers.dll so that's great. But why the warning? I then went away and removed that dependency on Microsoft.XmlSerializer.Generator directly from the .nuspec file and recreated the nuget package, because the Serializers have already been created already. Now I don't get the build warning...

So, is there a way to make this nuget package just a build dependency somehow, without it being included in the nuget package itself? When I look at nuget.org for this Microsoft.XmlSerializer.Generator package, I can see that not many packages appear to include it: https://www.nuget.org/packages/Microsoft.XmlSerializer.Generator#usedby-body-tab

Yet I am just following the instructions here:

https://go.microsoft.com/fwlink/?linkid=858594

Share Improve this question edited Feb 7 at 8:30 rene 42.5k78 gold badges121 silver badges165 bronze badges asked Feb 6 at 17:44 BillyBilly 4091 gold badge7 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Microsoft.XmlSerializer.Generator should be a build dependency. The fix is to use PrivateAssets="all"

    <ItemGroup>
        <PackageReference Include="Microsoft.XmlSerializer.Generator" Version="9.0.1" PrivateAssets="all" />
        <DotNetCliToolReference Include="Microsoft.XmlSerializer.Generator" Version="9.0.1" />
    </ItemGroup>

Then the .nuspec is clean of Microsoft.XmlSerializer.Generator and the ConsoleApp builds fine. Frustrating that this is not specified on the Microsoft website here: https://go.microsoft.com/fwlink/?linkid=858594

发布评论

评论列表(0)

  1. 暂无评论