Tidy up - Unused Project and Nuget package reference using Visual Studio 2019

If you are a Developer/Architect using Visual Studio as IDE for your development activities, this blog post will be of your interest. During the Ignite 2021 conference, Microsoft released Visual Studio 2019 v16.9 and v16.10 Preview 1. As part of version 16.10 Preview 1, one of the cool features they introduced is to "Remove Unused References..." for any Projects and Nuget packages that are not in use. At the time of writing this blog post, we have Visual Studio Version 16.10.0 (official release) which includes this new feature.  As part of development, we generally get carried away and introduce new Nuget package references to your project and add new references to your Projects. By the end of development, you will not be 100% sure which are not being referenced and unused which means you will leave those unused project references in your application. Now you might be wondering what's the big deal in it since it doesn't harm. The advantage of removing unused project r

Azure Function in a Docker Container - Part 1

From Azure Function v2, .NET developers can now write functions using .NET Core. This means you can develop and run your Azure functions in more places (cross-platform). This opens up opportunities for running your Azure function in Docker container and taking it one step ahead by running Azure Functions on Kubernetes with KEDA (Kubernetes-based Event Driven Autoscaling). In this blog post, we will see how to run Azure Function in Docker Container.    

Now few questions arise like why do we need to run in a container, do we lose the benefit of Serverless because you will be paying only for the time your function is running based on your plan in Azure Function, etc. All these questions are valid but as soon as we enter the world of containerization and Kubernetes we want our application to be flexible enough to be hosted in an on-premises/cloud/hybrid environment. With containerization now you can use Azure Functions as your event-driven host regardless of the environment. By having Azure Function in a docker container it enables options for hosting in different environments.

Pre-requisites

Build the Azure Functions container image and test locally

1. In this demo we will Create Azure Functions with HttpTriggered template and to support cross-platform we will be creating Azure Function v2 and v3 versions. You can similarly create new Azure Functions using Azure CLI commands e.g. func new
2. Create a Docker file for the respective version of Azure Function using following command.
  • Navigate to Azure Functions Project folder
  • Using the CLI tool/cmd prompt/PowerShell terminal use the following command
func init --docker-only --worker-runtime dotnet --chsarp    
 In the above command flag --docker tells Azure Function Core Tool to generate the Dockerfile
  • By default, the Docker file is created for Azure Function v2 even if you are using for Azure function v3 version. In that case please update the Docker file with the appropriate image version as shown in the below code snippet.
Azure Function v2 version of Docker file:
FROM microsoft/dotnet:2.2-sdk AS installer-env

COPY . /src/dotnet-function-app
RUN cd /src/dotnet-function-app && \
    mkdir -p /home/site/wwwroot && \
    dotnet publish *.csproj --output /home/site/wwwroot

# To enable ssh & remote debugging on app service change the base image to the one below
# FROM mcr.microsoft.com/azure-functions/dotnet:2.0-appservice 
FROM mcr.microsoft.com/azure-functions/dotnet:2.0
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]

Azure Function v3 version of Docker file:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS installer-env

COPY . /src/dotnet-function-app
RUN cd /src/dotnet-function-app && \
    mkdir -p /home/site/wwwroot && \
    dotnet publish *.csproj --output /home/site/wwwroot

# To enable ssh & remote debugging on app service change the base image to the one below
# FROM mcr.microsoft.com/azure-functions/dotnet:3.0-appservice 
FROM mcr.microsoft.com/azure-functions/dotnet:3.0
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]
  • Build Azure Function docker images using the below commands by navigating to the docker file folder.
docker build -t azurefuncv3 .
  • From the above step docker image is built locally, now let's see what is the command for running it locally.
 docker run --rm -it -p 7071:80/tcp azurefuncv3:latest


Now you should be able to browse the HttpTriggered endpoint Azure function which is running inside a container.


Now you might be interested in how to run it on Azure, check out this post Azure Function in a Docker Container - Part 2

Comments

Popular posts from this blog

Tidy up - Unused Project and Nuget package reference using Visual Studio 2019

Swagger UI for Azure Function v2 & v3 APIs

Authenticate Azure Functions - API Keys