views:

624

answers:

3

Hi All,

I am using Http Handler ashx file for showing the images. I was using Session object to get image and return in the response

Now problem is i need to use custom Session object its nothing but the Wrapper on HttpSession State But when i am trying to get existing custom session object its creating new ... its not showing session data , i checked the session Id which is also different Please adive how can i get existing session in ashx file ?

Note: When i use ASP.NET Sesssion its working fine

 [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class GetImage : IHttpHandler, System.Web.SessionState.IRequiresSessionState 
    {
A: 

you can just use a Actionresult rather than a handler for this

return new FileStreamResult(new FileStream(path, FileMode.Open), "image/jpeg");

or

return(new FileResult(Pathtoimage, "image/jpeg"));

that should make things easier as you wil be using a controll/action as your url

ie

<img src="/Images/showImage/1">

you can then have your actions deal with anything like pulling from db as bytes streaming, check validation etc

minus4
I believe this would only work with ASP.NET MVC. The OP was in regards to a custom HttpHandler. There are no controllers nor actions...
jrista
the question is an asp.net mvc question !!! also he said he was trying to get an image comming from winforms he prob used a httpHandler to do that, but MVC is a design pattern so why break the pattern, the pattern works fine without having to use any httpHandler as this is replaced by the Controller / Action and wouldent you be better leaving a solution !!!!
minus4
your solution will workt .. but what will be the path for the image ? i can't store image at server side .. again controller doing this will take more time as compare to http handler
prakash
+1  A: 

The fact that it's an ashx should be irrelevant - assuming the request is being spawned off a request from an exsiting session; I'm assuming it should be - but it might pay to check exactly how the request is being formed. Always pays to go back to basics :)

Assuming that's ok, this is how I've been doing it:

string sessionId = string.Empty; 
System.Web.SessionState.SessionIDManager sessionIDManager = new System.Web.SessionState.SessionIDManager();
bool supportSessionIDReissue;
sessionIDManager.InitializeRequest(httpContext, false, out supportSessionIDReissue); sessionId = sessionIDManager.GetSessionID(httpContext);
if (sessionId == null)
{
 // Create / issue new session id: 
 sessionId = sessionIDManager.CreateSessionID(httpContext);
}

At the end of this the sessionId variable will (should) contain the existing Session ID, or a newly created one that you can reuse later..

Adrian K
+1  A: 

When you want to get access to your Session State from an ASHX or HttpHandler you need to implement IReadOnlySessionState or IRequiresSessionState if you need read/write access.

abilauca