Skip to content

Commit 3656c9a

Browse files
author
mwatson
committed
Prevent error accessing Request object
1 parent df2ea7e commit 3656c9a

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

Src/StackifyLib/Internal/Logs/LogQueue.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Runtime.Remoting.Messaging;
5-
using System.Text;
65
using System.Collections.Concurrent;
7-
using System.Threading;
86
using System.Threading.Tasks;
9-
using System.Web.Hosting;
107
using StackifyLib.Models;
118
using StackifyLib.Utils;
129
using StackifyLib.Web;
@@ -135,14 +132,18 @@ public void QueueLogMessage(Models.LogMsg msg)
135132
if (string.IsNullOrEmpty(msg.TransID))
136133
{
137134
if (_IsWebApp && System.Web.Hosting.HostingEnvironment.IsHosted
138-
&& System.Web.HttpContext.Current != null &&
135+
&& System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Handler != null &&
139136
System.Web.HttpContext.Current.Request != null)
140137
{
141138
msg.TransID = System.Web.HttpContext.Current.Request.GetHashCode().ToString();
142139
}
143140

144141
}
145142
}
143+
catch (System.Web.HttpException ex)
144+
{
145+
StackifyAPILogger.Log("Request not available \r\n" + ex.ToString());
146+
}
146147
catch(Exception ex)
147148
{
148149
StackifyAPILogger.Log("Error figuring out TransID \r\n" + ex.ToString());
@@ -151,8 +152,9 @@ public void QueueLogMessage(Models.LogMsg msg)
151152

152153

153154
if (_IsWebApp && System.Web.Hosting.HostingEnvironment.IsHosted
154-
&& System.Web.HttpContext.Current != null &&
155-
System.Web.HttpContext.Current.Request != null)
155+
&& System.Web.HttpContext.Current != null
156+
&& System.Web.HttpContext.Current.Handler != null
157+
&& System.Web.HttpContext.Current.Request != null)
156158
{
157159
var context = System.Web.HttpContext.Current;
158160

Src/StackifyLib/StackifyError.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,17 @@ private void Init()
124124

125125
try
126126
{
127-
HttpRequest request = System.Web.HttpContext.Current.Request;
128-
129-
if (request != null)
127+
if (System.Web.Hosting.HostingEnvironment.IsHosted
128+
&& System.Web.HttpContext.Current != null
129+
&& System.Web.HttpContext.Current.Handler != null
130+
&& System.Web.HttpContext.Current.Request != null)
130131
{
131-
requestAvailable = true;
132+
HttpRequest request = System.Web.HttpContext.Current.Request;
133+
134+
if (request != null)
135+
{
136+
requestAvailable = true;
137+
}
132138
}
133139
}
134140
catch

0 commit comments

Comments
 (0)