0

i have a web method that reads a file as MemoryStream, but while testing my service i get this error:

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: Timeouts are not supported on this stream.
   at System.IO.Stream.get_ReadTimeout()
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write4_MemoryStream(String n, String ns, MemoryStream o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write6_MemoryStream(Object o)
   at Microsoft.Xml.Serialization.GeneratedAssembly.MemoryStreamSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
   at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
   at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
   at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
   at System.Web.Services.Protocols.WebServiceHandler.Invoke()

no idea for what happening and confused as i know that there is no TimeOut limit for MemoryStreams. any help is appreciated.

this is my web method:

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public MemoryStream getApp()
{
    string aid = "0";
    if (Context.Request.QueryString["aid"] != null) aid=Context.Request.QueryString["aid"];
    MemoryStream memSTR = new MemoryStream();
    memSTR = CommonClasses.getAppStream(aid);
    return memSTR;
}

and i call my service with this url: http://localhost:3430/idx.asmx/getApp?aid=2018

and this is CommonClasses.getAppStream(aid):

public static MemoryStream getAppStream(string tID)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["DBCnn"].ConnectionString;
            SqlConnection connection = new SqlConnection(connectionString);
            int tID = int.Parse(tID);
            connection.Open();
            SqlCommand command = new SqlCommand("SPGetApp", connection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@AID", SqlDbType.Int).Value = tID;
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult | CommandBehavior.CloseConnection);
            MemoryStream memoryStream = new MemoryStream();
            if (reader.Read())
            {
                var blob = new Byte[(reader.GetBytes(0, 0, null, 0, int.MaxValue))];
                reader.GetBytes(0, 0, blob, 0, blob.Length);
                memoryStream.Write(blob, 0, blob.Length);
            }
            reader.Close();
            connection.Close();
            return memoryStream;
        }

if you think it's may some miss configuration in web.config causes the problem, this is my configuration file content:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <httpRuntime/>
    <webServices>
      <protocols>
        <add name="HttpGet"/>
      </protocols>
    </webServices>
    <pages controlRenderingCompatibilityVersion="4.0"/>
  </system.web>
  <connectionStrings>
    <remove name="LocalSqlServer"/>
    <add name="DBCnn" connectionString="Data Source=.;Initial Catalog=myDatabase;Persist Security Info=True;User ID=myUserID;Password=myPassword; Connection Lifetime=0;Min Pool Size=10;Max Pool Size=300; Pooling=true;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>
  • Show the code from CommonClasses.getAppStream – Sir Rufo May 21 '17 at 09:20
  • thanks for reply, i added all necessary codes. – Masood Abolghasemzadeh May 21 '17 at 10:14
  • At look at https://github.com/Microsoft/referencesource/blob/master/mscorlib/system/io/stream.cs show that the default implementation is to throw an exception for ReadTimeout property and it is not overwritten in MemoryStream, FileStram or BinaryStream. Seems the XmlSerializer fails to check the CanTimeout property which is false by default. – Sir Rufo May 21 '17 at 10:30
  • that's well, by why it fails when every thing is implemented and programmed in a simple and clear way, as you see above? – Masood Abolghasemzadeh May 21 '17 at 10:53
  • It fails because the XmlSerializer reads the Stream.ReadTimeout property and that throws an exception. – Sir Rufo May 21 '17 at 11:06
  • any way to solve the problem? – Masood Abolghasemzadeh May 21 '17 at 11:08
  • You could write a StreamWrapper class based on Stream class – Sir Rufo May 21 '17 at 11:25
  • i don't know how to do, you may help in please – Masood Abolghasemzadeh May 21 '17 at 11:31
  • Have a look at https://msdn.microsoft.com/de-de/library/jj200585(v=vs.85).aspx that wraps an IStream and let it now behave as a Stream – Sir Rufo May 21 '17 at 11:49
  • Possible duplicate of [Creating IStream object in C#](https://stackoverflow.com/questions/12909850/creating-istream-object-in-c-sharp) – Paul Sweatte Jul 07 '17 at 15:26
  • By the way, this question still needs an answer as to why Microsoft is using their own classes incorrectly. writing a SreamWrapper is a fine _workaround_ but it is not a solution. Why does the System.IO.Stream.get_ReadTimeout() function behave like it was overridden in MemoryStream, even though it is [not documented here](https://msdn.microsoft.com/en-us/library/system.io.memorystream%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396)? @SirRufo – NH. Aug 28 '17 at 20:06
  • @NH. The exception is raised because ReadTimeout is **not** overriden (as documented -> just inherited from Stream). How can it behave like being overridden when it does whats implemented in Stream? BTW I am **not** Microsoft or related in any way to them. If you want to know why they use xy in the way they did then you should ask them not me – Sir Rufo Aug 28 '17 at 21:35
  • @SirRufo meet me in https://chat.stackoverflow.com/rooms/7/c – NH. Aug 28 '17 at 22:02

0 Answers0