Response not available in this context when BuildSpellChecker

342 views
Skip to first unread message

Ted Jardine

unread,
Jun 6, 2011, 3:52:54 AM6/6/11
to SolrNet
On various development machines there's no issue, but when we deploy a
SolrNet application on our production server, we're ending up with a
"Response is not available in this context" exception when enabling
the ISolrOperations<T>.BuildSpellCheckDictionary(); in
Application_Start. Removing that spell check build call resolves the
issue (but obviously no spell checker built).

I know that for i.e. HttpContext is not available in integrated
pipeline applications in Application_Start, but I've not run into this
issue before and haven't found anyone else with a similar one - hoping
somebody out there will give a shout-out as to what the issue is.

It's an ASP.NET MVC 3 application running under ASP.NET 4 on Windows
2008 x64. Not running under Medium Trust. Oh, and SolrNet.0.3.1.0.
Solr instance running on Ubuntu Natty x64.

...a little bit later:

I dug into the source, and it could be simply because in
https://github.com/mausch/SolrNet/blob/master/SolrNet/Impl/SolrConnection.cs

try {
using (var postStream = request.GetRequestStream()) {
CopyTo(content, postStream);
}
return GetResponse(request).Data;
} catch (WebException e) {
var msg = e.Message;
if (e.Response != null) {
using (var s = e.Response.GetResponseStream())
using (var sr = new StreamReader(s))
msg = sr.ReadToEnd();
}
throw new SolrConnectionException(msg, e);
}

There must be an error occurring when attempting to get the response,
but e.Response is not available in Application_Start (and thus
throwing up the rabbit trail HttpException instead of the underlying
issue).

Therefore, I verified by temporarily putting BuildSpellChecker in
Begin_Request (where HttpContext is available) and everything is fine.
I'm thinking then that there's a bug in SolrConnection as e.Response
is not available in integrated pipeline if there's a WebEception that
occurs, but I'll have to figure out what my configuration's issue is
somehow as it's only on our production server and the rethrown
SolrConnectionException is either not occurring with the temporary
switch to Begin_Request or is not being logged anywhere/bubbling all
the way up.

Here's the stack trace:

[HttpException (0x80004005): Response is not available in this
context.]
System.Web.Util.HttpEncoder.get_Current() +11324418
System.Web.HttpUtility.UrlEncode(String str, Encoding e) +132
SolrNet.Impl.SolrConnection.<Get>b__0(KeyValuePair`2 input) in c:
\prg\SolrNet\svn\SolrNet\Impl\SolrConnection.cs:121
SolrNet.Utils.<Select>d__1a`2.MoveNext() in c:\prg\SolrNet\svn
\SolrNet\Utils\Func.cs:132
SolrNet.Utils.Func.Reduce(IEnumerable`1 source, TResult startValue,
Accumulator`2 accumulator) in c:\prg\SolrNet\svn\SolrNet\Utils\Func.cs:
37
SolrNet.Impl.SolrConnection.Get(String relativeUrl, IEnumerable`1
parameters) in c:\prg\SolrNet\svn\SolrNet\Impl\SolrConnection.cs:120
SolrNet.Impl.SolrQueryExecuter`1.Execute(ISolrQuery q, QueryOptions
options) in c:\prg\SolrNet\svn\SolrNet\Impl\SolrQueryExecuter.cs:308
SolrNet.Impl.SolrBasicServer`1.Query(ISolrQuery query, QueryOptions
options) in c:\prg\SolrNet\svn\SolrNet\Impl\SolrBasicServer.cs:83
SolrNet.Impl.SolrServer`1.BuildSpellCheckDictionary() in c:\prg
\SolrNet\svn\SolrNet\Impl\SolrServer.cs:116
abc.xyz.Core.Search.SolrSearchWarmup.Warm() in D:\Projects\Client
Projects\xyz\pqr\trunk\Web\abc.xyz.Core\Search\SolrSearchWarmup.cs:20
abc.xyz.Web.MvcApplication.Application_Start() in D:\Projects
\Client Projects\xyz\pqr\trunk\Web\abc.xyz.Web\Global.asax.cs:49

[HttpException (0x80004005): Response is not available in this
context.]

System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext
context, HttpApplication app) +3988565
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr
appContext, HttpContext context, MethodInfo[] handlers) +191
System.Web.HttpApplication.InitSpecial(HttpApplicationState state,
MethodInfo[] handlers, IntPtr appContext, HttpContext context) +325

System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr
appContext, HttpContext context) +407
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr
appContext) +375

[HttpException (0x80004005): Response is not available in this
context.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context)
+11529072
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context)
+141

System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest
wr, HttpContext context) +4784373

Mauricio Scheffer

unread,
Jun 6, 2011, 9:01:45 AM6/6/11
to sol...@googlegroups.com
I don't think this is related to SolrNet. It seems to be an issue with HttpContext.Response on IIS7 integrated mode, and SolrNet doesn't use HttpContext.

