On this page

Web services - The underlying connection was closed
Working with the sql server 2000 sequences in c#
Lost in Polymorphism world

Ads

Navigation

Search

Categories

Clouds

Sql Server (5) .Net (16) .Net 2.0 (2) C# (3) @ff Topic (5) Architectural solutions (9) ASP (1) BDD (5) Blog related (8) database (2) Development process (8) Facebook (1) job interviews (1) Lessons (5) Life (12) Microsoft (5) IIS 6 (2) SPS (sharepoint server) (3) Drivers (1) Internet Explorer (2) Windows 2003 server (1) NightDuck (2) Performance (5) Security (9) Sql Server 2000 (4) Study (2) TDD (1) Threading (3) Under the hood (1) Web (1) Web services (1) XSS (6)

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 63
This Year: 7
This Month: 0
This Week: 0
Comments: 33

Sign In
Pick a theme:

 Sunday, September 30, 2007
Sunday, September 30, 2007 2:30:05 PM (GMT Standard Time, UTC+00:00) ( .Net 2.0 | C# | Web services )

recently i encountered some errors with web services regarding webService response.

the full Exception that was thown was :
"Server was unable to process request. -> the underlying connection was closed: A connection that was expected to be kept alive was closed by the server"

hmm, not a very nice exception.
digging a little in , i found few reasons that may cause that error.

  • idle time on the client side is greater than on the server side.
  • network issues that prevent the Keep-Alive feature from being committed. (firewalls, proxies and so...).

what is this KeepAlive feature :
this "great" feature actually keeps the connection between the WebService client and server open for a defined amount of time,
thus enhancing WebService interaction performance by eliminating the need to open a new connection each time.

unfortunately, the time for that window is not synchronized between the client and the server.
this situation creates a problem, on which the client may think that the connection is still open, but the server closed it.

so, what can be done so solve that matter ?

  • ensure the reasons above would not happen
  • disable the KeepAlive feature.

since, ensuring that our time is not greater than the one on the server side is impossible when dealing with 3rd party services,
not even mentioning the 3rd party network configuration, the optimal solution for this issue will be disabling the Keep-Alive feature.

how can we do that ?

when adding a web reference with visual studio 2005, it automatically generates a file called reference.cs which is used as a proxy to the web service on the server.
this is the place which we should do the "fix".

there are 2 ways to prevent this certain problem from happening :

since our "proxy" is being generated each time we update our web reference, we would not want the change to be made in the reference.cs file, since it can be overridden the next time we will do "update web reference"

what we can do, is use the "partial class" feature that presented in .Net 2.0 framework to solve this issue.

namespace theNamespaceOfYourServiceAsStatedInTheReferenceFile
{
public partial class MyService
{
/// <summary>
/// this method overides the base getWebRequest,
/// thus preventing the KeepAlive feature
/// </summary>
/// <param name="uri">the given uri</param>
/// <returns>the needed web request with the disabled keep alive feature</returns>
protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
// Do the base class operation and obtain the specific web request
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)base.GetWebRequest(uri);

// do the keep alive handling
return Utils.DisableKeepAlive(webRequest);
}
}

now we need to decide how we are going the disable it(write the DisableKeepAlive in our utils)

/// <summary>
/// This method should disable the keep alive feature
/// </summary>
/// <param name="w">the needed HttpWebRequest</param>
/// <returns>Disabled HttpWebRequest</returns>
public static HttpWebRequest DisableKeepAlive(HttpWebRequest w)
{
// Set the keep-Alive to false, thus for not using it
w.KeepAlive = false;


// the second way
w.ProtocolVersion = System.Net.HttpVersion.Version10;

return w;
}

thats it, now we have successfully disabled the KeepAlive feature

Comments [0] | | # 
 Thursday, July 19, 2007
