Link to home
Start Free TrialLog in
Avatar of gogetsome
gogetsomeFlag for United States of America

asked on

Disable keepalive in webservice class

Hello, How can I turn off keep alive in the following class? I'm a newb with c# and with web services and would appreciate the help as I sometimes get the following error:

The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.

on this line of code:
 this.RawXml = ws.GetQuote(this.Symbol);

In this code block:
private void GetQuote()
    {
        using (StockQuoteService.StockQuote ws = new StockQuoteService.StockQuote())
        {
            this.RawXml = ws.GetQuote(this.Symbol);
        }

        XmlDocument doc = new XmlDocument();
        doc.InnerXml = this.RawXml;
        foreach (XmlNode node in doc.FirstChild.FirstChild.ChildNodes)
        {
            this.Parameters.Add(node.Name, node.InnerText);
        }
    }




using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Collections.Specialized;

/// <summary>
/// Summary description for StockQuote
/// </summary>
public class StockQuote
{
    private string _symbol = string.Empty;
    public string Symbol
    {
        get { return _symbol; }
        set { _symbol = value; }
    }

    private string _raw_xml = string.Empty;
    public string RawXml
    {
        get { return _raw_xml; }
        set { _raw_xml = value; }
    }

    private NameValueCollection _parameters = new NameValueCollection();
    public NameValueCollection Parameters
    {
        get { return _parameters; }
        set { _parameters = value; }
    }

    public string Last
    {
        get { return GetParameter("Last"); }
    }

    public DateTime QuoteDateTime
    {
        get
        {
            DateTime dVal = DateTime.Now;
            DateTime.TryParse(GetParameter("Date") + " " + GetParameter("Time"), out dVal);
            return dVal;
        }
    }

    public string Change
    {
        get { return GetParameter("Change"); }
    }


      public StockQuote()
      {
            //
            // TODO: Add constructor logic here
            //
      }

    public StockQuote(string symbol)
    {
        this.Symbol = symbol;
        GetQuote();
    }

    private void GetQuote()
    {
        using (StockQuoteService.StockQuote ws = new StockQuoteService.StockQuote())
        {
            this.RawXml = ws.GetQuote(this.Symbol);
        }

        XmlDocument doc = new XmlDocument();
        doc.InnerXml = this.RawXml;
        foreach (XmlNode node in doc.FirstChild.FirstChild.ChildNodes)
        {
            this.Parameters.Add(node.Name, node.InnerText);
        }
    }

    private string GetParameter(string key)
    {
        if (this.Parameters[key] == null)
        {
            throw new Exception(string.Format("I could not find the key ({0}) you are looking for", key));
        }
        return this.Parameters[key];
    }

}





Avatar of RedKelvin
RedKelvin

Hi, add this to your webservice

protected override WebRequest GetWebRequest(Uri uri)
{
HttpWebRequest webRequest = (HttpWebRequest) base.GetWebRequest(uri);
webRequest.KeepAlive = false;
return webRequest;
}
Avatar of gogetsome

ASKER

Hey RedKevin thanks for your reply! I have seen that bit of code, but don't know where to put it. New to web services. I'm consuming the service from http://www.webservicex.net/WCF/default.aspx.

This is the actual service: http://www.webservicex.net/WCF/default.aspx
ASKER CERTIFIED SOLUTION
Avatar of RedKelvin
RedKelvin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm trying to find the Reference.vb file but it does not seem to be in my project. Actually, Reference.vb is not on my whole computer. How can that be?
I have learned that Reference.vb is not part of an ASP.net project.