Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. System.Web.HttpContext cannot be serialized because it does not have a parameterless constructor.

System.Web.HttpContext cannot be serialized because it does not have a parameterless constructor.

Scheduled Pinned Locked Moved C#
csharphelpasp-netlinqgraphics
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ASPnoob
    wrote on last edited by
    #1

    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.

    Kornfeld Eliyahu PeterK 1 Reply Last reply
    0
    • A ASPnoob

      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.

      Kornfeld Eliyahu PeterK Offline
      Kornfeld Eliyahu PeterK Offline
      Kornfeld Eliyahu Peter
      wrote on last edited by
      #2

      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) תפסיק לספר לה' כמה הצרות שלך גדולות, תספר לצרות שלך כמה ה' גדול!

      "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups