mirror of https://github.com/abpframework/abp
parent
8c966c0829
commit
ed98b55e11
@ -0,0 +1,186 @@
|
|||||||
|
# Getting Started ABP With AspNet Core MVC Web Application
|
||||||
|
|
||||||
|
This tutorial explains how to start ABP from scratch with minimal dependencies. You generally want to start with the **[startup template](Getting-Started-AspNetCore-MVC-Template.md)**.
|
||||||
|
|
||||||
|
## Create A New Project
|
||||||
|
|
||||||
|
1. Create a new empty AspNet Core Web Application from Visual Studio:
|
||||||
|
|
||||||
|
![](images/create-new-aspnet-core-application.png)
|
||||||
|
|
||||||
|
2. Select Empty Template
|
||||||
|
|
||||||
|
![](images/select-empty-web-application.png)
|
||||||
|
|
||||||
|
You could select another template, but I want to show it from a clear project.
|
||||||
|
|
||||||
|
## Install Volo.Abp.AspNetCore.Mvc Package
|
||||||
|
|
||||||
|
Volo.Abp.AspNetCore.Mvc is AspNet Core MVC integration package for ABP. So, install it to your project:
|
||||||
|
|
||||||
|
````
|
||||||
|
Install-Package Volo.Abp.AspNetCore.Mvc
|
||||||
|
````
|
||||||
|
|
||||||
|
## Create First ABP Module
|
||||||
|
|
||||||
|
ABP is a modular framework and it requires a **startup (root) module** class derived from ``AbpModule``:
|
||||||
|
|
||||||
|
````C#
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Volo.Abp;
|
||||||
|
using Volo.Abp.AspNetCore.Modularity;
|
||||||
|
using Volo.Abp.AspNetCore.Mvc;
|
||||||
|
using Volo.Abp.Modularity;
|
||||||
|
|
||||||
|
namespace BasicAspNetCoreApplication
|
||||||
|
{
|
||||||
|
[DependsOn(typeof(AbpAspNetCoreMvcModule))]
|
||||||
|
public class AppModule : AbpModule
|
||||||
|
{
|
||||||
|
public override void OnApplicationInitialization(ApplicationInitializationContext context)
|
||||||
|
{
|
||||||
|
var app = context.GetApplicationBuilder();
|
||||||
|
var env = context.GetEnvironment();
|
||||||
|
|
||||||
|
if (env.IsDevelopment())
|
||||||
|
{
|
||||||
|
app.UseDeveloperExceptionPage();
|
||||||
|
}
|
||||||
|
|
||||||
|
app.UseMvcWithDefaultRoute();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
````
|
||||||
|
|
||||||
|
``AppModule`` is a good name for the startup module for an application.
|
||||||
|
|
||||||
|
ABP packages define module classes and a module can depend on another module. In the code above, our ``AppModule`` depends on ``AbpAspNetCoreMvcModule`` (defined by Volo.Abp.AspNetCore.Mvc package). It's common to add a ``DependsOn`` attribute after installing a new ABP nuget package.
|
||||||
|
|
||||||
|
Instead of Startup class, we are configuring ASP.NET Core pipeline in this module class.
|
||||||
|
|
||||||
|
## The Startup Class
|
||||||
|
|
||||||
|
Next step is to modify Startup class to integrate to ABP module system:
|
||||||
|
|
||||||
|
````C#
|
||||||
|
using System;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace BasicAspNetCoreApplication
|
||||||
|
{
|
||||||
|
public class Startup
|
||||||
|
{
|
||||||
|
public IServiceProvider ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddApplication<AppModule>();
|
||||||
|
|
||||||
|
return services.BuildServiceProviderFromFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Configure(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
app.InitializeApplication();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
````
|
||||||
|
|
||||||
|
Changed ``ConfigureServices`` method to return ``IServiceProvider`` instead of ``void``. This change allows us to replace AspNet Core's Dependency Injection with another framework (see Autofac integration section below). ``services.AddApplication<AppModule>()`` adds all services defined in all modules beginning from the ``AppModule``.
|
||||||
|
|
||||||
|
``app.InitializeApplication()`` call in ``Configure`` method initializes and starts the application.
|
||||||
|
|
||||||
|
## Hello World!
|
||||||
|
|
||||||
|
The application above does nothing. Let's create an MVC controller does something:
|
||||||
|
|
||||||
|
````C#
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Volo.Abp.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace BasicAspNetCoreApplication.Controllers
|
||||||
|
{
|
||||||
|
public class HomeController : AbpController
|
||||||
|
{
|
||||||
|
public IActionResult Index()
|
||||||
|
{
|
||||||
|
return Content("Hello World!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
````
|
||||||
|
|
||||||
|
If you run the application, you will see a "Hello World!" message on the page.
|
||||||
|
|
||||||
|
Derived ``HomeController`` from ``AbpController`` instead of standard ``Controller`` class. This is not required, but ``AbpController`` class has useful base properties and methods to make your development easier.
|
||||||
|
|
||||||
|
## Using Autofac as the Dependency Injection Framework
|
||||||
|
|
||||||
|
While AspNet Core's Dependency Injection (DI) system is fine for basic requirements, Autofac provides advanced features like Property Injection and Method Interception which are required by ABP to perform advanced application framework features.
|
||||||
|
|
||||||
|
Replacing AspNet Core's DI system by Autofac and integrating to ABP is pretty easy.
|
||||||
|
|
||||||
|
1. Install [Volo.Abp.Autofac](https://www.nuget.org/packages/Volo.Abp.Autofac) package
|
||||||
|
|
||||||
|
````
|
||||||
|
Install-Package Volo.Abp.Autofac
|
||||||
|
````
|
||||||
|
|
||||||
|
2. Add ``AbpAutofacModule`` Dependency
|
||||||
|
|
||||||
|
````C#
|
||||||
|
[DependsOn(typeof(AbpAspNetCoreMvcModule))]
|
||||||
|
[DependsOn(typeof(AbpAutofacModule))] //Add dependency to ABP Autofac module
|
||||||
|
public class AppModule : AbpModule
|
||||||
|
{
|
||||||
|
...
|
||||||
|
}
|
||||||
|
````
|
||||||
|
|
||||||
|
3. Change ``services.AddApplication<AppModule>();`` line in the ``Startup`` class as shown below:
|
||||||
|
|
||||||
|
````C#
|
||||||
|
services.AddApplication<AppModule>(options =>
|
||||||
|
{
|
||||||
|
options.UseAutofac(); //Integrate to Autofac
|
||||||
|
});
|
||||||
|
````
|
||||||
|
|
||||||
|
4. Update `Program.cs` to not use the `WebHost.CreateDefaultBuilder()` method since it uses the default DI container:
|
||||||
|
|
||||||
|
````csharp
|
||||||
|
public class Program
|
||||||
|
{
|
||||||
|
public static void Main(string[] args)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
https://github.com/aspnet/AspNetCore/issues/4206#issuecomment-445612167
|
||||||
|
CurrentDirectoryHelpers exists in: \framework\src\Volo.Abp.AspNetCore.Mvc\Microsoft\AspNetCore\InProcess\CurrentDirectoryHelpers.cs
|
||||||
|
Will remove CurrentDirectoryHelpers.cs when upgrade to ASP.NET Core 3.0.
|
||||||
|
*/
|
||||||
|
CurrentDirectoryHelpers.SetCurrentDirectory();
|
||||||
|
|
||||||
|
BuildWebHostInternal(args).Run();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IWebHost BuildWebHostInternal(string[] args) =>
|
||||||
|
new WebHostBuilder()
|
||||||
|
.UseKestrel()
|
||||||
|
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||||
|
.UseIIS()
|
||||||
|
.UseIISIntegration()
|
||||||
|
.UseStartup<Startup>()
|
||||||
|
.Build();
|
||||||
|
}
|
||||||
|
````
|
||||||
|
|
||||||
|
## Source Code
|
||||||
|
|
||||||
|
Get source code of the sample project created in this tutorial from [here](https://github.com/abpframework/abp/tree/master/samples/BasicAspNetCoreApplication).
|
||||||
|
|
@ -0,0 +1,102 @@
|
|||||||
|
## Getting Started With the ASP.NET Core MVC Template
|
||||||
|
|
||||||
|
This tutorials explains how to create a new ASP.NET Core MVC web application using the startup template, configure and run it.
|
||||||
|
|
||||||
|
### Creating a New Project
|
||||||
|
|
||||||
|
This tutorial uses **ABP CLI** to create a new project. See the [Get Started](https://abp.io/get-started) page for other options.
|
||||||
|
|
||||||
|
Install the ABP CLI using a command line window, if you've not installed before:
|
||||||
|
|
||||||
|
````bash
|
||||||
|
dotnet tool install -g Volo.Abp.Cli
|
||||||
|
````
|
||||||
|
|
||||||
|
Use `abp new` command in an empty folder to create your project:
|
||||||
|
|
||||||
|
````bash
|
||||||
|
abp new Acme.BookStore
|
||||||
|
````
|
||||||
|
|
||||||
|
> You can use different level of namespaces; e.g. BookStore, Acme.BookStore or Acme.Retail.BookStore.
|
||||||
|
|
||||||
|
`new` command creates a **layered MVC application** with **Entity Framework Core** as the database provider. However, it has additional options. See the [CLI documentation](CLI.md) for all available options.
|
||||||
|
|
||||||
|
#### Pre Requirements
|
||||||
|
|
||||||
|
The created solution requires;
|
||||||
|
|
||||||
|
* [Visual Studio 2017 (v15.9.0+)](https://visualstudio.microsoft.com/tr/downloads/)
|
||||||
|
* [.NET Core 2.2+](https://www.microsoft.com/net/download/dotnet-core/)
|
||||||
|
|
||||||
|
### The Solution Structure
|
||||||
|
|
||||||
|
Open the solution in **Visual Studio**:
|
||||||
|
|
||||||
|
![bookstore-visual-studio-solution](images/bookstore-visual-studio-solution-v3.png)
|
||||||
|
|
||||||
|
The solution has a layered structure (based on [Domain Driven Design](Domain-Driven-Design.md)) and contains unit & integration test projects properly configured to work with **EF Core** & **SQLite in-memory** database.
|
||||||
|
|
||||||
|
> See [MVC application template document](Startup-Templates/Mvc.md) to understand the solution structure in details.
|
||||||
|
|
||||||
|
### Database Connection String
|
||||||
|
|
||||||
|
Check the **connection string** in the `appsettings.json` file under the `.Web` project:
|
||||||
|
|
||||||
|
````json
|
||||||
|
{
|
||||||
|
"ConnectionStrings": {
|
||||||
|
"Default": "Server=localhost;Database=BookStore;Trusted_Connection=True"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
````
|
||||||
|
|
||||||
|
The solution is configured to use **Entity Framework Core** with **MS SQL Server**. EF Core supports [various](https://docs.microsoft.com/en-us/ef/core/providers/) database providers, so you can use another DBMS if you want. Change the connection string if you need.
|
||||||
|
|
||||||
|
### Create Database & Apply Database Migrations
|
||||||
|
|
||||||
|
You have two options to create the database.
|
||||||
|
|
||||||
|
#### Using the DbMigrator Application
|
||||||
|
|
||||||
|
The solution contains a console application (named `Acme.BookStore.DbMigrator` in this sample) that can create database, apply migrations and seed initial data. It is useful on development as well as on production environment.
|
||||||
|
|
||||||
|
> `.DbMigrator` project has its own `appsettings.json`. So, if you have changed the connection string above, you should also change this one.
|
||||||
|
|
||||||
|
Right click to the `.DbMigrator` project and select **Set as StartUp Project**:
|
||||||
|
|
||||||
|
![set-as-startup-project](images/set-as-startup-project.png)
|
||||||
|
|
||||||
|
Hit F5 (or Ctrl+F5) to run the application. It will have an output like shown below:
|
||||||
|
|
||||||
|
![set-as-startup-project](images/db-migrator-app.png)
|
||||||
|
|
||||||
|
#### Using EF Core Update-Database Command
|
||||||
|
|
||||||
|
Ef Core has `Update-Database` command which creates database if necessary and applies pending migrations. Right click to the `.Web` project and select **Set as StartUp Project**:
|
||||||
|
|
||||||
|
![set-as-startup-project](images/set-as-startup-project.png)
|
||||||
|
|
||||||
|
Open the **Package Manager Console**, select `.EntityFrameworkCore.DbMigrations` project as the **Default Project** and run the `Update-Database` command:
|
||||||
|
|
||||||
|
![pcm-update-database](images/pcm-update-database-v2.png)
|
||||||
|
|
||||||
|
This will create a new database based on the configured connection string.
|
||||||
|
|
||||||
|
> Using the `.Migrator` tool is the suggested way, because it also seeds the initial data to be able to properly run the web application.
|
||||||
|
|
||||||
|
### Running the Application
|
||||||
|
|
||||||
|
Ensure that the `.Web` project is the startup project. Run the application which will open the **home** page in your browser:
|
||||||
|
|
||||||
|
![bookstore-homepage](images/bookstore-homepage.png)
|
||||||
|
|
||||||
|
Click the **Login** button, enter `admin` as the username and `1q2w3E*` as the password to login to the application.
|
||||||
|
|
||||||
|
The startup template includes the **identity management** and **tenant management** modules. Once you login, the Administration menu will be available where you can manage **tenants**, **roles**, **users** and their **permissions**. User management page is shown below:
|
||||||
|
|
||||||
|
![bookstore-user-management](images/bookstore-user-management-v2.png)
|
||||||
|
|
||||||
|
### What's Next?
|
||||||
|
|
||||||
|
* [Application development tutorial](Tutorials/AspNetCore-Mvc/Part-I.md)
|
@ -0,0 +1,181 @@
|
|||||||
|
# Getting Started ABP With Console Application
|
||||||
|
|
||||||
|
This tutorial explains how to start ABP from scratch with minimal dependencies. You generally want to start with a **[startup template](https://abp.io/Templates)**.
|
||||||
|
|
||||||
|
## Create A New Project
|
||||||
|
|
||||||
|
Create a new Regular .Net Core Console Application from Visual Studio:
|
||||||
|
|
||||||
|
![](images/create-new-net-core-console-application.png)
|
||||||
|
|
||||||
|
## Install Volo.Abp Package
|
||||||
|
|
||||||
|
Volo.Abp.Core is the core nuget package to create ABP based applications. So, install it to your project:
|
||||||
|
|
||||||
|
````
|
||||||
|
Install-Package Volo.Abp.Core
|
||||||
|
````
|
||||||
|
|
||||||
|
## Create First ABP Module
|
||||||
|
|
||||||
|
ABP is a modular framework and it requires a **startup (root) module** class derived from ``AbpModule``:
|
||||||
|
|
||||||
|
````C#
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Volo.Abp.Modularity;
|
||||||
|
|
||||||
|
namespace AbpConsoleDemo
|
||||||
|
{
|
||||||
|
public class AppModule : AbpModule
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
````
|
||||||
|
|
||||||
|
``AppModule`` is a good name for the startup module for an application.
|
||||||
|
|
||||||
|
## Initialize The Application
|
||||||
|
|
||||||
|
The next step is to bootstrap the application using the startup module created above:
|
||||||
|
|
||||||
|
````C#
|
||||||
|
using System;
|
||||||
|
using Volo.Abp;
|
||||||
|
|
||||||
|
namespace AbpConsoleDemo
|
||||||
|
{
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
using (var application = AbpApplicationFactory.Create<AppModule>())
|
||||||
|
{
|
||||||
|
application.Initialize();
|
||||||
|
|
||||||
|
Console.WriteLine("Press ENTER to stop application...");
|
||||||
|
Console.ReadLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
````
|
||||||
|
|
||||||
|
``AbpApplicationFactory`` is used to create the application and load all modules taking ``AppModule`` as the startup module. ``Initialize()`` method starts the application.
|
||||||
|
|
||||||
|
## Hello World!
|
||||||
|
|
||||||
|
The application above does nothing. Let's create a service that does something:
|
||||||
|
|
||||||
|
````C#
|
||||||
|
using System;
|
||||||
|
using Volo.Abp.DependencyInjection;
|
||||||
|
|
||||||
|
namespace AbpConsoleDemo
|
||||||
|
{
|
||||||
|
public class HelloWorldService : ITransientDependency
|
||||||
|
{
|
||||||
|
public void SayHello()
|
||||||
|
{
|
||||||
|
Console.WriteLine("Hello World!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
````
|
||||||
|
|
||||||
|
``ITransientDependency`` is a special interface of ABP that automatically registers the service as transient (see [dependency injection document](Dependency-Injection.md)).
|
||||||
|
|
||||||
|
Now, we can resolve the ``HelloWorldService`` and say hello. Change the Program.cs as shown below:
|
||||||
|
|
||||||
|
````C#
|
||||||
|
using System;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Volo.Abp;
|
||||||
|
|
||||||
|
namespace AbpConsoleDemo
|
||||||
|
{
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
using (var application = AbpApplicationFactory.Create<AppModule>())
|
||||||
|
{
|
||||||
|
application.Initialize();
|
||||||
|
|
||||||
|
//Resolve a service and use it
|
||||||
|
var helloWorldService =
|
||||||
|
application.ServiceProvider.GetService<HelloWorldService>();
|
||||||
|
helloWorldService.SayHello();
|
||||||
|
|
||||||
|
Console.WriteLine("Press ENTER to stop application...");
|
||||||
|
Console.ReadLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
````
|
||||||
|
|
||||||
|
While it's enough for this simple code example, it's always suggested to create scopes in case of directly resolving dependencies from ``IServiceProvider`` (see the [Dependency Injection documentation](Dependency-Injection.md)).
|
||||||
|
|
||||||
|
## Using Autofac as the Dependency Injection Framework
|
||||||
|
|
||||||
|
While AspNet Core's Dependency Injection (DI) system is fine for basic requirements, Autofac provides advanced features like Property Injection and Method Interception which are required by ABP to perform advanced application framework features.
|
||||||
|
|
||||||
|
Replacing AspNet Core's DI system by Autofac and integrating to ABP is pretty easy.
|
||||||
|
|
||||||
|
1. Install [Volo.Abp.Autofac](https://www.nuget.org/packages/Volo.Abp.Autofac) package
|
||||||
|
|
||||||
|
```
|
||||||
|
Install-Package Volo.Abp.Autofac
|
||||||
|
```
|
||||||
|
|
||||||
|
1. Add ``AbpAutofacModule`` Dependency
|
||||||
|
|
||||||
|
```c#
|
||||||
|
[DependsOn(typeof(AbpAutofacModule))] //Add dependency to the AbpAutofacModule
|
||||||
|
public class AppModule : AbpModule
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
1. Change ``Program.cs`` file as shown below:
|
||||||
|
|
||||||
|
```c#
|
||||||
|
using System;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Volo.Abp;
|
||||||
|
|
||||||
|
namespace AbpConsoleDemo
|
||||||
|
{
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
using (var application = AbpApplicationFactory.Create<AppModule>(options =>
|
||||||
|
{
|
||||||
|
options.UseAutofac(); //Autofac integration
|
||||||
|
}))
|
||||||
|
{
|
||||||
|
application.Initialize();
|
||||||
|
|
||||||
|
//Resolve a service and use it
|
||||||
|
var helloWorldService =
|
||||||
|
application.ServiceProvider.GetService<HelloWorldService>();
|
||||||
|
helloWorldService.SayHello();
|
||||||
|
|
||||||
|
Console.WriteLine("Press ENTER to stop application...");
|
||||||
|
Console.ReadLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Just called `options.UseAutofac()` method in the `AbpApplicationFactory.Create` options.
|
||||||
|
|
||||||
|
## Source Code
|
||||||
|
|
||||||
|
Get source code of the sample project created in this tutorial from [here](https://github.com/abpframework/abp/tree/master/samples/BasicConsoleApplication).
|
Loading…
Reference in new issue