I’ve got the following code (lifted from here), and I’m trying to run it on a linux server w/ mono 2.10.5.
private static HttpContext GetCurrentContext(ControllerContext context) {
var currentApplication = (HttpApplication)context.HttpContext.GetService(typeof(HttpApplication));
if (currentApplication == null) {
throw new NullReferenceException("currentApplication");
}
return currentApplication.Context;
}
When running on mono, I get the following exception, which is straightforward enough:
System.NotImplementedException: The requested feature is not
implemented. at System.Web.HttpContextWrapper.GetService
(System.Type serviceType) [0x00000] in <filename unknown>:0
Is there a known workaround I can use to accomplish the same result on mono?
Not sure about the
GetServicemethod in Mono but the code you have lifted could be shortened like this:You don’t really need to go through the Application in order to fetch the HttpContext when you have direct access to it. I have also changed the return type to
HttpContextBaseinstead ofHttpContextbecause in ASP.NET MVC it is recommended to always work with abstractions. It makes your code more weakly coupled and unit testable.Or if for some unknown to me reason you want to work with an actual
HttpContext(no idea why someone would like to tie his code to it) you could try the following:But as you can see in both cases the existence of this
GetCurrentContextstatic method becomes quite questionable.