Thursday, July 19, 2007 8:12:19 PM (GMT Standard Time, UTC+00:00) ( Sql Server  | C# | Sql Server 2000 )

For those who didn't read the previews post about Sequence Implementation In SQL Server 2000 , please read it before continuing.

basically the idea is quite simple but i tough to add this post to complete the sequence framework.
adding some handling functions to your DAL object :

        /// <summary>
        /// this procedure retrieves from the database the value of the desired sequence.
        /// this is a generic function that gets the stored procedure to get the data from and the sequence name itself
        /// </summary>
        /// <param name="seqName">the name of the desired sequence</param>
        /// <param name="storedProcedure">the stored prosedure to call</param>
        /// <param name="connection">the connection to the database</param>
        /// <returns>an integer that represent the value of the sequence</returns>
        internal virtual int GetSequenceID(string seqName,string storedProcedure, SqlConnection connection)
        {
            int ret = int.MinValue;
            
            // Create a command
            SqlCommand command = new SqlCommand(storedProcedure, connection);    
            command.CommandType = CommandType.StoredProcedure;

            // pass the parameter of the sequence name for the series sequence
            SqlParameter p1 = new SqlParameter("@seqName",SqlDbType.VarChar);
            p1.Value = seqName;
            p1.Direction = ParameterDirection.Input;
            command.Parameters.Add(p1);        

            // open connection or using it if already opened
            bool OpenedConnection = false;
            try
            {
                if(connection.State!=System.Data.ConnectionState.Open)
                {
                    OpenedConnection = true;
                    connection.Open();
                }            
                
                // Execute the command
                ret = Convert.ToInt32(command.ExecuteScalar());
            }
            finally
            {
                if ((OpenedConnection) && (connection.State != System.Data.ConnectionState.Closed))
                    connection.Close();
            }
            
            return ret;
        }


        /// <summary>
        /// this is an internal function that retrieves the sequence value
        /// generally, it wraps the other internal method that does the actual work
        /// </summary>
        /// <param name="seqName">the name of the sequence that we want</param>
        /// <param name="storedProcedure">the stored procedure that we address for</param>
        /// <returns>the value of the sequence</returns>
        internal int GetSequenceID(string seqName,string storedProcedure)
        {
            int ret = int.MinValue;

            // Use the connection
            using(SqlConnection connection = DalServices.Connection())
            {
                try
                {
                    // Open the connection
                    connection.Open();
                    
                    // Get the object throught the internal method
                    ret = GetSequenceID(seqName,storedProcedure,connection);
                }
                
                finally
                {
                    // Close the connection
                    if (connection.State != System.Data.ConnectionState.Closed) connection.Close();
                }
            }
            
            return ret;
        }


and add some wrapping functions to extern the methods:

        /// <summary>
        /// The Next value of the series sequence
        /// </summary>
        /// <returns>the value of the sequence</returns>
        public int GetNextSeries()
        {
            // Calling the internall function to get the value
            return GetSequenceID("sq_series","sp_SequenceNextVal");
        }

        /// <summary>
        /// The current value of the series sequence
        /// </summary>
        /// <returns>the value of the sequence</returns>
        public int GetCurrentSeries()
        {
            // Calling the internall function to get the value
            return GetSequenceID("sq_series","sp_SequenceCurrVal");
        }

once again i'm a happy kamper.
Comments [0] | | # 
 Monday, April 16, 2007
Monday, April 16, 2007 11:12:18 AM (GMT Standard Time, UTC+00:00) ( .Net | C# )

ok, this is the point where all the OOP guru's can contribute some of thier knowledge.
i'm facing some strange problem maybe someone can point the solution.

i'm having this code :

public class Father
{
}

public class Son:Father
{
}

public class Tester
{
    public void TestFunc(Father a)
    {
    }

    public void Test()
    {
        Son aSon = new Son();

        TestFunc(aSon);
    }    
}

this code will compile with no problems.

but if i want to pass the object by reference, it will cause a compilation error.

public class Father
{
}

public class Son:Father
{
}

public class Tester
{
    public void TestFunc(ref Father a)
    {
    }

    public void Test()
    {
        Son aSon = new Son();

        TestFunc(ref aSon);
    }    
}

the error i'm getting is something like : "can not convert 'ref son' to 'ref Father' "
does someone knows why ?
Comments [0] | | #