【问题标题】:MSBuild cannot find a reference - ReactJs.NETMSBuild 找不到参考 - ReactJs.NET
【发布时间】:2016-07-15 18:01:48
【问题描述】:

将 Newtonsoft.Json 升级到 9.0.0 版本并将 ReactJS.Net 包升级到 2.5.0 后,TransformBabel.proj 停止工作:

  <?xml version="1.0" encoding="utf-8" ?>
  <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="TransformBabel">
      <!-- ReactJS.NET - Transpile JavaScript via Babel -->
      <UsingTask AssemblyFile="$(OutputPath)\React.MSBuild.dll" TaskName="TransformBabel" />
      <Target Name="TransformBabel">
          <TransformBabel SourceDir="$(MSBuildProjectDirectory)" />
      </Target>
  </Project>

返回以下内容:

TransformBabel.proj(6, 3): error MSB4018: The "TransformBabel" task failed unexpectedly.
[Exec] TransformBabel.proj(6, 3): error MSB4018: React.TinyIoC.TinyIoCResolutionException: Unable to resolve type: React.IReactSiteConfiguration ---> System.TypeInitializationException: The type initializer for 'React.ReactSiteConfiguration' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.

似乎无法加载Newtonsoft 6.0.0.0 版本。 web.config 有一个程序集重定向:

  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
  </dependentAssembly>

但我不确定,因为它正在启动一个新的 msbuild 进程,如果它被忽略。我想提示 msbuild 程序集的位置,但直到现在都没有成功。

【问题讨论】:

  • $(OutputPath) 上是否存在 Newtonsoft.Json.dll?
  • @KMoraz 是的,但不是 6.0.0.0 版本。
  • 这就是错误的解释。从源而不是项目输出路径使用 React.MSBuild.dll。见usage
  • @KMoraz 的来源是指 nuget 包文件夹?
  • 是的,或者它存在于输出路径之外的任何其他位置。

标签: msbuild reactjs.net


【解决方案1】:

在这里聚会有点晚了,但希望我的经验能对遇到同样问题的其他人有所帮助。

我最近遇到了与React.MSBuild 3.1.0. 相同的问题,因为我已使用 NuGet 将Newtonsoft.Json 更新到最新 (10.0.3) 并正确设置重定向,所以它似乎已对特定版本进行了硬编码,但构建保留与您提到的相同的错误失败。

我所做的只是卸载所有 React 包(MSBuild 和 Core)以及 Newtonsoft.Json(使用 -force,因为还有其他依赖项),然后让 NuGet 再次安装 React.MSBuild .它安装了所有依赖项,导致获得Newtonsoft.Json 9.0.1.

不确定他们为什么将 Newtonsoft.Json 库限制为特定版本,但这更多是 React 开发人员的问题。除非您需要最新(或其他特定版本),否则这应该可以解决问题。

【讨论】:

  • 感谢您的帮助,但它不能解决问题,因为我需要 Newtonsoft.Json 的 10+ 版本。最好的!
【解决方案2】:

我知道这是一篇旧帖子...但这是我的解决方法:

我把Newtonsoft.Json.dll v6.0.0.0放到项目目录相对的Tools目录下,让msbuild复制到$(OutputPath),满足TransformBabel任务条件。
我的TransformBabel.proj 看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="TransformBabel">
  <!-- ReactJS.NET - Transpile JavaScript via Babel -->
  <UsingTask AssemblyFile="$(OutputPath)\React.MSBuild.dll" TaskName="TransformBabel" />
  <Target Name="TransformBabel">
    <Copy SourceFiles="$(MSBuildProjectDirectory)\Tools\Newtonsoft.Json.dll" DestinationFolder="$(OutputPath)" />
    <TransformBabel SourceDir="$(MSBuildProjectDirectory)" />
  </Target>
</Project>

完成此TransformBabel 任务后,让msbuild 用我的项目实际使用的Newtonsoft.Json.dll 版本覆盖$(OutputPath) 中的Newtonsoft.Json.dll v6.0.0.0,例如:v8.0.3
所以,在主项目.csproj,我有这样的东西:

<ItemGroup>
  ...
  <Reference Include="React.MSBuild, Version=2.3.0.0, Culture=neutral, PublicKeyToken=9aed67b161f7db78, processorArchitecture=MSIL">
    <HintPath>Tools\React.MSBuild.dll</HintPath>
    <Private>True</Private>
  </Reference>
  ...
</ItemGroup>
...
<ItemGroup>
  ...
  <Content Include="Tools\Newtonsoft.Json.dll" />
  <Content Include="Tools\React.MSBuild.dll" />
  ...
</ItemGroup>
...
<Target Name="TransformBabel" AfterTargets="Build">
  <Exec Command="&quot;$(msbuildtoolspath)\msbuild.exe&quot; $(ProjectDirectory)TransformBabel.proj /p:OutputPath=$(OutputPath) /nr:false" />
</Target>

<Target Name="AfterTransformBabel" AfterTargets="TransformBabel">
  <Copy SourceFiles="..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll" DestinationFolder="$(OutputPath)" />
</Target>

根据您的需要替换AfterTransformBabel 任务中的路径Newtonsoft.Json.8.0.3

【讨论】:

    猜你喜欢
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    相关资源
    最近更新 更多