1
votes

I try to send HttpWebRequest by Socket and when I want to Serialize it, I get this Exception:

An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll Additional information: Type 'System.Net.WebRequest+WebProxyWrapper' in Assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

This is My Code:

 [Serializable()]

class BROWSER
{
    HttpListener Response = null;
    HttpListenerContext context = null;
    HttpListenerRequest request = null;
    HttpListenerResponse response = null;
    public HttpWebRequest Webrequest = null;
    public WebResponse Webresponce = null;
    string responseString = null; 
    //when my Webrequest is ready ...
      public void SendToProxyServer(HttpWebRequest Webrequest)
    {
        byte[] SndData;
        S_RSocket sock = new S_RSocket();
        SndData = Serializ.seryaliz(this.Webrequest);
        sock.Send(SndData);
        this.responseString = sock.res;

    }

and My Serialize Class :

abstract class Serializ
 {
   #region Filds
   static MemoryStream ms = null;
   static BinaryFormatter formatter = null;
   #endregion
   #region Methods
   public static byte [] seryaliz(object obj)
   {
       ms = new MemoryStream();
       formatter = new BinaryFormatter();
       formatter.Serialize(ms, obj);
       ms.Position = 0;
       String tmp = null;
       StreamReader sr = new StreamReader(ms);
       tmp = sr.ReadToEnd();
       ms.Position = 0;
       sr.Close();
       ms.Close();
       return Encoding.ASCII.GetBytes(tmp);
   }
   public static object DEseryaliz(byte [] Data)
   {
       object obj = new object();
       formatter = new BinaryFormatter();
       ms = new MemoryStream(Data);
       ms.Position = 0;
       obj=(object)formatter.Deserialize(ms);
       ms.Close();
       return obj;

   }
   #endregion 
 }

I am Setting my Class as Serializable() but it was not working.

Where is the problem?

2

2 Answers

2
votes

You can't serialize an HttpWebRequest because it, or a property it has, cannot be serialized because they don't have the Serializable attribute. In this case, it's a property inside HttpWebRequest. Adding Serializable to your class that is doing the serializing won't do anything.

2
votes

where is problem ?

The problem is perfectly described in the exception:

Type 'System.Net.WebRequest+WebProxyWrapper' in Assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

Just because you mark something as [Serializable()] doesn't mean it actually can be per the SerializableAttribute Remarks

Apply the SerializableAttribute attribute to a type to indicate that instances of this type can be serialized. The common language runtime throws SerializationException if any type in the graph of objects being serialized does not have the SerializableAttribute attribute applied.

One of your fields or properties is using a WebRequest which internally has a class WebProxyWrapper that is not marked as serializable. Thus you cannot serialize your WebRequest because one of its fields or properties is using the WebProxyWrapper.