我是否需要同时处理CRM OrganizationServiceProxy和OrganizationServiceContext?

时间:2011-10-11 20:06:42

标签: dynamics-crm-2011

OrganizationServiceProxy和OrganizationServiceContext都支持dispose方法。我是否需要将它们包装在using语句中?

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

或者正确处理上下文关闭代理,这意味着只需要这个吗?

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

1 个答案:

答案 0 :(得分:6)

上下文无法处理代理,因为它无法确定它是否被任何其他对象使用。 如果您查看OrganizationServiceContext的Dispose,您将看到

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

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

顺便说一句。你可以结合使用语句

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