Can an ASMX web service response return with a Response.BinaryWrite?

Instead of returning a binary stream (MTOM / Base64 encoded) in the web method itself (like SOAP XML), like this:

[WebMethod]
public byte[] Download(string FileName)
....
return byteArray;

      

Could this method react in some way (possibly via an object Server

) ?:

Response.BinaryWrite(byteArray);

      

Pseudo:

[WebMethod]
public DownloadBinaryWrite(string FileName)
...
Response.BinaryWrite(byteArray);

      

+3


source to share


2 answers


Yes it is possible. Note. I am changing the return type to void as we are going to write the response directly and I need to manually set the content type and complete the response.

[WebMethod]
public void Download(string FileName)
{
    HttpContext.Current.Response.ContentType="image/png";
    HttpContext.Current.Response.BinaryWrite(imagebytes);
    HttpContext.Current.Response.End();
}

      



Please note that WebMethod is not currently supported , you should switch to Web API or WCF (if you need SOAP support).

+7


source


If you want to do BinaryWrite, you probably want to write a separate IHttpHandler instead of a web method. Web methods are focused on SOAP stuff, so cracking them into custom responses, while possible, is odd.



0


source







All Articles