Response is not available in this context

一曲冷凌霜 提交于 2019-11-29 06:45:55
Allen Rice

After alot of digging and looking around the SolrNet code, they don't appear to be doing anything wrong. Also, as Darin pointed out in an indirect manner, HttpUtility.UrlEncode should work fine in code without a HttpContext, such as a console application, and it does.

However, as VinayC pointed out in his comment on that answer of Darin's:

Actually, it appears to be a bug. From reflector, actual code appears to be "if (null != current && null != current.Response && ...)" where current is current http context. Issue here is that Response getter throws an exception, instead of returning null

Instead of throwing that overly descriptive exception (no doubt they were trying to be helpful), they should have just returned null and let null reference exceptions happen. In this case, they were simply checking for nulls, so the exception wouldn't have happened anyway! I'll report it as a bug if it hasn't been already.

Unfortunately, what this means to you is that you have pretty much no choice but to run in Classic mode. Technically you could put the call to TebeComSearchEngine.SolrManager.RecalculateMostRequested() in a thread that you spawn in application_start and delay its execution until after the app finishes starting. As far as I know, there is no surefire way to programmatically signal the end of the application starting so, that approach may be a little messy.

If you're up for it though, you could probably get that delayed startup mechanism implemented. Compared to punishing the first visitor to the site, it doesn't seem too bad.

'Response is not available in this context'. What can be the problem?

You are running this in IIS7 Integrated Application Pool mode instead of Classic mode. In Integrated mode you don't have access to the HttpResponse in Application_Start any any attempt to access it will blow.

Here's a blog post which covers a similar situation but with the HttpRequest.

This was discussed about a month ago in the SolrNet mailing list.

It's a regression in ASP.NET 4, here's a mention of this bug.

A future release of SolrNet will replace System.Web.HttpUtility.UrlEncode to work around this bug. (or if you really need this, why not fork the source code and fix it?)

EDIT: I just fixed this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!