-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathGlobal.asax.cs
More file actions
102 lines (85 loc) · 3.66 KB
/
Global.asax.cs
File metadata and controls
102 lines (85 loc) · 3.66 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
using System;
using Funq;
using ServiceStack;
using ServiceStack.Auth;
using ServiceStack.Configuration;
using ServiceStack.Data;
using ServiceStack.DataAnnotations;
using ServiceStack.Messaging;
using ServiceStack.Messaging.Redis;
using ServiceStack.MiniProfiler;
using ServiceStack.MiniProfiler.Data;
using ServiceStack.OrmLite;
using ServiceStack.Razor;
using ServiceStack.Redis;
namespace Reusability
{
public class CustomSession : AuthUserSession {}
public class EmailRegistration
{
[PrimaryKey]
public string Email { get; set; }
}
public class AppHost : AppHostBase
{
public AppHost() : base("Reusability demo", typeof(SMessage).Assembly) {}
public override void Configure(Container container)
{
container.RegisterAutoWired<EmailProvider>();
container.RegisterAutoWired<FacebookGateway>();
container.RegisterAutoWired<TwitterGateway>();
Plugins.Add(new RazorFormat());
Plugins.Add(new RequestLogsFeature());
var appSettings = new AppSettings();
Plugins.Add(new AuthFeature(() => new CustomSession(),
new IAuthProvider[] {
new CredentialsAuthProvider(appSettings),
new TwitterAuthProvider(appSettings),
new FacebookAuthProvider(appSettings),
}));
container.Register<IRedisClientsManager>(new PooledRedisClientManager("localhost:6379"));
container.Register(c => c.Resolve<IRedisClientsManager>().GetCacheClient());
container.Register<IDbConnectionFactory>(
new OrmLiteConnectionFactory("~/App_Data/db.sqlite".MapHostAbsolutePath(),
SqliteDialect.Provider) {
ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current)
});
//Store User Data into above OrmLite database
container.Register<IAuthRepository>(c =>
new OrmLiteAuthRepository(c.Resolve<IDbConnectionFactory>()));
//If using and RDBMS to persist UserAuth, we must create required tables
var authRepo = container.Resolve<IAuthRepository>();
authRepo.InitSchema();
//Register MQ Service
var mqService = new RedisMqServer(container.Resolve<IRedisClientsManager>());
container.Register<IMessageService>(mqService);
container.Register(mqService.MessageFactory);
mqService.RegisterHandler<SMessage>(ServiceController.ExecuteMessage);
mqService.RegisterHandler<CallFacebook>(ServiceController.ExecuteMessage);
mqService.RegisterHandler<EmailMessage>(ServiceController.ExecuteMessage);
mqService.RegisterHandler<PostStatusTwitter>(ServiceController.ExecuteMessage);
mqService.Start();
if (appSettings.Get("ResetAllOnStartUp", false))
{
ResetAll(container, (OrmLiteAuthRepository)authRepo);
}
}
private static void ResetAll(Container container, OrmLiteAuthRepository authRepo)
{
authRepo.DropAndReCreateTables();
using (var db = container.Resolve<IDbConnectionFactory>().Open())
{
db.DropAndCreateTable<EmailRegistration>();
db.DropAndCreateTable<SMessageReceipt>();
}
container.Resolve<IRedisClientsManager>().Exec(r => r.FlushAll());
}
}
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
new AppHost().Init();
}
}
}