How do I close a database connection in a WCF service?
- by Dan
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