/
Global.asax.cs
195 lines (166 loc) · 6.92 KB
/
Global.asax.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Net;
using System.Web;
using Check.ServiceInterface;
using Check.ServiceModel;
using Funq;
using ServiceStack;
using ServiceStack.Api.Swagger;
using ServiceStack.Data;
using ServiceStack.MiniProfiler;
using ServiceStack.MiniProfiler.Data;
using ServiceStack.OrmLite;
using ServiceStack.Razor;
using ServiceStack.Text;
using ServiceStack.Validation;
using ServiceStack.Web;
namespace CheckWeb
{
public class AppHost : AppHostBase
{
/// <summary>
/// Initializes a new instance of the <see cref="AppHost"/> class.
/// </summary>
public AppHost()
: base("CheckWeb", typeof(ErrorsService).Assembly) {}
/// <summary>
/// Configure the Web Application host.
/// </summary>
/// <param name="container">The container.</param>
public override void Configure(Container container)
{
//this.CustomErrorHttpHandlers[HttpStatusCode.NotFound] = null;
// Change ServiceStack configuration
this.SetConfig(new HostConfig
{
//UseHttpsLinks = true,
AppendUtf8CharsetOnContentTypes = new HashSet<string> { MimeTypes.Html },
// Set to return JSON if no request content type is defined
// e.g. text/html or application/json
//DefaultContentType = MimeTypes.Json,
#if !DEBUG
// Show StackTraces in service responses during development
DebugMode = true,
#endif
// Disable SOAP endpoints
//EnableFeatures = Feature.All.Remove(Feature.Soap)
//EnableFeatures = Feature.All.Remove(Feature.Metadata)
});
container.Register<IServiceClient>(c =>
new JsonServiceClient("http://localhost:55799/") {
CaptureSynchronizationContext = true,
});
// Configure JSON serialization properties.
this.ConfigureSerialization(container);
// Configure ServiceStack database connections.
this.ConfigureDataConnection(container);
// Configure ServiceStack Authentication plugin.
this.ConfigureAuth(container);
// Configure ServiceStack Fluent Validation plugin.
this.ConfigureValidation(container);
// Configure ServiceStack Razor views.
this.ConfigureView(container);
Plugins.Add(new AutoQueryFeature());
Plugins.Add(new PostmanFeature());
Plugins.Add(new CorsFeature());
container.Register<IDbConnectionFactory>(
new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider));
using (var db = container.Resolve<IDbConnectionFactory>().Open())
{
db.DropAndCreateTable<Rockstar>();
db.InsertAll(SeedRockstars);
}
var dbFactory = (OrmLiteConnectionFactory)container.Resolve<IDbConnectionFactory>();
dbFactory.RegisterConnection("SqlServer",
new OrmLiteConnectionFactory(
"Server={0};Database=test;User Id=test;Password=test;".Fmt(Environment.GetEnvironmentVariable("CI_HOST")),
SqlServerDialect.Provider) {
ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current)
});
}
public static Rockstar[] SeedRockstars = new[] {
new Rockstar { Id = 1, FirstName = "Jimi", LastName = "Hendrix", Age = 27 },
new Rockstar { Id = 2, FirstName = "Jim", LastName = "Morrison", Age = 27 },
new Rockstar { Id = 3, FirstName = "Kurt", LastName = "Cobain", Age = 27 },
new Rockstar { Id = 4, FirstName = "Elvis", LastName = "Presley", Age = 42 },
new Rockstar { Id = 5, FirstName = "David", LastName = "Grohl", Age = 44 },
new Rockstar { Id = 6, FirstName = "Eddie", LastName = "Vedder", Age = 48 },
new Rockstar { Id = 7, FirstName = "Michael", LastName = "Jackson", Age = 50 },
};
/// <summary>
/// Configure JSON serialization properties.
/// </summary>
/// <param name="container">The container.</param>
private void ConfigureSerialization(Container container)
{
// Set JSON web services to return idiomatic JSON camelCase properties
JsConfig.EmitCamelCaseNames = true;
//JsConfig.EmitLowercaseUnderscoreNames = true;
// Set JSON web services to return ISO8601 date format
JsConfig.DateHandler = DateHandler.ISO8601;
// Exclude type info during serialization as an effect of IoC
JsConfig.ExcludeTypeInfo = true;
}
/// <summary>
/// // Configure ServiceStack database connections.
/// </summary>
/// <param name="container">The container.</param>
private void ConfigureDataConnection(Container container)
{
// ...
}
/// <summary>
/// Configure ServiceStack Authentication plugin.
/// </summary>
/// <param name="container">The container.</param>
private void ConfigureAuth(Container container)
{
// ...
}
/// <summary>
/// Configure ServiceStack Fluent Validation plugin.
/// </summary>
/// <param name="container">The container.</param>
private void ConfigureValidation(Container container)
{
// Provide fluent validation functionality for web services
Plugins.Add(new ValidationFeature());
container.RegisterValidators(typeof(AppHost).Assembly);
}
/// <summary>
/// Configure ServiceStack Razor views.
/// </summary>
/// <param name="container">The container.</param>
private void ConfigureView(Container container)
{
// Enable ServiceStack Razor
Plugins.Add(new RazorFormat());
// Enable support for Swagger API browser
Plugins.Add(new SwaggerFeature
{
UseBootstrapTheme = true,
LogoUrl = "//lh6.googleusercontent.com/-lh7Gk4ZoVAM/AAAAAAAAAAI/AAAAAAAAAAA/_0CgCb4s1e0/s32-c/photo.jpg"
});
//Plugins.Add(new CorsFeature()); // Uncomment if the services to be available from external sites
}
}
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
new AppHost().Init();
}
protected void Application_BeginRequest(object src, EventArgs e)
{
if (Request.IsLocal)
Profiler.Start();
}
protected void Application_EndRequest(object src, EventArgs e)
{
Profiler.Stop();
}
}
}