title: WCF Service Reference in ASP.net Page - When to Dispose?
body: <p>I have an ASP.net page that is creating a service reference to a WCF service and making calls in multiple places in my page. I instantiate the service reference in Page_Load and have an instance variable to store it:</p>
  
  <pre><code>private FooClient _serviceClient;
  
  protected void Page_Load(object sender, EventArgs e)
  {
      _serviceClient = nwe FooClient();
      _serviceClient.GetAllFoos();
  }
  
  protected void btnSave_Click(object sender, EventArgs e)
  {
      _serviceClient.SaveFoo();
  }
  </code></pre>
  
  <p>I just discovered that I need to be disposing of the service reference when I am done using it or else the connections will be kept alive and will block incoming connections if I reach the max number of connections. Where would the best place to dispose of these references be? I was thinking of doing it on the OnUnLoad event. </p>
  
  <p>Is there a better way of doing this?</p>