HttpContext.Current的NullReferenceException在静态方法 [英] HttpContext.Current NullReferenceException in Static Method

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

问题描述

我有几个静态方法静态类。在这些方法中,我试图用 HttpContext.Current 来访问当前线程的上下文。例如:

I have a static class with several static methods. In these methods, I'm trying to access the current thread's context using HttpContext.Current. For example:

var userName = HttpContext.Current.User.Identity.Name;

然而,当我这样做,我收到了的NullReferenceException ,臭名昭著的对象引用不设置到对象的实例。

However, when I do that, I receive a NullReferenceException, the infamous "Object reference not set to an instance of an object."

任何想法?

推荐答案

这是不是从原来的文章明确指出,的HttpContext 实际上是缺少了什么。在 HttpContext.User中属性还可以在生命周期的某些阶段,这会给你确切的同样的异常空。所有其他的问题不谈,你需要单步调试源,看看哪些除权pression的一部分实际上是

It isn't clear from the original post that the HttpContext is actually what's missing. The HttpContext.User property can also be null at certain stages of the lifecycle, which would give you the exact same exception. All other issues aside, you need to step through the source and see which part of the expression is actually null.

当你写code,引用如 HttpContext.Current 的静态方法/属性,你必须写他们知道你的code不能保证当方法/属性实际可用来运行。通常情况下,你有这样的事情:

When you write code that references static methods/properties like HttpContext.Current, you have to write them knowing that your code isn't guaranteed to be run when the methods/properties are actually available. Normally you have something like this:

static string GetCurrentUserName()
{
    HttpContext context = HttpContext.Current;
    if (context == null)
        return null;
    IPrincipal user = context.User;
    if (user == null)
        return null;
    return user.Identity.Name;
}

虽然我怀疑这不会真的在这里解决您的问题,它只是摆脱了异常。这个问题更可能是你调用在时间或地点时的背景是根本无法使用,如在后台线程,静态构造函数或字段初始值,或在的Application_BeginRequest 的方法,或者一些类似的地方。

Although I suspect that this wouldn't really solve your problem here, it would just get rid of the exception. The issue is more likely that you're calling this method at a time or place when the context is simply not available, such as on a background thread, static constructor or field initializer, or in the Application_BeginRequest method, or some similar place.

我可能会通过改变静态方法依赖于一个的HttpContext 实例类(即在构造函数中获取)的实例方法开始。这很容易给自己误以为,像 GetCurrentUserName 方法是简单的实用的方法,但他们真的不,这是一般无效被调用一个方法引用 HttpContext.Current 通过从任何地方的静态属性,你不已经有一个的实例的参照同的HttpContext (即从类)。奇怪的是,如果你开始重写你这样的类:

I might start by changing the static methods to instance methods of a class that depends on an HttpContext instance (i.e. taken in the constructor). It's easy to fool yourself into thinking that methods like GetCurrentUserName are simple "utility" methods, but they're really not, and it is generally invalid to be invoking a method that references HttpContext.Current through the static property from any place where you don't already have an instance reference to the same HttpContext (i.e. from the Page class). Odds are, if you start rewriting your classes like this:

public class UserResolver
{
    private HttpContext context;

    public UserResolver(HttpContext context)
    {
        if (context == null)
            throw new ArgumentNullException("context");
        this.context = context;
    }

    public string GetUserName()
    {
        return (context.User != null) ? context.User.Identity.Name : null;
    }
}

......那么你很可能会发现的非常的很快这家连锁店正在被打破,这将是在您需要的参考点 HttpContext.Current ,因为你不能从其他地方得到它。

...then you will likely find out very quickly where the chain is being broken, which will be the point at which you need to reference HttpContext.Current because you can't get it from anywhere else.

在这个特定的情况下,很明显,你可以采取的堆栈跟踪解决问题的的NullReferenceException 来找出链开始的地方/时,所以你不要'T必须让我上面所描述的变化 - 我只是建议的一般方法,这将有助于降低未来这些各种各样的失踪单的错误

In this specific case, obviously, you can solve the problem just by taking the stack trace of the NullReferenceException to find out where/when the chain begins, so you don't have to make the changes I've described above - I'm simply recommending a general approach that will help reduce these sorts of "missing singleton" errors in the future.

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

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