10

I have files that I reference from inside by C# code such as:

public static string Canonical()
{
    return File.ReadAllText(@"C:\\myapp\\" + "CanonicalMessage.xml");
}

How do I reference this file from within an Azure Function?

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    var data = File.ReadAllText(@"c:\\myapp\\" + "CanonicalMessage.xml");

    //etc
}

Perhaps I can simply embed this resource in the project?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062

1 Answers1

12

Yes, put the file at the root of Azure Function project and set its property Copy to Output Directory to Copy if newer. Use code below.

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log, ExecutionContext context)
{
    var data = File.ReadAllText(context.FunctionAppDirectory+"/CanonicalMessage.xml");

    //etc
}

Check the doc for more details.

If we need to add this file from anywhere locally, right click on Function project, Edit <FunctionProjectName>.csproj. Add Item below, relative or absolute path are both ok.

<ItemGroup>
  <None Include="c:\\myapp\\CanonicalMessage.xml">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </None>
</ItemGroup> 
Jerry Liu
  • 17,282
  • 4
  • 40
  • 61
  • does it matter which project i put that file in as long as i set the property to `copy to output directory` ??? can i put it in any project in any folder so long as it exists within the solution? – Alex Gordon Nov 19 '18 at 01:43
  • @l--''''''---------'''''''''''' We can put it anywhere we want, just make sure we copy it to function output directory when project built. See my update. – Jerry Liu Nov 19 '18 at 01:55