-
Notifications
You must be signed in to change notification settings - Fork 272
/
AppHost.cs
79 lines (64 loc) · 2.27 KB
/
AppHost.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
using Funq;
using ServiceStack.Configuration;
using ServiceStack.Data;
using ServiceStack.Examples.ServiceInterface;
using ServiceStack.Examples.ServiceInterface.Support;
using ServiceStack.Examples.ServiceModel;
using ServiceStack.Logging;
using ServiceStack.OrmLite;
using ServiceStack.OrmLite.Sqlite;
using ExampleConfig = ServiceStack.Examples.ServiceInterface.ExampleConfig;
namespace ServiceStack.Examples.Host.Console
{
/// <summary>
/// An example of a AppHost to have your services running inside a webserver.
/// </summary>
public class AppHost
: AppHostHttpListenerBase
{
private static ILog log;
public AppHost()
: base("ServiceStack Examples", typeof(GetFactorialService).Assembly)
{
LogManager.LogFactory = new DebugLogFactory();
log = LogManager.GetLogger(typeof(AppHost));
}
public override void Configure(Container container)
{
//Signal advanced web browsers what HTTP Methods you accept
base.SetConfig(new HostConfig
{
DebugMode = true,
});
container.Register<IAppSettings>(new AppSettings());
var appSettings = container.Resolve<IAppSettings>();
container.Register(c => new ExampleConfig(c.Resolve<IAppSettings>()));
var appConfig = container.Resolve<ExampleConfig>();
container.Register<IDbConnectionFactory>(c =>
new OrmLiteConnectionFactory(
appConfig.ConnectionString.MapAbsolutePath(),
SqliteOrmLiteDialectProvider.Instance));
if (appSettings.Get("PerformTestsOnInit", false))
{
log.Debug("Performing database tests...");
DatabaseTest(container.Resolve<IDbConnectionFactory>());
}
}
private void DatabaseTest(IDbConnectionFactory connectionFactory)
{
ConfigureDatabase.Init(connectionFactory);
var storeRequest = new StoreNewUser {
Email = "new@email",
Password = "password",
UserName = "new UserName"
};
var storeHandler = Container.Resolve<StoreNewUserService>();
storeHandler.Any(storeRequest);
var getAllHandler = Container.Resolve<GetAllUsersService>();
var response = (GetAllUsersResponse)getAllHandler.Any(new GetAllUsers());
var user = response.Users[0];
System.Console.WriteLine("Stored and retrieved user: {0}, {1}, {2}",
user.Id, user.UserName, user.Email);
}
}
}