如何在MS构建任务中使用NewtonSoft.json?

时间:2017-12-18 13:56:08

标签: c# .net json visual-studio msbuild

我有一个构建任务,我想在其中使用newtonsoft.json进行一些json序列化/反序列化,如下所示:

<UsingTask TaskName="ReplaceFileText" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
      <TargetFilename ParameterType="System.String" Required="true" />
      <SourceFilename ParameterType="System.String" Required="true" />
    </ParameterGroup>
    <Task>
      <Reference Include="System.Core" />

      <Using Namespace="System" />
      <Using Namespace="System.IO" />

      <Code Type="Fragment" Language="cs">
        <![CDATA[

            string sourceJsonString = File.ReadAllText(SourceFilename);
            string targetJsonString = File.ReadAllText(TargetFilename);

            dynamic sourceJson = JsonConvert.DeserializeObject(sourceJsonString);
            dynamic targetJson = JsonConvert.DeserializeObject(targetJsonString);

            targetJson.firstProperty = sourceJson.firstProperty;
            targetJson.secondProperty = sourceJson.secondProperty;


            targetJsonString = JsonConvert.SerializeObject(targetJson, Formatting.Indented);

            File.WriteAllText(targetFilename, targetJsonString);

          ]]>
      </Code>
    </Task>
  </UsingTask>

  <Target Name="TransformsWithStaging" BeforeTargets="Build">
    <ReplaceFileText TargetFilename="$(ProjectDir)appsettings.json" SourceFilename="$(ProjectDir)appsettings.Staging.json" />
  </Target>

上面的代码不起作用,因为我在上面的任务中使用了NewtonSoft.Json,这个任务没有引用。

如何在构建任务中导入或引用NewtonSoft.Json。我使用的是Visual Studio 2017 Professional,.NET Core 2.0,该项目是WebApi应用程序吗?

2 个答案:

答案 0 :(得分:2)

使用以下内容:

<UsingTask TaskName="ReplaceFileText" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
      <TargetFilename ParameterType="System.String" Required="true" />
      <SourceFilename ParameterType="System.String" Required="true" />
    </ParameterGroup>
    <Task>
      <Reference Include="System.Core" />
      <Reference Include="System.Runtime" />
      <Reference Include="System.Dynamic.Runtime" />
      <Reference Include="$(USERPROFILE)\.nuget\packages\microsoft.netcore.app\2.0.0\ref\netcoreapp2.0\System.ObjectModel.dll" />
      <Reference Include="$(USERPROFILE)\.nuget\packages\newtonsoft.json\10.0.3\lib\netstandard1.3\Newtonsoft.Json.dll" />
      <Reference Include="$(USERPROFILE)\.nuget\packages\system.runtime.serialization.primitives\4.1.1\lib\netstandard1.3\System.Runtime.Serialization.Primitives.dll" />

      <Using Namespace="System" />
      <Using Namespace="System.IO" />
      <Using Namespace="System.Collections.Specialized" />
      <Using Namespace="Newtonsoft.Json" />
      <Using Namespace="Newtonsoft.Json.Linq" />

      <Code Type="Fragment" Language="cs">
        <![CDATA[

            string sourceJsonString = File.ReadAllText(SourceFilename);
            string targetJsonString = File.ReadAllText(TargetFilename);

            JObject sourceJsonObject = JObject.Parse(sourceJsonString);
            JObject targetJsonObject = JObject.Parse(targetJsonString);

            targetJsonObject["someProperty"] = sourceJsonObject["someProperty"];

            File.WriteAllText(TargetFilename, targetJsonObject.ToString());

          ]]>
      </Code>
    </Task>
  </UsingTask>

  <Target Name="TransformsWithDevelopment" Condition="'$(Configuration)'=='Development'" BeforeTargets="Build">
    <ReplaceFileText TargetFilename="$(ProjectDir)appsettings.json" SourceFilename="$(ProjectDir)appsettings.Development.json" />
  </Target>

  <Target Name="TransformsWithStaging" Condition="'$(Configuration)'=='Staging'" BeforeTargets="Build">
    <ReplaceFileText TargetFilename="$(ProjectDir)appsettings.json" SourceFilename="$(ProjectDir)appsettings.Staging.json" />
  </Target>

  <Target Name="TransformsWithProduction" Condition="'$(Configuration)'=='Production'" BeforeTargets="Build">
    <ReplaceFileText TargetFilename="$(ProjectDir)appsettings.json" SourceFilename="$(ProjectDir)appsettings.Production.json" />
  </Target>

答案 1 :(得分:0)

  

如何在构建任务中导入或引用NewtonSoft.Json。

由于您使用的是Visual Studio 2017 Professional,.NET Core 2.0且该项目是WebApi应用程序,因此您可以在.csproj文件中找到以下代码段:

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
    <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
  </ItemGroup>

因此将引用NewtonSoft.Json添加到项目中。但是我们无法在任务中引用它。因为任务dll不能引用任何包 - 它们被加载到正在运行的msbuild实例中,并且只能引用托管msbuild实例可用的dll。有关详细信息,请参阅this thread

作为一种解决方法,您可以尝试直接从packages文件夹中引用dll文件:

    <Task>
        <Reference Include="C:\Users\<UseraName>\.nuget\packages\newtonsoft.json\10.0.3\lib\netstandard1.3\Newtonsoft.Json.dll" />
        <Code Type="Fragment" Language="cs">...working fine...</Code>
    </Task>

希望这有帮助。