System.Web.HttpContext cannot be serialized because it does not have a parameterless constructor.
-
Hi all, I am attempting to save images from the desktop in a folder on the server using the following web service code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.Services;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;namespace MultipleImageUploadDemo
{
/// /// Summary description for SaveImageService
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class SaveImageService : System.Web.Services.WebService
{public SaveImageService() { } \[WebMethod\] public void ProcessRequest(HttpContext context) { if (context.Request.Files.Count > 0) { HttpFileCollection files = context.Request.Files; foreach (string key in files) { HttpPostedFile file = files\[key\]; string fileName = file.FileName; fileName = context.Server.MapPath("~/Upload/" + fileName); file.SaveAs(fileName); } } context.Response.ContentType = "image/JPEG"; context.Response.Write("File(s) uploaded successfully!"); } }
}
I got the following error when I try to run the web service:
System.Web.HttpContext cannot be serialized because it does not have a parameterless constructor.
I looked up the error message online and many posts I came across suggest that I create a parameterless constructor in my web service class which is responsible for deserializing the binary data. I tried that as you can see in my code, but I still get the same error message. If you think there is something else I should try please help. Thanks in advance for your help.
-
Hi all, I am attempting to save images from the desktop in a folder on the server using the following web service code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.Services;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;namespace MultipleImageUploadDemo
{
/// /// Summary description for SaveImageService
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class SaveImageService : System.Web.Services.WebService
{public SaveImageService() { } \[WebMethod\] public void ProcessRequest(HttpContext context) { if (context.Request.Files.Count > 0) { HttpFileCollection files = context.Request.Files; foreach (string key in files) { HttpPostedFile file = files\[key\]; string fileName = file.FileName; fileName = context.Server.MapPath("~/Upload/" + fileName); file.SaveAs(fileName); } } context.Response.ContentType = "image/JPEG"; context.Response.Write("File(s) uploaded successfully!"); } }
}
I got the following error when I try to run the web service:
System.Web.HttpContext cannot be serialized because it does not have a parameterless constructor.
I looked up the error message online and many posts I came across suggest that I create a parameterless constructor in my web service class which is responsible for deserializing the binary data. I tried that as you can see in my code, but I still get the same error message. If you think there is something else I should try please help. Thanks in advance for your help.
WebMethod attributes creates a method public and visible outside of your class. To enable to any client call that method the framework/compiler will create a proxy for the method (after all not every client what HttpContext is!). As part of that proxy the call and return parameters are serialized into XML or JSON...That's the part where you run into problem. Your input parameter is of type HttpContext, and that type can not be serialized... It means that you can not create a method that will receive such a parameter! In anyway it is sound extremely strange to me! Why should you get HttpContext as call parameter? As it seams you try to upload files via web service call, you should do some search for samples... One I found is here: http://blogs.msdn.com/b/codefx/archive/2012/02/23/more-about-rest-file-upload-download-service-with-asp-net-web-api-and-windows-phone-background-file-transfer.aspx[^]
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V) תפסיק לספר לה' כמה הצרות שלך גדולות, תספר לצרות שלך כמה ה' גדול!