/
Startup.cs
151 lines (130 loc) · 5.97 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
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
using System;
using System.IO;
using System.Linq;
using System.Threading;
using Abp.AspNetCore;
using Abp.AspNetCore.Configuration;
using Abp.AspNetCore.Mvc.Antiforgery;
using Abp.AspNetCore.Mvc.Extensions;
using Abp.Castle.Logging.Log4Net;
using Abp.Dependency;
using Abp.Json;
using Abp.PlugIns;
using AbpAspNetCoreDemo.Controllers;
using AbpAspNetCoreDemo.Core.Domain;
using Castle.Core.Logging;
using Castle.Facilities.Logging;
using Castle.MicroKernel.ModelBuilder.Inspectors;
using Castle.MicroKernel.SubSystems.Conversion;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.OData;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OData.ModelBuilder;
using Newtonsoft.Json.Serialization;
namespace AbpAspNetCoreDemo
{
public class Startup
{
private readonly IWebHostEnvironment _env;
public static readonly AsyncLocal<IocManager> IocManager = new AsyncLocal<IocManager>();
public Startup(IWebHostEnvironment env)
{
_env = env;
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddSingleton(Configuration);
//Some test classes
services.AddTransient<MyTransientClass1>();
services.AddTransient<MyTransientClass2>();
services.AddScoped<MyScopedClass>();
//Add framework services
services.AddMvc(options =>
{
options.Filters.Add(new AbpAutoValidateAntiforgeryTokenAttribute());
}).AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new AbpMvcContractResolver(IocManager.Value)
{
NamingStrategy = new CamelCaseNamingStrategy()
};
}).AddRazorRuntimeCompilation().AddOData(opts =>
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Product>("Products").EntityType.Expand().Filter().OrderBy().Page().Select();
builder.EntitySet<Product>("ProductsDto").EntityType.Expand().Filter().OrderBy().Page().Select();
var edmModel = builder.GetEdmModel();
opts.AddRouteComponents("odata", edmModel);
});
//Configure Abp and Dependency Injection. Should be called last.
return services.AddAbp<AbpAspNetCoreDemoModule>(options =>
{
options.IocManager = IocManager.Value ?? new IocManager();
string plugDllInPath = "";
#if DEBUG
plugDllInPath = Path.Combine(_env.ContentRootPath,
@"..\AbpAspNetCoreDemo.PlugIn\bin\Debug\net6.0\AbpAspNetCoreDemo.PlugIn.dll");
#else
plugDllInPath = Path.Combine(_env.ContentRootPath,
@"..\AbpAspNetCoreDemo.PlugIn\bin\Release\net6.0\AbpAspNetCoreDemo.PlugIn.dll");
#endif
if (!File.Exists(plugDllInPath))
{
throw new FileNotFoundException("There is no plugin dll file in the given path.", plugDllInPath);
}
options.PlugInSources.Add(new AssemblyFileListPlugInSource(plugDllInPath));
//Configure Log4Net logging
options.IocManager.IocContainer.AddFacility<LoggingFacility>(
f => f.UseAbpLog4Net().WithConfig("log4net.config")
);
var propInjector = options.IocManager.IocContainer.Kernel.ComponentModelBuilder
.Contributors
.OfType<PropertiesDependenciesModelInspector>()
.Single();
options.IocManager.IocContainer.Kernel.ComponentModelBuilder.RemoveContributor(propInjector);
options.IocManager.IocContainer.Kernel.ComponentModelBuilder.AddContributor(new AbpPropertiesDependenciesModelInspector(new DefaultConversionManager()));
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
app.UseAbp(); //Initializes ABP framework. Should be called first.
// Return IQueryable from controllers
app.UseUnitOfWork(options =>
{
options.Filter = httpContext => httpContext.Request.Path.Value.StartsWith("/odata");
});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseEmbeddedFiles(); //Allows to expose embedded files to the web!
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("defaultWithArea", "{area}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
app.ApplicationServices.GetRequiredService<IAbpAspNetCoreConfiguration>().EndpointConfiguration.ConfigureAllEndpoints(endpoints);
});
}
}
}