title: SPWeb disposing himself and causing exception
body: <p>When trying to use <code>SPWeb.GetSiteData(SPSiteDataQuery)</code> (Trying to bind some data to SPGridView), it throws me an TargetInvocationException with a following stacktrace:</p>
  
  <h2>Exception</h2>
  
  <pre><code>  at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct&amp; sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
     at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
     at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
     at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
     at System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method, Boolean disposeInstance, Object&amp; instance)
     at System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments)
     at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
     at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
     at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
     at System.Web.UI.WebControls.GridView.DataBind()
     at Balticovo.SharePoint.WebParts.CrossSiteDataQueryWebPart.OnLoad(EventArgs e)
  </code></pre>
  
  <p>The method that is being invocated is GetData. After investigating InnerException with message <code>Exception from HRESULT: 0x80020009 (DISP_E_EXCEPTION))</code> it looks like SharePoint want to release resources somwhere:</p>
  
  <pre><code>   at Microsoft.SharePoint.Library.SPRequest.ReleaseResources()
     at Microsoft.SharePoint.SPRequestManager.Release(SPRequest request)
     at Microsoft.SharePoint.SPWeb.Invalidate()
     at Microsoft.SharePoint.SPWeb.Close()
     at Microsoft.SharePoint.SPWeb.Dispose()
     at Balticovo.SharePoint.Extensions.GetCrossSiteData(SPWebApplication webApp, SPCrossSiteDataQuery query)
     at Balticovo.SharePoint.TasksTableAdapter.GetData(Guid queryId, Boolean dataForOutlook)
     at Balticovo.SharePoint.TasksTableAdapter.GetData(Guid queryId)
  </code></pre>
  
  <p>But all those methods up to SPWeb.Dispose() I don't dispose anything before I call SPWeb.GetSiteData(SPSiteDataQuery) which then throws an exception.</p>
  
  <h2>My Code</h2>
  
  <p>Instance of SPWebApplication i get like this - <code>Microsoft.SharePoint.WebControls.SPControl.GetContextWebApplication(HttpContext.Current);</code></p>
  
  <p>Then i do some stuff like this in function GetCrossSiteData:</p>
  
  <pre><code>foreach (string siteUrl in query.QuerySites)
  {
     try
     {
        using (SPSite site = new SPSite(siteUrl))
        using (SPWeb web = site.OpenWeb())
        {
           DataTable dt = web.GetSiteData(query.SiteDataQuery); //Hangs here
           ....
        }
     }
  }
  </code></pre>
  
  <p>Any ideas? Thank you.</p>