You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

链接其他项目Web.config时遇路径未找到错误,本地正常Azure部署失败

问题

我有两个项目:WebApi和IntegrationTests。WebApi项目中存在Web.config文件,我希望从IntegrationTests项目中访问该文件的配置值。

我的操作步骤为:

  • 将Web.config以链接形式添加到IntegrationTests项目
  • 将该链接项的Build Action设置为Embedded Resource,Copy to Output Directory设置为Copy always
  • 通过以下代码读取配置值:
[Test]
public async Task Test_ReturnsSuccess()
{
    using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("IntegrationTests.Web.config"))
    {
        var doc = XDocument.Load(stream);

        var myValue = doc.Element("configuration")
                   .Element("appSettings")
                   .Elements("add")
                   .FirstOrDefault(e => e.Attribute("key").Value == "MyValue").Attribute("value").Value;
    }
}

该代码在本地Visual Studio中可正常运行,但部署到Azure时出现如下错误:

##[error]CSC(0,0): Error CS1566: Error reading resource 'IntegrationTests.Web.config' -- 'Could not find a part of the path 'D:\54703\s\WebApi\Web.config'.'
CSC : error CS1566: Error reading resource 'IntegrationTests.Web.config' -- 'Could not find a part of the path 'D:\54703\s\WebApi\Web.config'.' [D:\54703\s\Test\IntegrationTests\IntegrationTests.csproj]

请问我遗漏了什么配置或操作?

解决方案

核心原因

Azure DevOps构建环境的目录结构与本地Visual Studio不一致,链接文件的路径解析出现问题,导致构建时无法找到原始Web.config文件;同时将链接文件设为Embedded Resource时,CI环境无法正确访问到原始文件的物理路径。

修复步骤

  1. 修正链接文件的相对路径
    打开IntegrationTests.csproj,找到链接Web.config的节点,确保Include属性使用相对于测试项目的路径(而非本地绝对路径),示例配置如下:

    <EmbeddedResource Include="..\WebApi\Web.config">
      <Link>Web.config</Link>
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </EmbeddedResource>
    

    本地Visual Studio可能自动生成绝对路径,需手动修改为相对路径。

  2. 调整项目构建顺序
    在Azure DevOps构建管道中,确保WebApi项目先于IntegrationTests项目构建。可以在构建任务中调整项目编译顺序,或在IntegrationTests.csproj中添加对WebApi项目的依赖:

    <ItemGroup>
      <ProjectReference Include="..\WebApi\WebApi.csproj" />
    </ItemGroup>
    
  3. 改用复制文件的替代方案
    若链接嵌入方式问题较多,可直接在构建时复制Web.config到测试项目输出目录:

    • 在IntegrationTests.csproj中添加配置:
      <ItemGroup>
        <Content Include="..\WebApi\Web.config">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
      </ItemGroup>
      
    • 修改读取代码,直接从输出目录读取文件:
      [Test]
      public async Task Test_ReturnsSuccess()
      {
          var configPath = Path.Combine(AppContext.BaseDirectory, "Web.config");
          var doc = XDocument.Load(configPath);
      
          var myValue = doc.Element("configuration")
                     .Element("appSettings")
                     .Elements("add")
                     .FirstOrDefault(e => e.Attribute("key")?.Value == "MyValue")?.Attribute("value")?.Value;
      }
      

    这种方式避免了嵌入资源的路径解析问题,稳定性更高。

  4. 检查Azure DevOps代码目录结构
    确认Azure DevOps拉取代码后的目录结构与本地一致,比如WebApi和IntegrationTests是否处于同级目录。若结构不同,需调整链接路径或修改构建管道的代码检出路径。

内容的提问来源于stack exchange,提问作者Joel Wiklund

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 05:36:00