首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在HangFire中添加HttpContext

在HangFire中添加HttpContext
EN

Stack Overflow用户
提问于 2017-11-21 10:59:56
回答 2查看 4.9K关注 0票数 6

我是HangFire的初学者,期待着每月使用HangFire在我的web应用程序中调用一些操作。但这些操作需要HttpContext。

那么我的问题是:有没有办法在HangFire项目中添加(或创建)一个httpcontext?

我试着用谷歌搜索,但没有合适的答案。谢谢你的帮忙!

我找到了一个简短的讨论。遗憾的是,答案是“不可能”。更新: Ref https://discuss.hangfire.io/t/passing-site-url-to-hangfire-recurrent-jobs/2641

EN

回答 2

Stack Overflow用户

发布于 2019-11-26 06:46:00

我有一个类似的场景,系统在很大程度上依赖于会话,我采用了同样的方法来伪造HttpContext并传递会话变量。

在我的方法中,我接收到一个可序列化的上下文,其中包含所有会话变量:

代码语言:javascript
复制
public static void MyMethod(Hangfire.Server.PerformContext context, Hangfire.IJobCancellationToken cancellationToken, 
        SerializeableHttpContext serializeableHttpContext, ... etc)
{
    using (var fakeContext = serializeableHttpContext.CreateFakeHttpContext()) {
    // ...
    }
}

在入队期间,我将当前上下文传递给我的可序列化上下文,它将捕获所有当前变量:

代码语言:javascript
复制
// null and null are injected by Hangfire
Hangfire.BackgroundJob.Enqueue(() => MyMethod(null, null, new SerializeableHttpContext(System.Web.HttpContext.Current), etc..);

这就是奇迹发生的地方。这将保存所有会话变量,并恢复它们。请注意,使用IDispose很重要,因为您的下一个Hangfire作业不想继承前一个作业的假HttpContext,因此您需要清理HttpContext。

代码语言:javascript
复制
/// <summary>
/// This serializes HttpContext with primitive Session variables
/// </summary>
[Serializable]
public class SerializeableHttpContext
{
    public Uri RequestUrl { get; set; }
    public Dictionary<string, object> SessionVariables { get; set; }

    /// <summary>
    /// Given a real HttpContext (you can pass System.Web.HttpContext.Current), this saves all useful information 
    /// into this serializable class, so that you can later reuse (restore) a cloned fake HttpContext
    /// </summary>
    /// <param name="httpContext">You'll probably want to pass System.Web.HttpContext.Current</param>
    public SerializeableHttpContext(HttpContext httpContext)
    {
        this.RequestUrl = httpContext.Request.Url;

        // Save all Session variables
        this.SessionVariables = new Dictionary<string, object>();
        foreach (object objkey in httpContext.Session.Keys)
        {
            string key = objkey as string;
            if (key == null || httpContext.Session[key] == null)
                continue;
            Type type = httpContext.Session[key].GetType();

            if (type.IsPrimitive || type == typeof(string))
            {
                try
                {
                    // ignore if not serializable
                    object val = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(httpContext.Session[key]));
                    this.SessionVariables.Add(key, httpContext.Session[key]);
                }
                catch (Exception) { }
            }
        }

    }

    /// This is for internal usage, when deserializing.
    public SerializeableHttpContext()
    {
    }

    /// <summary>
    /// Deserializes into a Fake HttpContext
    /// </summary>
    /// <returns></returns>
    protected HttpContext Deserialize()
    {
        var httpRequest = new HttpRequest("", this.RequestUrl.AbsoluteUri, "");
        var stringWriter = new StringWriter();
        var httpResponse = new HttpResponse(stringWriter);
        var httpContext = new HttpContext(httpRequest, httpResponse);

        var sessionContainer = new HttpSessionStateContainer("id", new SessionStateItemCollection(),
                                                new HttpStaticObjectsCollection(), 10, true,
                                                HttpCookieMode.AutoDetect,
                                                SessionStateMode.InProc, false);

        httpContext.Items["AspSession"] = typeof(HttpSessionState).GetConstructor(
                                    BindingFlags.NonPublic | BindingFlags.Instance,
                                    null, CallingConventions.Standard,
                                    new[] { typeof(HttpSessionStateContainer) },
                                    null)
                            .Invoke(new object[] { sessionContainer });

        // Restore session variables
        if (this.SessionVariables != null)
            foreach (string key in this.SessionVariables.Keys)
                httpContext.Session[key] = this.SessionVariables[key];

        // Restore context variables
        if (this.ContextVariables != null)
            foreach (string key in this.ContextVariables.Keys)
                httpContext.Items[key] = this.ContextVariables[key];

        return httpContext;
    }

    /// <summary>
    /// Deserializes this class back into a fake HttpContext, and automatically sets that into System.Web.HttpContext.Current
    /// Don't forget to DISPOSE this instance at the end, so that the Context is cleared (else Hangfire will reuse this thread with previous HttpContext)
    /// </summary>
    public FakeHttpContext CreateFakeHttpContext()
    {
        return new FakeHttpContext(this.Deserialize());
    }

    public class FakeHttpContext : IDisposable
    {
        HttpContext previousContext;
        public FakeHttpContext(HttpContext context)
        {
            previousContext = HttpContext.Current;
            HttpContext.Current = context;
        }
        public void Dispose()
        {
            HttpContext.Current = previousContext; // previousContext is probably null, but one might be using FakeHttpContexts even inside an existing web context
        }
    }
}
票数 3
EN

Stack Overflow用户

发布于 2017-11-23 10:14:21

解决这个问题3天后,我发现在HangFire中创建一个假HttpContext是可能的。在这个伪HttpContext中有很多东西需要构造。但是,您可以只初始化所需的属性,而不需要定义所有属性。

非常感谢@jbl

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47404479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档