ServiceStack:AppHost 不支持通过单例访问当前请求

ServiceStack: AppHost does not support accessing the current Request via a Singleton

我在我的一项服务中尝试使用 this.SessionAs<T> 时遇到以下异常:

"AppHost does not support accessing the current Request via a Singleton"

违规代码位于构造函数中。奇怪的是,我只在使用 POST 动词时才看到这一点。我还不确定这是否只是巧合。

很抱歉此时没有提供详细信息。我试图弄清楚我的故障排除应该采取哪个方向。我会根据需要进行编辑。

谢谢

更新 - 堆栈跟踪

at ServiceStack.HostContext.GetCurrentRequest() at ServiceStack.SessionFeature.GetOrCreateSession[T](ICacheClient cache, IRequest httpReq, IResponse httpRes) at ServiceStack.Service.SessionAsTUserSession at PeruseServiceStack.Services.Settings.UserService..ctor() at lambda_method(Closure , Container ) at Funq.Container.ResolveImpl[TService](String name, Boolean throwIfMissing)

我会尽快更新,我觉得这个痕迹不完整。

从 StackTrace 看来,您正试图在 UserService 构造函数中访问 SessionAs<T>,您不能这样做,因为会话只能在请求的上下文中访问,并且服务构造函数无法访问注入的 IRequest 上下文以访问会话。

因此您需要将 SessionAs<T>() 调用从您的构造函数移到您的服务实现中。