Asp.net mvc 使用简单注入器保存会话对象时出现的问题

Asp.net mvc 使用简单注入器保存会话对象时出现的问题,asp.net-mvc,simple-injector,Asp.net Mvc,Simple Injector,我有一个MVC应用程序,我试图使用SimpleInjector的DI框架在会话中保存一些对象。 我将对象注册如下 container.RegisterPerWebRequest<Func<HttpSessionState>>(() => () => HttpContext.Current.Session); container.RegisterSingle<MemoryCache>(() => MemoryCache.Default

我有一个MVC应用程序,我试图使用SimpleInjector的DI框架在会话中保存一些对象。 我将对象注册如下

container.RegisterPerWebRequest<Func<HttpSessionState>>(() => 
    () => HttpContext.Current.Session);
container.RegisterSingle<MemoryCache>(() => MemoryCache.Default);
container.RegisterPerWebRequest<ISessionCacheService, SessionCacheService>();
我们需要使用此结构的主要原因是我们需要存储多播委托给会话,如下面的示例所示

public RuntimeContext Current
{
    get
    {
        Func<RuntimeContext> getRuntimeContextFunc = () =>
        {
            return new RuntimeContext() 
            { 
                ImpersonatingAgentId = Guid.Empty 
            };
        };
        return this._SessionCacheService.Get<RuntimeContext>(
            typeof(RuntimeContext).ToString(), 
            getRuntimeContextFunc);
    }
}
公共运行时上下文当前
{
得到
{
Func getRuntimeContextFunc=()=>
{
返回新的RuntimeContext()
{ 
ImpersonatingAgentId=Guid.Empty
};
};
返回此。\u SessionCacheService.Get(
typeof(RuntimeContext).ToString(),
getRuntimeContextFunc);
}
}
稍后,我们可以使用get方法从会话中检索这些项

public T Get<T>(string key, Func<T> acquire)
{
    if (this.IsSet(key))
    {
        return this.Get<T>(key);
    }
    else
    {
        var result = acquire();
        this.Set<T>(key, result);
        return result;
    }
}
public T-Get(字符串键,Func-acquire)
{
如果(此IsSet(键))
{
返回此。获取(键);
}
其他的
{
var result=acquire();
此.Set(键、结果);
返回结果;
}
}

My web.config看起来像这样,为什么要使用
Func
?为什么不使用
container.Register(()=>HttpContext.Current.Session)感谢您的回复。我在原始问题中提供了支持代码,解释了为什么我们需要在会话中存储一组委托。从提供的代码片段中很难看出您想要做什么。我猜不出我需要添加什么代码来将它们连接在一起。
this._HttpSessionState()
public RuntimeContext Current
{
    get
    {
        Func<RuntimeContext> getRuntimeContextFunc = () =>
        {
            return new RuntimeContext() 
            { 
                ImpersonatingAgentId = Guid.Empty 
            };
        };
        return this._SessionCacheService.Get<RuntimeContext>(
            typeof(RuntimeContext).ToString(), 
            getRuntimeContextFunc);
    }
}
public T Get<T>(string key, Func<T> acquire)
{
    if (this.IsSet(key))
    {
        return this.Get<T>(key);
    }
    else
    {
        var result = acquire();
        this.Set<T>(key, result);
        return result;
    }
}