Introduction to Azure Functions Pt. 1 - Creating the .NET Core Solution
Tags: azure, serverless, dotnet, devops
Published: 2019-11-11 17:01:38

---
title: Introduction to Azure Functions Pt. 1 - Creating the .NET Core Solution
published: true
description: 
tags: azure, serverless, dotnet, devops
series: Introduction to Azure Functions
---

## Introduction
In this post we will be creating a .NET Core solution, a class library to hold shared business logic, and an Azure Functions project. We will also link both projects to the solution, and add some basic business logic to our class library.

## Prerequisites
To create the .NET Core solution and the business logic class library we will be using the dotnet cli which is included in the .NET Core SDK. We will be using version 2.1 of the SDK. The SDK installer can be found [here](https://dotnet.microsoft.com/download). To create the Azure Function project we will be using the azure-functions-core-tools cli. Instructions for installing the CLI can be found [here](https://github.com/Azure/azure-functions-core-tools#installing).

We will be using the command line to build out the solution and its projects, and to link them together. Some basic knowledge of the command line will be useful. 

Shell commands will be prefixed with `$`.

This wiki assumes that you have created a repository in a source control provider such as GitHub and that you have cloned that repository locally.

## Creating the .NET Core Solution
1. Change directories into your locally cloned repository. 
    ```sh
    $ cd ~/code/HelloAzure/
    ```
2. Create a new .NET Core solution.
    ```sh
    $ dotnet new solution -n HelloAzure -o
    ```
    1. The `dotnet new solution` command creates a new solution.
    2. The `-n HelloAzure` flag is the name of the solution.
    3. The `-o .` flag specifies where to output the results of the create operation, in this case the current directory.

## Creating the Azure Function Project
1. From your root directory create a new azure function project .
    ```sh
    $ func init HelloAzure.Functions
    ```
    1. When prompted choose the dotnet runtime.
    2. This will create an empty Azure Functions project and place it in a directory named `HelloAzure.Functions`.
        1. The azure-core-tools-cli will replace `.` chars with `_` so the generated csproj file will be `HelloAzure_Functions.csproj`. 

**Note**. The name of the project can be whatever you would like. I went with naming it `Functions` and name-spacing it under the `HelloAzure` solution.

### Linking the Azure Function Project
1. From your root directory link the Azure Function Project to the solution.
    ```sh
    $ dotnet sln add HelloAzure.Functions/HelloAzure_Functions.csproj
    ```

## Creating a business logic class library
1. From your root directory create a new shared library to contain business logic.
    ```sh
    $ dotnet new classlib -n HelloAzure.BusinessLogic
    ```
    1. The `dotnet new classlib` command creates a new class library.
    2. The `-n HelloAzure.BusinessLogic` flag is the name of the class library.

**Note**. The name of the project can be whatever you would like. I went with naming it `BusinessLogic` and name-spacing it under the `HelloAzure` solution.

### Linking the business logic class library
1. From your root directory link the business logic class library to the solution.
    ```sh
    $ dotnet sln add HelloAzure.BusinessLogic/HelloAzure.BusinessLogic.csproj
    ```

### Adding some "business logic" logic to the BusinessLogic library
The BusinessLogic class library is stubbed out with an empty class named `Class1.cs`. We need to rename it and give it some actual business logic to handle.

1. Change directories into the BusinessLogic project
    ```sh
    $ cd HelloAzure.BusinessLogic/
    ```
2. Rename the `Class1.cs` file to `GreetingService.cs`
    ```sh
    $ mv Class1.cs GreetingService.cs
    ```
3. In a text editor replace the contents of `GreetingService.cs` with the following.
    ```csharp
    namespace HelloAzure.BusinessLogic
    {
        public class GreetingService
        {
            public string GetGreeting(string name)
            {
                return $"Hello, {name}";
            }
        }
    }
    ```

## Recap
In this post we created the following.

- A .NET Core solution.
- An Azure Functions project.
- A BusinessLogic class library.

We also added links from each project to the solution.

Finally we added some actual business logic to the BusinessLogic class library.

If you followed all the above steps you should have a project structure that looks like the following.

```
.
+-- .gitignore
+-- HelloAzure.sln
+-- HelloAzure.BusinessLogic/
|   +-- GreetingService.cs
|   +-- HelloAzure.BusinessLogic.csproj
+-- HelloAzure.Functions/
|   +-- .vscode/
|   +-- .gitignore
|   +-- HelloAzure_Functions.csproj
|   +-- host.json
|   +-- local.settings.json
```

Congrats on getting all the setup complete 😊 . We now have a solution that we can add functions to 🎉.

This would be a good time to commit the changes you've made to source.