Pregunta

I have my custom httphandler which calls a wcf service. By Service returns a Stream

[OperationContract]
Stream GetMyStream(int width, int height);

I am calling the http handler in my aspx page from an image tag:

<asp:Image ID="imgStream" runat="server" ImageUrl="MyStreamHandler.ashx" Visible="true" />

In my handler, I have a reference to the WCF service and I am calling the operation as below:

MyServiceClient tcpClient = new MyServiceClient();
Image img = Image.FromStream(tcpClient.GetMyStream(30,100));

I am using NetTcp binding in my service. Now, I can call the http handler in my aspx page through jquery ajax and display the stream in the image tag so as to avoid post backs on the page.

Thanks.

¿Fue útil?

Solución

I do not think you will need to use ajax, you could just use the below and add the image reference to point to the handler in the src.

Please note the image type will need to match your image.

public class MyStreamHandler : IHttpHandler {

public void ProcessRequest (HttpContext context) {

    MyServiceClient tcpClient = new MyServiceClient();
    Image img = Image.FromStream(tcpClient.GetMyStream(30,100));
    img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)  
        img.Dispose()
    }

    public bool IsReusable {
    get {
        return false;
    }
   }
}

Otros consejos

You should use the native file handler of IIS to return the contents of an image file as opposed to running the stream through managed code. While hutchonoid's answer works, I would like to point out that the end result will probably be less scalable since it is writing out the stream to an Image object in memory and then sending that back through the response stream.

It will probably work just fine for most work loads, just saying it's actually less elegant a solution even though it is harder to implement. If anything, your service would probably be best if it uploaded to some CDN and then returned an image path to that resource as opposed to serving it through an httphandler.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top