Deploying ASP.NET Core to App Service on Linux

When you attempt to deploy an ASP.NET Core project to App Service on Linux, you may run into an error during the build process – Object reference not set to an instance of an object. When you try to deploy the project to App Service on Windows, everything works, so where is the issue?

So apparently, when you include a Solution (.sln) file with your project, Kudu is going to have a hard time to parse it on App Service on Linux since it requires to load some assemblies to parse the .sln, however those are not present on ASL yet. There are multiple ways to go around this:

You can either remove the .sln file from the whole repository, which will result in the build to succeed, the application will deploy and we are safe and sound, however, it will be harder to use Visual Studio, since it kind of requires the solution file to be present.

If you don’t want to do that, you can include a custom deployment script with your code by adding a .deployment file into the project’s root and specifying the deployment script in it:

1

2

[config]

command=deploy.sh

Then you need to also include the deploy.sh script along with the .deployment file, the script can look like this (this is what gets autogenerated when you don’t include the solution file, don’t forget to rename the WebApplication1 to your .csproj):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

#!/bin/bash

# ----------------------

# KUDU Deployment Script

# Version: 1.0.13

# ----------------------

# Helpers

# -------

exitWithMessageOnError(){

if[!$?-eq0];then

echo"An error has occurred during web site deployment."

echo$1

exit1

fi

}

# Prerequisites

# -------------

# Verify node.js installed

hash node2>/dev/null

exitWithMessageOnError"Missing node.js executable, please install node.js, if already installed make sure it can be reached from current environment."

# Setup

# -----

SCRIPT_DIR="${BASH_SOURCE[0]%\\*}"

SCRIPT_DIR="${SCRIPT_DIR%/*}"

ARTIFACTS=$SCRIPT_DIR/../artifacts

KUDU_SYNC_CMD=${KUDU_SYNC_CMD//\"}

if[[!-n"$DEPLOYMENT_SOURCE"]];then

DEPLOYMENT_SOURCE=$SCRIPT_DIR

fi

if[[!-n"$NEXT_MANIFEST_PATH"]];then

NEXT_MANIFEST_PATH=$ARTIFACTS/manifest

if[[!-n"$PREVIOUS_MANIFEST_PATH"]];then

PREVIOUS_MANIFEST_PATH=$NEXT_MANIFEST_PATH

fi

fi

if[[!-n"$DEPLOYMENT_TARGET"]];then

DEPLOYMENT_TARGET=$ARTIFACTS/wwwroot

else

KUDU_SERVICE=true

fi

if[[!-n"$KUDU_SYNC_CMD"]];then

# Install kudu sync

echo Installing Kudu Sync

npm install kudusync-g--silent

exitWithMessageOnError"npm failed"

if[[!-n"$KUDU_SERVICE"]];then

# In case we are running locally this is the correct location of kuduSync

KUDU_SYNC_CMD=kuduSync

else

# In case we are running on kudu service this is the correct location of kuduSync

And last, but not least, you can build the source code somewhere else, like in Visual Studio Team Services, and then just publish it to he App Service on Linux.

Additionally, it is quite important to mention, that you will need to set the startup command for the container in order for the application to start successfully, else it is going to attempt searching for .csproj in the root, which it won’t be able to find and therefore the container will keep crashing (you will see it in the logs). The startup command will look like this: dotnet /home/site/wwwroot/WebApplication1.dll