Hello everyone,
Today’s focus is on ‘Creating Virtual Applications in Azure Web Apps.’ I’ll be guiding you through the process of deploying multiple web applications or APIs to a single Azure Web App using Virtual Applications. While similar setups can be achieved with IIS, let’s explore how Azure Web Apps streamline this process for optimal efficiency.
The Process
I have already deployed my Web App and it’s based on .NET 7 , This is a clean web app without any other applications.
Azure Web App
We need to configure a view settings in the web app. On the left hand side select “Configuration“.
You will be presented with the Configuration page. Select “Path Mappings” as shown in the image below.
Next click on “New virtual application or directory”
you will then be presented with the “New application or directory” Page. In the below image you will see that I have typed in “/api2” for the Virtual Path, and “site\api2” for the Physical Path. you will need to change this to your application name or if you are following along with this post then change this to be api1 and then do the same again for api2.
you also need to make sure that the “Directory” check box is not checked, it should be like I have shown in the image. Then click on the “OK” button.
The below image shows what the end result should look like.
Enabling Basic Authentication and FTP
Now I’m jumping a head slightly but I’ve enabled Basic Auth and FTP credentials (I don’t need to actually use FTP but I wanted to show you how to enable this if you need it). I needed to enable basic auth as I will be deploying my code using Visual studio 2022.
Ok lets get back to Web apps. The below image shows how to enable this. You will need to click on “General Settings” tab. and enable Basic Auth and FTP if you need it like the below image shows.
Creating the Virtual Application Directories
OK, now we need to create the directories. I’m not sure why Microsoft does not do this automatically when we configure the settings for our virtual application but it is what it is. To do this we need to Click on “Advanced Tools” which is located on the left hand menu.
Once clicked you will be presented with the following page. you will then need to click on “Go” I’ve indicated this with a red underline.
The link will open in a new Tab. and you will see the homepage as shown in the below image. from the Top navigation menu you will need to click on “Debug Console” and then from the drop down select “PowerShell“.
You will then see the page as shown below. From the folder view you will then need to click on “site”. again I have shown this with a red line.
from within this folder we now need to create two folders one for api1 and api2. To do this first select the Plus icon and then select “New folder“.
you will need to type in the name of the folder in my case this was first “api1” and the doing the same process again I created one for “api2“. The end result should look like the below image.
Publishing from Visual Studio 2022
OK. That’s all that is needed on the Azure Web App side of things. Now we can use Visual studio to publish our code. To start with I created two new web api projects based on .NET 7 using Visual studio, these are the basic templates that get created. I created a new publish profile based on Azure as shown below.
I followed the wizard so I shall not show this here. Once the Publish profile has been created you will need to change some settings. To do this click on “Show all settings” on the newly created publish profile.
you will be shown the following page, from this view you will need to select “Connection” as shown in the below image.
you will need to change the “Site name:” to have one the end the name of the virtual application, in my case this would be “api1” and for the other project would be “api2”. The below images shown the before and after change has been made.
Before
After
now once you have done you will need to click on the “Save” button.
Time to Publish
now we can publish our apps. To do this you will need to click the “Publish” button.
Just wait whilst Visual Studio does its publish.
Once the publish is done, I went to the URL in question: virtualappdemo.azurewebsites.net/api1
I got an error 404 page which is expected since this is an API If this was a website then the homepage would have loaded, to solve this I need to visit the endpoint directly. in my case this was the following: virtualappdemo.azurewebsites.net/api1/weatherforecast
Publishing the Second App
To publish the other app you just need to do the same again as we did above. But this time I was presented with the following error when I visited the URL: https://virtualappdemo.azurewebsites.net/api2
1 |
HTTP Error 500.35 - ANCM Multiple In-Process Applications in same Process |
to solve this I added the following to my .csproj file for the second project.
1 |
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel> |
should look like the below.
1 2 3 4 5 6 7 |
<PropertyGroup> <TargetFramework>net7.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> <!--added the below line--> <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel> </PropertyGroup> |
I then did a re-publish.
I was then presented with a different error, this time the error was:
1 |
HTTP Error 500.34 - ANCM Mixed Hosting Models Not Supported |
The reason for this error is because you can’t have two different applications running in two different hosting models. Our first app is running the in-process model whilst our second app was running the out of process model.
to solve this I added the same code to project 1 .csproj file and re-published project1.
1 2 3 4 5 6 7 |
<PropertyGroup> <TargetFramework>net7.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> <!--added the below line--> <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel> </PropertyGroup> |
Once the publish was done I was then able to visit virtualappdemo.azurewebsites.net/api2/weatherforecast
That’s all folks. I have added some links to further reading on Azure Web apps.
Disable basic authentication In web apps
https://azure.microsoft.com/en-gb/products/app-service/web