Startup.cs
5.49 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
using System.ServiceModel;
using Autofac.Extensions.DependencyInjection;
using Infrastructure;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Quartz.AspNetCore;
using SoapCore;
using Swashbuckle.AspNetCore.Swagger;
using WebApp;
using WebMvc.Areas.WebService;
using WebRepository;
namespace WebMvc
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.Configure<FormOptions>(options =>
{
options.ValueCountLimit = int.MaxValue;
options.ValueLengthLimit = int.MaxValue;
options.KeyLengthLimit = int.MaxValue;
options.MultipartBodyLengthLimit = int.MaxValue;
options.MultipartBoundaryLengthLimit = int.MaxValue;
});
services.AddMvc(option =>
{
option.ModelBinderProviders.Insert(0, new JsonBinderProvider());
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddMemoryCache();
services.AddOptions();
//映射配置文件
services.Configure<AppSetting>(Configuration.GetSection("AppSetting"));
//解密数据库连接字符串的密码
SqlConnectionStringBuilder sqlConnectionStringBuilder = new SqlConnectionStringBuilder(Configuration.GetConnectionString("BaseDBContext"));
sqlConnectionStringBuilder.Password = Encryption.Decrypt(sqlConnectionStringBuilder.Password);
services.AddDbContext<BaseDBContext>(options =>
//SQL2008启用分页支持
options.UseSqlServer(sqlConnectionStringBuilder.ConnectionString, b => b.UseRowNumberForPaging())
);
#region 启用Quartz中间件
services.AddQuartz(options =>
{
options.UseSqlServer(sqlConnectionStringBuilder.ConnectionString);
options.UseProperties(false);
});
#endregion
//注册数据库基础操作和工作单元
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
services.AddScoped(typeof(IUnitWork), typeof(UnitWork));
services.AddScoped(typeof(ISqlWork), typeof(SqlWork));
//WebService soap服务建立
services.TryAddSingleton<ISampleService, SampleService>();
#region 添加Swagger中间件
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new Info
{
Version = "v1",
Title = "华恒API",
Description = "华恒API接口",
});
//注入WebAPI注释文件给Swagger
var xmlPath = Path.Combine(AppContext.BaseDirectory, "WebMvc.xml");
options.IncludeXmlComments(xmlPath, true);
var assembly = Assembly.GetAssembly(typeof(BaseDBContext));
options.IncludeXmlComments(assembly.Location.Replace("dll", "xml"));
options.IgnoreObsoleteActions();
});
#endregion
services.AddSignalR();
//WebService soap服务建立
services.AddSoapCore();
//使用AutoFac进行注入
return new AutofacServiceProvider(AutofacExt.InitAutofac(services));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
#region 启用Swagger中间件
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.ShowExtensions();
c.SwaggerEndpoint("/swagger/v1/swagger.json", "华恒API接口文档");
c.InjectStylesheet("/css/swagger.css");
c.DisplayOperationId();
c.DisplayRequestDuration();
c.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.List);
});
#endregion
#region 启用Quartz中间件
app.UseQuartz();
#endregion
app.UseMvcWithDefaultRoute();
app.UseSignalR(routes =>
{
routes.MapHub<ChartHub>("/ChartHub");
});
//WebService soap服务建立
app.UseSoapEndpoint<ISampleService>("/Service.svc", new BasicHttpBinding(), SoapSerializer.DataContractSerializer);
app.UseSoapEndpoint<ISampleService>("/Service.asmx", new BasicHttpBinding(), SoapSerializer.XmlSerializer);
app.UseMvc(routes =>
{
routes.MapRoute(
name: "areas",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);
});
}
}
}