2017-12-18 252 views
0

我有一个生成任务,其中我想做使用newtonsoft.json一些JSON序列化/反序列化如下:如何在MS构建任务中使用NewtonSoft.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应用程序?

+0

此问题的任何更新?你解决了这个问题吗?如果没有,请您告诉我关于这个问题的最新信息? –

+0

你的答案确实奏效。但是当我尝试构建项目时,我遇到了其他构建错误。你可以尝试运行上述任务并检查错误吗? –

回答

0

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

由于您使用的Visual Studio 2017年专业,.NET 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

作为一种变通方法,您可以尝试引用直接从包文件夹中的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> 

希望这有助于。

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

真的很感谢你的分享。它可以帮助其他社区成员获得相同的问题 –

+0

您也可以尝试使用'$(NuGetPackageRoot)newtonsoft.json \ 10.0.3 \ ...',因此您不需要对nuget路径进行硬编码并使其跨平台工作( CodeTaskFactory也适用于mono的msbuild)。 –

+0

这是一个非常好的建议。感谢@MartinUllrich。 –

相关问题