gogetsome
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.StockQu ote ws = new StockQuoteService.StockQuo te())
{
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.N ame, 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.Special ized;
/// <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(GetParam eter("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.StockQu ote ws = new StockQuoteService.StockQuo te())
{
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.N ame, 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];
}
}
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.StockQu
{
this.RawXml = ws.GetQuote(this.Symbol);
}
XmlDocument doc = new XmlDocument();
doc.InnerXml = this.RawXml;
foreach (XmlNode node in doc.FirstChild.FirstChild.
{
this.Parameters.Add(node.N
}
}
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.
using System.Web.UI.HtmlControls
using System.Xml;
using System.Collections.Special
/// <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(GetParam
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.StockQu
{
this.RawXml = ws.GetQuote(this.Symbol);
}
XmlDocument doc = new XmlDocument();
doc.InnerXml = this.RawXml;
foreach (XmlNode node in doc.FirstChild.FirstChild.
{
this.Parameters.Add(node.N
}
}
private string GetParameter(string key)
{
if (this.Parameters[key] == null)
{
throw new Exception(string.Format("I
}
return this.Parameters[key];
}
}
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
This is the actual service: http://www.webservicex.net/WCF/default.aspx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
ASKER
I have learned that Reference.vb is not part of an ASP.net project.
protected override WebRequest GetWebRequest(Uri uri)
{
HttpWebRequest webRequest = (HttpWebRequest) base.GetWebRequest(uri);
webRequest.KeepAlive = false;
return webRequest;
}