How do I close a database connection in a WCF service?

Posted by Dan on Stack Overflow See other posts from Stack Overflow or by Dan
Published on 2010-04-08T01:00:11Z Indexed on 2010/04/08 1:03 UTC
Read the original article Hit count: 419

Filed under:
|
|
|

I have been unable to find any documentation on properly closing database connections in WCF service operations. I have a service that returns a streamed response through the following method.

    public virtual Message GetData()
    {
        string sqlString = BuildSqlString();
        SqlConnection conn = Utils.GetConnection();
        SqlCommand cmd = new SqlCommand(sqlString, conn);
        XmlReader xr = cmd.ExecuteXmlReader();

        Message msg = Message.CreateMessage(
            OperationContext.Current.IncomingMessageVersion,
            GetResponseAction(),
            xr);

        return msg;
    }

I cannot close the connection within the method or the streaming of the response message will be terminated. Since control returns to the WCF system after the completion of that method, I don't know how I can close that connection afterwards. Any suggestions or pointers to additional documentation would be appreciated.

Dan

© Stack Overflow or respective owner

Related posts about wcf

Related posts about c#