我有一個生成任務,其中我想做使用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應用程序?
此問題的任何更新?你解決了這個問題嗎?如果沒有,請您告訴我關於這個問題的最新信息? –
你的答案確實奏效。但是當我嘗試構建項目時,我遇到了其他構建錯誤。你可以嘗試運行上述任務並檢查錯誤嗎? –