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
}
最佳答案
上下文无法处理代理,因为它无法决定它是否被任何其他对象使用。
如果您查看 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
}
关于dynamics-crm-2011 - 我是否需要同时处理 CRM OrganizationServiceProxy 和 OrganizationServiceContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7731854/