简体   繁体   中英

how to use NewtonSoft.json in MS build task?

I have a build task, in which I want to do some json serialization/deserialization using newtonsoft.json as follows:

<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>

The above code does not work as I am making use of NewtonSoft.Json in the above task which is not referenced in this build task.

How can I import or reference NewtonSoft.Json in my build task. I am using Visual Studio 2017 Professional, .NET Core 2.0 and the project is a WebApi application ?

Got it working with the following:

<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>

How can I import or reference NewtonSoft.Json in my build task.

Since you are using Visual Studio 2017 Professional, .NET Core 2.0 and the project is a WebApi application, you will find below code snippet in the .csproj file:

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

So the reference NewtonSoft.Json is added to the project. But we could not reference this in the task. Because task dlls cannot reference any packages - they are loaded into the running msbuild instance and will only be able to reference the dlls that are available to the hosting msbuild instance. See this thread for detailed info.

As a workaround, you can try to reference the dll file from the packages folder directly:

    <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>

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM