ionServiceProxy和OrganizationServ

ionServiceProxy和OrganizationServ

本文介绍了我是否需要同时处置CRM OrganizationServiceProxy和OrganizationServiceContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
 }


推荐答案

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

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。您可以将两个using语句组合使用

btw. you can combine both using statements

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

这篇关于我是否需要同时处置CRM OrganizationServiceProxy和OrganizationServiceContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 06:04