How can I use a standard ASP.NET session object within ServiceStack service implementation

后端 未结 4 1187
夕颜
夕颜 2020-12-12 23:12

I\'m just getting started with ServiceStack and, as a test case, I am looking to rework an existing service which is built using standard ASP.Net handlers. I\'ve managed to

4条回答
  •  情话喂你
    2020-12-12 23:43

    Thanks @Richard for your answer above. I am running a new version of service stack and they have removed the ServiceStackHttpFactory with HttpFactory. Instead of having

    private readonly static ServiceStackHttpHandlerFactory factory = new ServiceStackHttpHandlerFactory();
    

    You need to have

    private static readonly HttpHandlerFactory Factory = new HttpHandlerFactory();
    

    Here is updated code for this service

    using ServiceStack;
    using System.Web;
    using System.Web.SessionState;
    
    namespace MaryKay.eCommerce.Mappers.AMR.Host
    {
    public class SessionHttpHandlerFactory : IHttpHandlerFactory
    {
        private static readonly HttpHandlerFactory Factory = new HttpHandlerFactory();
        public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
        {
            var handler = Factory.GetHandler(context, requestType, url, pathTranslated);
            return handler == null ? null : new SessionHandlerDecorator(handler);
        }
    
        public void ReleaseHandler(IHttpHandler handler)
        {
            Factory.ReleaseHandler(handler);
        }
    }
    
    public class SessionHandlerDecorator : IHttpHandler, IRequiresSessionState
    {
        private IHttpHandler Handler { get; set; }
    
        internal SessionHandlerDecorator(IHttpHandler handler)
        {
            Handler = handler;
        }
    
        public bool IsReusable
        {
            get { return Handler.IsReusable; }
        }
    
        public void ProcessRequest(HttpContext context)
        {
            Handler.ProcessRequest(context);
        }
    }
    }
    

提交回复
热议问题