Azure / Azure/azure-functions-host

Build issue prevents Razor from working in functions v2

Open
#4,132 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
C#
Stars
2k
Forks
482
Avg merge
2d 10h
Merged PRs (30d)
36

Description

I've been experiencing a number of issues trying to get the Razor View Engine working in Azure Functions (v2 using 1.0.24 of the c# sdk and 2.4.317 of the cli tools), and have narrowed down the issues to what is likely an issue or number of issues in the build process. I've created a repo project that contains both a functions project and a console project calling the same code from a class library to different results: https://github.com/Doddler/FunctionsRazorTest

The first issue is, when attempting to compile a razor view, you'll encounter a wall of errors suggesting basic predefined types (string, object, type, etc) are not defined. This can be encountered in the console app as well in certain circumstances, but the solution to that is to set this property in your csproject:

```true```

As I understand it, this option generates additional details in the AssemblyName.deps.json that is used by Razor. This doesn't solve the issue in the Functions project, but in testing I found that I could fix the errors by moving the .deps.json file into the bin directory (to clarify, the bin directory located "ProjectName\bin\Debug\netcoreapp2.2\bin\"). The rendering still fails with a different error (Cannot find compilation library location for package), and I've yet to solve this one, but it's clear that the file isn't where it's being looked for.

To work around the rendering issue, I decided to explore precompiled views using a class library, but encountered a very similar issue. A razor class library generates two files, Libraryname.dll and Libraryname.Views.dll. During the build process, the latter file does not get moved into the bin directory and hence not visible to the function app. Manually moving the views dll into the bin directory and re-running results in the view finally being rendered correctly.

To sum up, some files aren't being found because they aren't being moved into the bin directory on compile.

(I apologize if this is the incorrect place to file this, it wasn't clear which project I should report this in)

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue with the linked Functions project, console project, and class library, comparing their build outputs and the nested bin directory. Inspect the .csproj settings, AssemblyName.deps.json placement, and generated Razor class-library views DLL. Done means the required compilation metadata and views assembly are present in the function app output and Razor rendering works without manual file moves.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, csharp
Domain
backend, build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.