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
-
Change directories into your locally cloned repository.
$ cd ~/code/HelloAzure/
-
Create a new .NET Core solution.
$ dotnet new solution -n HelloAzure -o
- The
dotnet new solution
command creates a new solution. - The
-n HelloAzure
flag is the name of the solution. - The
-o .
flag specifies where to output the results of the create operation, in this case the current directory.
- The
Creating the Azure Function Project
-
From your root directory create a new azure function project .
$ func init HelloAzure.Functions
- When prompted choose the dotnet runtime.
- This will create an empty Azure Functions project and place it in a directory named
HelloAzure.Functions
.- The azure-core-tools-cli will replace
.
chars with_
so the generated csproj file will beHelloAzure_Functions.csproj
.
- The azure-core-tools-cli will replace
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
-
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
-
From your root directory create a new shared library to contain business logic.
$ dotnet new classlib -n HelloAzure.BusinessLogic
- The
dotnet new classlib
command creates a new class library. - The
-n HelloAzure.BusinessLogic
flag is the name of the class library.
- The
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
-
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.
-
Change directories into the BusinessLogic project
$ cd HelloAzure.BusinessLogic/
-
Rename the
Class1.cs
file toGreetingService.cs
$ mv Class1.cs GreetingService.cs
-
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
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)
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_guideI sure can! Thanks for the feedback! That's super useful info :) .