HttpContext.GetService在单 [英] HttpContext.GetService in Mono

查看:221
本文介绍了HttpContext.GetService在单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code(从的这里),而我试图在Linux服务器瓦特/单声道2.10.5上运行它。

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?

推荐答案

不知道有关单声道的GetService 方法,但你有code的取消的可以缩短这样的:

Not sure about the GetService method in Mono but the code you have lifted could be shortened like this:

private static HttpContextBase GetCurrentContext(ControllerContext context) {
    return context.HttpContext;
}

您并不真的需要经过申请,以获取HttpContext的时候,你必须直接访问它。我也改变了返回类型为 HttpContextBase 而不是的HttpContext ,因为在ASP.NET MVC建议总是工作与抽象。它使你的code更弱耦合和单元测试。

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 HttpContextBase instead of HttpContext because in ASP.NET MVC it is recommended to always work with abstractions. It makes your code more weakly coupled and unit testable.

或者,如果不知我原因,你要使用实际的HttpContext (不知道为什么有人想绑他的code的话)你可以尝试以下方法:

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:

private static HttpContext GetCurrentContext(ControllerContext context) {
    return context.HttpContext.ApplicationInstance.Context;
}

但你可以在这两种情况下看到这个存在 GetCurrentContext 静态方法变得相当可疑的。

But as you can see in both cases the existence of this GetCurrentContext static method becomes quite questionable.

这篇关于HttpContext.GetService在单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