gpt4 book ai didi

.net - 不能引用其他项目

转载 作者:行者123 更新时间:2023-12-04 23:07:55 25 4
gpt4 key购买 nike

我想在 Visual Studio 解决方案中创建 3 个项目:

  • Windows 服务(引用业务层、公共(public)层、log4net.dll)
  • 业务层(引用公共(public)层、log4net.dll)
  • 公共(public)层(引用 log4net.dll)

  • 脚步:
  • 我使用向导创建了业务层和公共(public)层,没有做任何修改。我设置了引用,一切正常。
  • 然后我创建了一个新的 Windows 服务项目添加到解决方案中。我注意到它添加了目标平台 x86 而不是 Any CPU。我引用了这 3 件事,代码颜色正确,预编译错误消失:这意味着引用被识别。
  • 但是,当我构建解决方案时,Windows 服务项目出现编译错误:找不到任何引用的程序集。

  • 这里发生了什么? x86 配置是否导致“找不到类型或命名空间名称‘log4net’(您是否缺少 using 指令或程序集引用?)”错误?

    我强制 Windows 服务项目使用任何 CPU 配置。我仔细检查了每一个引用,它们是正确的。我仍然无法引用任何东西。

    系统规范:
  • Windows 7 x64 SP1 Visual Studio 2010
  • 终极版
  • 最佳答案

    好吧,你已经找到了答案。 Log4net 依赖于 System.Web.dll,这是一个在客户端配置文件中不可用的程序集。这种依赖的可能核心原因是 log4net.Appender.AspNetTraceAppender 类,它使用 HttpContext 类并且需要 System.Web。

    现在,这是您需要从库的文档中或通过艰难的方式找到它来了解的内容。构建会产生警告,但它只是警告而不是错误,很容易错过:

    C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1360,9): warning MSB3253: The referenced assembly "C:\projects\WindowsFormsApplication2\ClassLibrary1\bin\Debug\ClassLibrary1.dll" could not be resolved because it has a dependency on "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which is not in the currently targeted framework ".NETFramework,Version=v4.0,Profile=Client". Please remove references to assemblies not in the targeted framework or consider retargeting your project.



    随后是代码中尝试引用包含 log4net 引用的程序集中的类的任何语句的错误。

    C:\projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs(12,9): error CS0246: The type or namespace name 'ClassLibrary1' could not be found (are you missing a using directive or an assembly reference?)



    如果您在编写代码时没有增量构建,您也不会错过任何内容,并且可能会出现很多错误。您专注于错误并且不会意识到警告是错误的真正来源。另一个令人讨厌的陷阱是 IntelliSense 一开始很高兴,它确实正确地解析了程序集引用。有一些证据表明警告并不总是产生,纯粹是因为许多程序员似乎错过了它。不过,我还没有找到这种情况。没有把握。

    关于.net - 不能引用其他项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6562013/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com