Do I need to dispose both the CRM OrganizationServiceProxy and the OrganizationServiceContext?

梦想与她 提交于 2019-12-05 04:50:11

问题


Both the OrganizationServiceProxy and the OrganizationServiceContext support the dispose method. Do I need to wrap both of them in a using statement?

using (var proxy = GetOrganizationServiceProxy(Constants.OrgName))
{
    using (var context = new OrganizationServiceContext(proxy))
    {
        // Linq Code Here
    }
 }

Or will disposing of the context close properly close the proxy, meaning only this is needed?

 var proxy = GetOrganizationServiceProxy(Constants.OrgName)
 using (var context = new OrganizationServiceContext(proxy))
 {
     // Linq Code Here
 }

回答1:


The context cannot dispose the proxy, as it cannot decide if it is used by any other object. IF you look into Dispose of OrganizationServiceContext, you'll see

public void Dispose()
{
  this.Dispose(true);
  GC.SuppressFinalize((object) this);
}

protected virtual void Dispose(bool disposing)
{
  if (!disposing)
    return;
  this.ClearChanges();
}

btw. you can combine both using statements

using (var proxy = GetOrganizationServiceProxy(Constants.OrgName))
using (var context = new OrganizationServiceContext(proxy))
{
    // Linq Code Here
}


来源:https://stackoverflow.com/questions/7731854/do-i-need-to-dispose-both-the-crm-organizationserviceproxy-and-the-organizations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!