forked from KevinDockx/AspNetCoreAsyncAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
156 lines (116 loc) · 5.57 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
152
153
154
155
156
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Serialization;
using Microsoft.AspNetCore.Mvc.Formatters;
using NLog.Extensions.Logging;
using Filenet.Apis.Services;
using Microsoft.Extensions.Configuration;
using Filenet.Apis.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Diagnostics;
using System.Net;
namespace Filenet.Apis
{
public class Startup
{
public static IConfigurationRoot Configuration;
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appSettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appSettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services) //for dependecny injection
{
services.AddMvc()
.AddMvcOptions(o => o.OutputFormatters.Add(
new XmlDataContractSerializerOutputFormatter()
)) //allows you to configure supported formatters for input and output
;
///////Most likely this will not be required ///// Most likely having first letter of propery lowercase is what will be wanted. Angular likes first letter lowercase
//.AddJsonOptions(o => //this code makes it so that automatic camel case is not sent back to caller - will take property names as they are defined
//{
// if(o.SerializerSettings.ContractResolver != null)
// {
// var castedResolver = o.SerializerSettings.ContractResolver
// as DefaultContractResolver;
// castedResolver.NamingStrategy = null; //will take property names as they are defined
// }
//}
//);
//compiler directives
#if DEBUG
services.AddTransient<IMailService, LocalMailService>();
#else
services.AddTransient<IMailService, CloudMailService>();
services.Configure<SMSoptions>(Configuration);
#endif
var connectionString = Startup.Configuration["connectionStrings:cityInfoDBConnectionString"];
services.AddDbContext<CityInfoContext>(o => o.UseSqlServer(connectionString)); //default scope lifetime
services.AddScoped<ICityInfoRepository, CityInfoRepository>(); //Scoped
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, CityInfoContext cityInfoContext)
{
loggerFactory.AddConsole();
loggerFactory.AddDebug();
loggerFactory.AddNLog();
loggerFactory.ConfigureNLog("nlog.config");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage(); //adds exception capability handling to the pipeline.
}
else
{
app.UseExceptionHandler();
//app.UseExceptionHandler(
// options =>
// {
// options.Run(
// async context =>
// {
// context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
// context.Response.ContentType = "text/html";
// var ex = context.Features.Get<IExceptionHandlerFeature>();
// if (ex != null)
// {
// var err = $"<h1>Error: {ex.Error.Message}</h1>{ex.Error.StackTrace }";
// await context.Response.WriteAsync(err).ConfigureAwait(false);
// }
// });
// }
//);
}
cityInfoContext.EnsureSeedDataForContext();
app.UseStatusCodePages(); //will receive the text based information for status code.
AutoMapper.Mapper.Initialize(cfg =>
{
cfg.CreateMap<Entities.City, Models.CityWithoutPointsOfInterestDto>();
cfg.CreateMap<Entities.City, Models.CityDto>();
cfg.CreateMap<Entities.PointOfInterest, Models.PointOfInterestDto>();
cfg.CreateMap<Models.PointOfInterestForCreationDto, Entities.PointOfInterest>();
cfg.CreateMap<Models.PointOfInterestForUpdateDto, Entities.PointOfInterest>();
cfg.CreateMap<Entities.PointOfInterest, Models.PointOfInterestForUpdateDto>();
});
app.UseMvc(); //add the MVC handles HTTP Middleware requests now
//app.Run((context) =>
//{
// throw new Exception("Exception");
//});
//app.Run(async (context) =>
//{
// await context.Response.WriteAsync("Hello World!");
//});
}
}
}