--
You received this message because you are subscribed to the Google Groups "SolrNet" group.
To post to this group, send email to sol...@googlegroups.com.
To unsubscribe from this group, send email to solrnet+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/solrnet?hl=en.


Ted Jardine

unread,
Jun 8, 2011, 3:07:57 PM6/8/11
to SolrNet
Mauricio,

I appreciate the very quick response, and even more importantly, your
work on SolrNet.

On this issue however, I believe you are mistaken as you can see in
the stack trace that "Response is not available in this context" and
it occurs in SolrNet. It turns out that in certain scenarios this is
indeed a bug in ASP.NET 4.0 (see http://ayende.com/blog/4599/hunt-the-bug)
whereby, starting in .NET 4.0, HttpUtility requires an HttpContext/
HttpResponse which is not yet available in Application_Start with IIS7
in integrated mode.

Everything made a lot more sense to me when I figured out I wasn't
looking at the applicable 0.3.1 tagged source ;-) What is happening
is:

1. BuildSpellChecker is being called in Application_Start.
2. HttpUtility.UrlEncode *now requires an HttpContext which not yet
available at Application_Start.* Or rather, it's available, but it's a
fake one. Getting the response throws an exception.
3. For some reason the stars align on our production server, but not
any of our other environments.

Solution:

a. Do as Ayende did and "write my own HttpUtility (well, take the one
from Mono and modify it) to avoid this bug."
b. or determine whether using HttpEncoder.Default instead does the
trick. I'm trying to track down how best to do this.
c. or use Uri.EscapeDataString as per
http://stackoverflow.com/questions/602642/server-urlencode-vs-httputility-urlencode/1148326#1148326
d. or just don't call BuildSpellChecker in Application_Start (where do
you typically recommend calling it?)

Unfortunately, even though the trunk code is changed (https://
github.com/mausch/SolrNet/blob/master/SolrNet/Impl/SolrConnection.cs)
it still will have the same issue.

If I provided a patch for this, I'm assuming it'll be a patch on the
trunk, but we'd prefer to not deploy onto a production site. Thoughts?

Thanks,

Ted

On Jun 6, 6:01 am, Mauricio Scheffer <mauricioschef...@gmail.com>
wrote:
> I don't think this is related to SolrNet. It seems to be an issue with
> HttpContext.Response on IIS7 integrated mode, and SolrNet doesn't use
> HttpContext.
> Maybe this can help:http://mvolo.com/blogs/serverside/archive/2007/11/10/Integrated-mode-...
>
> --
> Mauricio
>
>
>
>
>
>
>
> On Mon, Jun 6, 2011 at 4:52 AM, Ted Jardine <t...@ovalsquare.com> wrote:
> > On various development machines there's no issue, but when we deploy a
> > SolrNet application on our production server, we're ending up with a
> > "Response is not available in this context" exception when enabling
> > the ISolrOperations<T>.BuildSpellCheckDictionary(); in
> > Application_Start. Removing that spell check build call resolves the
> > issue (but obviously no spell checker built).
>
> > I know that for i.e. HttpContext is not available in integrated
> > pipeline applications in Application_Start, but I've not run into this
> > issue before and haven't found anyone else with a similar one - hoping
> > somebody out there will give a shout-out as to what the issue is.
>
> > It's an ASP.NET MVC 3 application running under ASP.NET 4 on Windows
> > 2008 x64. Not running under Medium Trust. Oh, and SolrNet.0.3.1.0.
> > Solr instance running on Ubuntu Natty x64.
>
> > ...a little bit later:
>
> > I dug into the source, and it could be simply because in
>
> >https://github.com/mausch/SolrNet/blob/master/SolrNet/Impl/SolrConnec...

Mauricio Scheffer

unread,
Jun 8, 2011, 4:40:25 PM6/8/11
to sol...@googlegroups.com
Ahh, good catch, didn't know about that issue. Let's grab Mono's url encoder, I was planning to do it anyway eventually for the client profile: http://code.google.com/p/solrnet/issues/detail?id=140

Can you do it and send me a patch or pull request? We can apply it to both master and the 0.3.x branch. 
The 0.3.x branch is currently just one commit ahead of the 0.3.1 release ( https://github.com/mausch/SolrNet/commit/7232706ae6f4c2e7d70331fbbb3db8066bf833e2 , it's just a test) so it's really the same as 0.3.1.

--
Mauricio

Ted Jardine

unread,
Jun 15, 2011, 10:37:22 PM6/15/11
to sol...@googlegroups.com

Mauricio,

Sorry for the delayed response, especially in light of your incredibly prompt responses. I hope to provide a patch for this but my schedule won't permit it immediately.

Ted

Mauricio Scheffer

unread,
Jun 16, 2011, 9:04:16 AM6/16/11
to sol...@googlegroups.com
I just created an issue about this: http://code.google.com/p/solrnet/issues/detail?id=151

As a workaround, you could configure Solr to build the dictionary on commit/optimize instead of doing it manually. See http://wiki.apache.org/solr/SpellCheckComponent#Building_on_Commits

--
Mauricio
Reply all
Reply to author
Forward
0 new messages