Browse Source

Merge pull request #5012 from abpframework/liangshiwei/docs-patch-1

Update console application startup template document
pull/5022/head
Halil İbrahim Kalkan 6 years ago
committed by GitHub
parent
commit
84e80e241c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 180
      docs/en/Getting-Started-Console-Application.md
  2. 8
      docs/en/Startup-Templates/Console.md
  3. BIN
      docs/en/images/basic-console-application-solution.png
  4. 123
      docs/zh-Hans/Getting-Started-Console-Application.md
  5. 10
      docs/zh-Hans/Startup-Templates/Console.md
  6. BIN
      docs/zh-Hans/images/basic-console-application-solution.png

180
docs/en/Getting-Started-Console-Application.md

@ -1,181 +1,3 @@
# 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-samples/tree/master/BasicConsoleApplication).
ABP provided console application startup template. See [Console Application Startup Template](../en/Startup-Templates/Console.md) document for more information.

8
docs/en/Startup-Templates/Console.md

@ -18,4 +18,10 @@ abp new Acme.MyConsoleApp -t console
`Acme.MyConsoleApp` is the solution name, like *YourCompany.YourProduct*. You can use single level, two-levels or three-levels naming.
###
## Solution Structure
After you use the above command to create a solution, you will have a solution like shown below:
![basic-console-application-solution](../images/basic-console-application-solution.png)
* `HelloWorldService` is a sample service that implements the `ITransientDependency` interface. It will be automatically registered to the [dependency injection](../Dependency-Injection.md) system.

BIN
docs/en/images/basic-console-application-solution.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

123
docs/zh-Hans/Getting-Started-Console-Application.md

@ -1,124 +1,3 @@
## 在控制台应用中使用ABP
本教程将介绍如何从头开始以最小的依赖关系启动ABP. 你通常希望以 **[启动模板](https://abp.io/Templates)** 开头.
### 创建一个新项目
使用Visual Studio创建一个新的.Net Core Console应用程序:
![](images/create-new-net-core-console-application.png)
### 安装 Volo.Abp 包
Volo.Abp.Core是创建基于ABP的应用程序的核心nuget包. 所以,将它安装到你的项目中:
````
Install-Package Volo.Abp.Core
````
### 创建第一个ABP模块
ABP是一个模块化框架, 它需要一个从``AbpModule``类派生的 **启动(根)模块** 类:
````C#
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
namespace AbpConsoleDemo
{
public class AppModule : AbpModule
{
}
}
````
``AppModule`` 是应用程序启动模块的好名称.
### 初始化应用程序
下一步是使用上面创建的启动模块引导应用程序:
````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`` 用于创建应用程序并加载所有以``AppModule``作为启动模块的模块. ``Initialize()``方法启动应用程序.
### Hello World!
上面的应用程序什么都不做, 让我们创建一个服务做一些事情:
````C#
using System;
using Volo.Abp.DependencyInjection;
namespace AbpConsoleDemo
{
public class HelloWorldService : ITransientDependency
{
public void SayHello()
{
Console.WriteLine("Hello World!");
}
}
}
````
``ITransientDependency``是ABP的一个特殊接口, 它自动将服务注册为**Transient**(参见[依赖注入文档](Dependency-Injection.md)).
现在,我们可以解析``HelloWorldService``并调用``SayHello``. 更改Program.cs, 如下所示:
````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();
// 解析服务并使用它
var helloWorldService =
application.ServiceProvider.GetService<HelloWorldService>();
helloWorldService.SayHello();
Console.WriteLine("Press ENTER to stop application...");
Console.ReadLine();
}
}
}
}
````
这对于这个简单的代码示例已足够, 如果是直接从``IServiceProvider``解析建议创建**Scoped**依赖.(参见[依赖注入文档](Dependency-Injection.md)).
### 源码
从[这里](https://github.com/abpframework/abp-samples/tree/master/BasicConsoleApplication)获取本教程中创建的示例项目的源代码.
ABP提供了控制台应用程序启动模板. 参阅[控制台应用程序启动模板]文档了解更多信息.

10
docs/zh-Hans/Startup-Templates/Console.md

@ -16,4 +16,12 @@ dotnet tool install -g Volo.Abp.Cli
abp new Acme.MyConsoleApp -t console
````
`Acme.MyConsoleApp` 是解决方案的名称, 如*YourCompany.YourProduct*. 你可以使用单级或多级名称.
`Acme.MyConsoleApp` 是解决方案的名称, 如*YourCompany.YourProduct*. 你可以使用单级或多级名称.
## 解决方案结构
使用以上命令创建解决方案后,你会得到如下所示的解决方案:
![basic-console-application-solution](../images/basic-console-application-solution.png)
* `HelloWorldService` 是一个实现了 `ITransientDependency` 接口的示例服务. 它会自动注册到[依赖注入](../Dependency-Injection.md)系统.

BIN
docs/zh-Hans/images/basic-console-application-solution.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Loading…
Cancel
Save