Return XML file from Web API I have a HttpGet method which returns XML file:

[HttpGet]
public async Task&lt;IActionResult&gt; Get()
{
    XmlDocument xmlDoc = new XmlDocument();
    await Task.Run(() =&gt; xmldoc.Load(_appEnvironment.WebRootPath + @"\\user.config"));
    return Ok(xmlDoc);
}


Colleague said to me that this piece of code has smell and that I interrupted SOLID principles, but he did not explained to me what exact I do wrong.

Guys, please review my code and please explain me what I've done wrong.
Dependency Inversion Priciple

The smell is that your code violates the Dependency Inversion Priciple which reads


  Entities must depend on abstractions not on concretions. It states that the high level module must not depend on the low level module, but they should depend on abstractions.


This meas that your API depends on the Load method of the XmlDocument and not an abstraction such as a service like this one.

interface IXmlLoader
{
    XmlDocument Load(string path);
}


In consequence you cannot replace it for testing or use dependency injection and easily exchange it later if necessary. A correct implementation could look like this where you pass the dependency via the constructor. In some cases it might be also a property:

class XmlController
{
    private readonly IXmlLoader _xmlLoader;
    public XmlControler(IXmlLoader xmlLoader)
    {
        _xmlLoader = xmlLoader;
    }

    // Sometimes a property.
    // public IXmlLoader XmlLoader { get; set; }

    [HttpGet]
    public async Task&lt;IActionResult&gt; Get()
    {
        await Task.Run(() =&gt; _xmlLoader.Load(_appEnvironment.WebRootPath + @"\\user.config"));
        return Ok(xmlDoc);
    }    

}


This is just an example. In your real code you should add null checks.



Another smaller smell is the hardcoded string user.config that you should at least configure via a property.