Setup
Here you will learn how to setup your solution and projects to use Gazel.
- Create a Solution : Create a Gazel solution, add and configure your hosts.
- Add a Module : Learn how to add a business module to your solution.
Create a solution
In this document, you will learn how to setup a solution from scratch.
Create a blank solution
Create a solution folder for your application. Name it in the
<Company>.<Component>
convention (see .NET Naming Conventions ). Under
this folder create an empty solution.
mkdir Inventiv.Sample; cd Inventiv.Sampledotnet new sln
We will use Inventiv
as a company, and Sample
as a component throughout
this document.
Create a service application
To create a service application, first create a new web project by typing below commands;
dotnet new web --output src/Inventiv.Sample.App.Servicedotnet sln add src/Inventiv.Sample.App.Service --solution-folder src
Now Inventiv.Sample.sln
should have below structure;
Inventiv.Sample
└── src
└── Inventiv.Sample.App.Service
├── Properties
│ └── launchSettings.json
├── appsettings.json
├── appsettings.Developement.json
└── Program.cs
Now add Gazel.Configuration package to the App.Service
project from
nuget.org.
dotnet add src/Inventiv.Sample.App.Service package Gazel.Configuration
To configure this project to use Gazel, modify Program.cs
file in
src/Inventiv.Sample.App.Service
as below;
var builder = WebApplication.CreateBuilder(args);var configuration = builder.Configuration;builder.Host.UseGazelServiceProvider();builder.Services.AddGazelServiceApplication(configuration, database: c => c.Sqlite("gazel.tutorial.db"), service: c => c.Routine("http://localhost:5000/service"), logging: c => c.Log4Net(LogLevel.Information, l => l.DefaultConsoleAppenders()), authentication: c => c.AllowAnonymous(), authorization: c => c.AllowAll());var app = builder.Build();app.UseGazel();app.Run();
As you can see there are 5 configurations;
database: c => c.Sqlite("gazel.tutorial.db")
tells Gazel to use a localSQLite
database with a file namedgazel.tutorial.db
. This file will be located in default user documents folder.service: c => c.Routine("http://localhost:5000/service")
tells Gazel the service url base to itself to be able to make internal calls.logging: c => c.Log4Net(LogLevel.Info, l => l.DefaultConsoleAppenders())
tells Gazel to use console appenders in info levelauthentication: c => c.AllowAnonymous()
disables authentication feature.authorization: c => c.AllowAll()
disables authorization feature.
You need to configure your application port correctly. You can go to
Properties/launchSettings.json
and set it to 5000
;
{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:5000", "sslPort": 5001 } }, "profiles": { "Inventiv.Sample.App.Service": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } }}
Gazel scans bin directory to configure your classes automatically. To
understand which assembly is yours, Gazel uses assembly name of your app
project. If your host project is named Foo.Bar.App.Service
then it will
only scan assemblies with a name that starts with Foo
.
Create a Rest API application
Now create another web project and name it after
<Company>.<Component>.App.Rest
format;
dotnet new web --output src/Inventiv.Sample.App.Restdotnet sln add src/Inventiv.Sample.App.Rest --solution-folder src
Now your solution explorer should look like this;
Inventiv.Sample
└── src
├── Inventiv.Sample.App.Rest
│ ├── Properties
│ │ └── launchSettings.json
│ ├── appsettings.json
│ ├── appsettings.Developement.json
│ └── Program.cs
└── Inventiv.Sample.App.Service
Now add Gazel.Configuration package to the App.Rest
project from
nuget.org.
dotnet add src/Inventiv.Sample.App.Rest package Gazel.Configuration
To configure this project to use Gazel, modify Program.cs
file in
src/Inventiv.Sample.App.Rest
as below;
var builder = WebApplication.CreateBuilder(args);var configuration = builder.Configuration;builder.Host.UseGazelServiceProvider();builder.Services.AddGazelApiApplication(configuration, serviceClient: c => c.Routine("http://localhost:5000/service"), restApi: c => c.Standard(), logging: c => c.Log4Net(LogLevel.Information, l => l.DefaultConsoleAppenders()), authentication: c => c.AllowAnonymous());var app = builder.Build();app.UseGazel();app.Run();
As you can see there are 3 main configurations.
serviceClient: c => c.Routine("http://localhost:5000/service"))
tells Gazel to use previously created service application as a backend service.restApi: c => c.Standard()
tells Gazel to use standard rest configuration which uses AspNetCore.Mvc template.logging: c => c.Log4Net(LogLevel.Information, l => l.DefaultConsoleAppenders())
tells Gazel to use console appenders in info levelauthentication: c => c.AllowAnonymous()
disables authentication feature.
Now you've set up your environment to start developing a new backend. To add a new business module to your project, check out next section.
Add a module
In this document you will learn to create a new business module to your existing solution.
Create a new module
A module is a project that contains the domain logic of a specficic subset of
your backend, so choose a name accordingly. Naming convention for module
projects is <Company>.<Component>.Module.<ModuleName>
.
Now create a class library project under src
and add it to solution.
dotnet new classlib --output src/Inventiv.Sample.Module.Tododotnet sln add src/Inventiv.Sample.Module.Todo --solution-folder src
Check if your solution has new module under src
folder;
Inventiv.Sample
└── src
├── Inventiv.Sample.App.Rest
├── Inventiv.Sample.App.Service
└── Inventiv.Sample.Module.Todo
Add Gazel to your new module;
dotnet add src/Inventiv.Sample.Module.Todo package Gazel
Modules only use Gazel . Do NOT add Gazel.Configuration package to a module project.
Finally you need to add your module project reference to App.Service
so that
it renders and exposes business services in your new module.
dotnet add src/Inventiv.Sample.App.Service reference src/Inventiv.Sample.Module.Todo
Create a test project
To test your business services you need to create a test project. Naming
convention for test projects is <Company>.<Component>.Test.<ModuleName>
.
Add a new class library project under test
folder. Add
Gazel.Configuration package and module reference to the new project;
dotnet new nunit --output test/Inventiv.Sample.Test.Tododotnet sln add test/Inventiv.Sample.Test.Todo --solution-folder testdotnet add test/Inventiv.Sample.Test.Todo package Gazel.Configurationdotnet add test/Inventiv.Sample.Test.Todo reference src/Inventiv.Sample.Module.Todo
Your directory structure should look like this;
Inventiv.Sample
├── src
│ ├── Inventiv.Sample.App.Rest
│ ├── Inventiv.Sample.App.Service
│ └── Inventiv.Sample.Module.Todo
└── test
└── Inventiv.Sample.Test.Todo
└── Usings.cs
If your test project does not have Usings.cs
, you can add it manually.
Then add global using NUnit.Framework;
.
Summary
You've created a solution with Rest and Service applications, added Module and Test Project. After finishing this setup, directory structure should look like this;
Inventiv.Sample
├── src
│ ├── Inventiv.Sample.App.Rest
│ │ ├── Properties
│ │ │ └── launchSettings.json
│ │ ├── appsettings.json
│ │ ├── appsettings.Developement.json
│ │ └── Program.cs
│ ├── Inventiv.Sample.App.Service
│ │ ├── Properties
│ │ │ └── launchSettings.json
│ │ ├── appsettings.json
│ │ ├── appsettings.Developement.json
│ │ └── Program.cs
│ └── Inventiv.Sample.Module.Todo
└── test
└── Inventiv.Sample.Test.Todo
└── Usings.cs