【问题标题】:The type 'HttpResponseMessage' is defined in an assembly that is not referenced'HttpResponseMessage' 类型是在未引用的程序集中定义的
【发布时间】:2020-12-15 09:02:14
【问题描述】:

我只是想通过使用 MSBuild 来构建没有 Visual Studio 的应用程序。

使用

  • Visual Studio 构建工具 2017。
  • 点网框架 4.6.1 SDK
  • MSBuild 自动检测:使用 msbuild 版本“15.9.21.664”

完整的错误信息:

错误 CS0012:“HttpResponseMessage”类型在未引用的程序集中定义。您必须添加对程序集 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 的引用。

知道为什么吗?谢谢...

更新

这是 Visual Studio 的 dll 路径

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\System.Net.Http.dll

这没有Visual Studio,只有MSBuild

C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\Common7\IDE\CommonExtensions\Microsoft\NuGet\Plugins\CredentialProvider.Microsoft

文件路径不同。我该如何解决这个问题?

【问题讨论】:

  • 嗨 Loran,有关于这个问题的最新消息吗?
  • @PerryQian-MSFT,您的回答让我很明白,我发现版本不同,参考位置也不同。所以我只是让它引用相同的位置并再次构建。现在它可以工作了。但我仍然需要检查。感谢您的详细解答。
  • 此外,您可以将System.Net.Http.dllhintpath 强设置为特定路径。并使用它来确保他们使用相同的路径或相似的路径。另外,感谢您分享您的解决方案,并很高兴知道问题消失了。我已将您的解决方案添加到我的答案和一些细节中。你可以检查一下。看我的update 1 :) 如果有帮助,你可以考虑accept it

标签: c# visual-studio msbuild


【解决方案1】:

您的项目似乎使用了关于System.Net.Http的较新版本dll,如果某些依赖项使用旧版本4.0.0.0 System.Net.Http.dll,它将错过旧版本。

外,请按以下步骤操作:

1)确保你的xxx.csproj文件有这个节点,如果没有,你应该这样修改:

<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>

另外,尝试将System.Net.Http.dll复制本地设置为true

xxx.csproj文件中像这样修改这样的节点:

<Reference Include="System.Net.Http">
     <Private>True</Private>
  </Reference>   

2)Administrator身份打开CMD,然后输入:

     cd C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools
               
     gacutil /i xxxxx\System.Net.Http.dll(path where the System.Net.Http.dll exists)

 // like the path C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\System.Net.Http.dll

3)也许你可以在app.config文件中试试这个:

<configuration>  
   <runtime>  
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
         <dependentAssembly>  
            <assemblyIdentity name="System.Net.Http"  
                              publicKeyToken="32ab4ba45e0a69a1"  
                              culture="neutral" />  
            <bindingRedirect oldVersion="0.0.0.0-4.2.0.0"  
                             newVersion="4.2.0.0"/>  
         </dependentAssembly>  
      </assemblyBinding>  
   </runtime>  
</configuration> 

a similar issue about it

4)然后删除binobj文件夹,然后使用Build Tool for VS2017重新构建项目。

================================================ ======

更新 1

感谢您分享您的解决方案。实际上,不确定当您将项目移动到构建代理时系统是否找不到System.Net.Http

其实你可以在你的项目中使用hintpath来强制引用System.Net.Http的路径,并确保它们是相同的。此外,此解决方案将确保将 dll 复制到输出文件夹中。它会让VS或msbuild找到并识别DLL。

并且您已将 DLLL 的位置更改为相同,以便修复问题。

您可以尝试在您的xxx.csproj 文件中使用它:

它还会将此类 dll 复制到输出文件夹中。

<Reference Include="System.Net.Http">
      <HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Net.Http.dll</HintPath>
    </Reference>

<Reference Include="System.Net.Http">
          <HintPath>C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\System.Net.Http.dll</HintPath>
        </Reference>

【讨论】:

  • 谢谢它是完美的。
【解决方案2】:

在您的 web.config 或 App.config 中添加:

          <compilation debug="true" targetFramework="4.6.1" />
             <assemblies>
                <add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
             </assemblies>
          </compilation>
   

【讨论】:

  • @DA,还是有这个问题。
猜你喜欢
  • 2016-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-16
相关资源
最近更新 更多