DEV Community

Jacob Smith
Jacob Smith

Posted on

Introduction to Azure Functions Pt. 1 - Creating the .NET Core Solution

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. 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.

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.

    $ cd ~/code/HelloAzure/
    
  2. Create a new .NET Core solution.

    $ 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 .

    $ 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.

    $ 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.

    $ 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.

    $ 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

    $ cd HelloAzure.BusinessLogic/
    
  2. Rename the Class1.cs file to GreetingService.cs

    $ mv Class1.cs GreetingService.cs
    
  3. In a text editor replace the contents of GreetingService.cs with the following.

    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
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (2)

Collapse
 
booyaa profile image
Mark Sta Ana

Jacob loving this series! Any chance you could add the series frontmatter tag to your series? It would automatically add a link to all your posts and remove the need to add next/prev links to your posts: dev.to/p/editor_guide

Collapse
 
jsmithdenverdev profile image
Jacob Smith

I sure can! Thanks for the feedback! That's super useful info :) .