问题
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