2017-12-18 255 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。 –

相關問題