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
|
//-----------------------------------------------------------------------
// <copyright file="Global.asax.cs" company="Andrew Arnott">
// Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace WebFormsRelyingParty {
using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.ServiceModel;
using System.Web;
public class Global : System.Web.HttpApplication {
private const string DataContextKey = "DataContext";
private const string DataContextTransactionKey = "DataContextTransaction";
/// <summary>
/// The logger for this sample to use.
/// </summary>
private static log4net.ILog logger = log4net.LogManager.GetLogger("DotNetOpenAuth.ConsumerSample");
public static log4net.ILog Logger {
get { return logger; }
}
public static User LoggedInUser {
get { return Global.DataContext.AuthenticationToken.Where(token => token.ClaimedIdentifier == HttpContext.Current.User.Identity.Name).Select(token => token.User).FirstOrDefault(); }
}
public static string ApplicationPath {
get {
string path = HttpContext.Current.Request.ApplicationPath;
if (!path.EndsWith("/")) {
path += "/";
}
return path;
}
}
/// <summary>
/// Gets the transaction-protected database connection for the current request.
/// </summary>
public static DatabaseEntities DataContext {
get {
DatabaseEntities dataContext = DataContextSimple;
if (dataContext == null) {
dataContext = new DatabaseEntities();
try {
dataContext.Connection.Open();
} catch (EntityException entityEx) {
var sqlEx = entityEx.InnerException as SqlException;
if (sqlEx != null) {
if (sqlEx.Class == 14 && sqlEx.Number == 15350) {
// Most likely the database schema hasn't been created yet.
HttpContext.Current.Response.Redirect("~/Setup.aspx");
}
}
throw;
}
DataContextTransactionSimple = dataContext.Connection.BeginTransaction();
DataContextSimple = dataContext;
}
return dataContext;
}
}
private static DatabaseEntities DataContextSimple {
get {
if (HttpContext.Current != null) {
return HttpContext.Current.Items[DataContextKey] as DatabaseEntities;
} else if (OperationContext.Current != null) {
object data;
if (OperationContext.Current.IncomingMessageProperties.TryGetValue(DataContextKey, out data)) {
return data as DatabaseEntities;
} else {
return null;
}
} else {
throw new InvalidOperationException();
}
}
set {
if (HttpContext.Current != null) {
HttpContext.Current.Items[DataContextKey] = value;
} else if (OperationContext.Current != null) {
OperationContext.Current.IncomingMessageProperties[DataContextKey] = value;
} else {
throw new InvalidOperationException();
}
}
}
private static IDbTransaction DataContextTransactionSimple {
get {
if (HttpContext.Current != null) {
return HttpContext.Current.Items[DataContextTransactionKey] as IDbTransaction;
} else if (OperationContext.Current != null) {
object data;
if (OperationContext.Current.IncomingMessageProperties.TryGetValue(DataContextTransactionKey, out data)) {
return data as IDbTransaction;
} else {
return null;
}
} else {
throw new InvalidOperationException();
}
}
set {
if (HttpContext.Current != null) {
HttpContext.Current.Items[DataContextTransactionKey] = value;
} else if (OperationContext.Current != null) {
OperationContext.Current.IncomingMessageProperties[DataContextTransactionKey] = value;
} else {
throw new InvalidOperationException();
}
}
}
protected void Application_Start(object sender, EventArgs e) {
log4net.Config.XmlConfigurator.Configure();
Logger.Info("Web application starting...");
}
protected void Session_Start(object sender, EventArgs e) {
}
protected void Application_BeginRequest(object sender, EventArgs e) {
}
protected void Application_EndRequest(object sender, EventArgs e) {
CommitAndCloseDatabaseIfNecessary();
}
protected void Application_AuthenticateRequest(object sender, EventArgs e) {
}
protected void Application_Error(object sender, EventArgs e) {
Logger.Error("An unhandled exception occurred in ASP.NET processing: " + Server.GetLastError(), Server.GetLastError());
if (DataContextTransactionSimple != null) {
DataContextTransactionSimple.Rollback();
DataContextTransactionSimple.Dispose();
}
}
protected void Session_End(object sender, EventArgs e) {
}
protected void Application_End(object sender, EventArgs e) {
Logger.Info("Web application shutting down...");
// this would be automatic, but in partial trust scenarios it is not.
log4net.LogManager.Shutdown();
}
private static void CommitAndCloseDatabaseIfNecessary() {
var dataContext = DataContextSimple;
if (dataContext != null) {
dataContext.SaveChanges();
if (DataContextTransactionSimple != null) {
DataContextTransactionSimple.Commit();
DataContextTransactionSimple.Dispose();
}
dataContext.Dispose();
DataContextSimple = null;
}
}
}
}
|