We help IT Professionals succeed at work.

Disable keepalive in webservice class

gogetsome
gogetsome asked
on
3,825 Views
Last Modified: 2011-04-14
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];
    }

}





Comment
Watch Question

Hi, add this to your webservice

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

Author

Commented:
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
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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?

Author

Commented:
I have learned that Reference.vb is not part of an ASP.net project.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.