-
Notifications
You must be signed in to change notification settings - Fork 445
/
Copy pathStartup.cs
93 lines (81 loc) · 2.5 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using App.Clients;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
namespace App
{
/// <summary>
/// 启动页
/// </summary>
public class Startup
{
/// <summary>
/// 获取配置
/// </summary>
public IConfiguration Configuration { get; }
/// <summary>
/// 环境
/// </summary>
public IWebHostEnvironment Environment { get; }
/// <summary>
/// 构造器
/// </summary>
/// <param name="configuration"></param>
/// <param name="environment"></param>
public Startup(IConfiguration configuration, IWebHostEnvironment environment)
{
Configuration = configuration;
Environment = environment;
}
/// <summary>
/// 添加服务
/// </summary>
/// <param name="services"></param>
public void ConfigureServices(IServiceCollection services)
{
// 添加控制器
services.AddControllers().AddXmlSerializerFormatters();
// 全局默认配置
services
.AddWebApiClient()
.UseJsonFirstApiActionDescriptor();
// 注册userApi
services.AddHttpApi(typeof(IUserApi)).ConfigureHttpApi(o =>
{
o.HttpHost = new Uri("http://localhost:5000/");
});
// 注册与配置clientId模式的token提者选项
services.AddClientCredentialsTokenProvider<IUserApi>().Configure(o =>
{
o.Endpoint = new Uri("http://localhost:5000/api/tokens");
o.Credentials.Client_id = "clientId";
o.Credentials.Client_secret = "xxyyzz";
});
// userApi客户端后台服务
services.AddScoped<UserService>().AddHostedService<UserHostedService>();
}
/// <summary>
/// 配置中间件
/// </summary>
/// <param name="app"></param>
public void Configure(IApplicationBuilder app)
{
if (Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}