0

I know that when using a stream I need to flush it so it will releases any resources related to the stream and flushing a stream takes any buffered data which hasn't been written yet, and writes it out right away.

I have the following class:

[XmlRoot("EventList")]
public class EventList
{
    private List<BaseEvent> events_ = new List<BaseEvent>();
    /// Default constructor. Required for serialization
    public EventList() {}

    [XmlElement(ElementName = "Event")]
    public List<BaseEvent> Events
    {
        get 
        {
            return events_;
        }
        set 
        {
            events_ = value;
        }
    }
}

now before I send it as a response to HTTPRequest do I need to flush it? if so how can I?

I'm asking because I have problem which described here: AJAX response - XmlHttp.responseXML is cut off

The method being activated by js client is:

[WebMethod]
public EventsRequestResult GetEvents(string agentId, int delayedReturnMS)

// EventsRequestResult - return value of the GetEvents method of the EventsService.
public class EventsRequestResult
{
    private EventList events_ = null;
    /// <summary>
    /// List of the events
    /// </summary>
    [XmlElement(ElementName = "Events")]
    public EventList Events
    {
        get
        {
            return events_;
        }
        set
        {
            events_ = value;
        }
    }
    /// Default constructor.
    public EventsRequestResult(){}
    /// <summary>
    /// Constructor used in case of the failure.
    /// </summary>
    /// <param name="errorCode">Numeric error code</param>
    /// <param name="errorDescription">Human readable error description</param>
    public EventsRequestResult(RequestErrorCode errorCode, string errorDescription) : base(errorCode, errorDescription){}
    /// <summary>
    /// Constructor used in case of successful events request.
    /// </summary>
    /// <param name="events">List of the events</param>
    public EventsRequestResult(EventList events) : base()
    {
        events_ = events;
    }
}
Community
  • 1
  • 1
Dor Cohen
  • 16,769
  • 23
  • 93
  • 161
  • How are you sending it as a response? `(new XmlSerializer(typeof(EventList))).Serialize(theResponse.OutputStream, eventList);` should do the trick if I'm reading the docs right... – AKX Mar 20 '13 at 09:20
  • XmlRoot is orthogonal to the question of flushing a stream; you don't show any of the code that is relevant to that. However, *closing* the stream will also cause it to be flushed. – Marc Gravell Mar 20 '13 at 09:20
  • @AKX I'm sending the response throw regular return statement, exactly like returning regular string – Dor Cohen Mar 20 '13 at 09:46
  • @MarcGravell I didn't understand your comment, how can I close the stream? it is regular class – Dor Cohen Mar 20 '13 at 09:50
  • @DorCohen the code in the question is *unrelated* to anything related to closing, flushing, etc. Somewhere you presumably have some code that *is* related to streams. – Marc Gravell Mar 20 '13 at 09:59
  • @MarcGravell The question was asked to know if I need to do flush/close on my current implementation, meaning that I have WebMethod which returns class that is being serialize by attributes. maybe I misunderstood something? – Dor Cohen Mar 20 '13 at 10:10

1 Answers1

0

Marc is right in his comment above but I think what you're looking for is something like

    using(var stream = theResponse.OutputStream)
    {
        var ser = new XmlSerializer(typeof(EventList));
        return ser.Deserialize(stream) as EventList;
    }

return from within a using block will close (and flush) the stream.

Barracoder
  • 3,696
  • 2
  • 28
  • 31