Search code examples
c#azurenunit

"Could not find a part of the path" when linking an existing Web.config from another project


I have two projects, WebApi and IntegrationTests. In WebApi I have a Web.config file. I would like to access the configuration values in that Web.config from the IntegrationTests project. What I've done is to add the Web.config as a link to the IntegrationTests project, then change the Build Action to Embedded Resource and Copy to Output Directory to Copy always of that linked item. After that I load the linked Web.config into a stream and get the value with Linq:

[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;
    }
}

This works locally in Visual Studio. When I however try to deploy this in Azure I get the following error:

##[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]

What am I missing here?


Solution

  • I added this to my WebApi.csproj instead:

    <Content Include="Web.config">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        <CopyToPublishDirectory>Always</CopyToPublishDirectory>
        <Link>..\IntegrationTests\Web.config</Link>
    </Content>
    

    So I got a copy of the Web.config in IntegrationTests, then used the above Linq code to get it.