【发布时间】:2016-02-24 09:37:36
【问题描述】:
概述:
我在 XRM 项目中遇到了一些初始化代码,其中正在初始化的实例实现了 IDisposible,但实例上没有围绕 using 块。
在 looked at 的示例中,在 using 块内有一个在服务上调用的实例方法。但在我下面的例子中,服务实例刚刚被初始化。直到在私有方法的代码中进一步调用服务方法本身才会被调用。
问题:
如何使用 using 块进行服务实例初始化?
代码示例 1:(服务初始化)
public static void Init(string connectionString, string name)
{
instanceName = name;
CreateSourceServiceObjects(connectionString);
}
//Service instances just Init here no methods are called:
private static void CreateSourceServiceObjects(string connectionString)
{
var connection = CrmConnection.Parse(connectionString);
sourceService = new OrganizationService(connection);
sourceContext = new OrganizationServiceContext(sourceService);
}
//Example of where the sourceService method's are used:
public static Entity GetUserInfo(Guid userId)
{
Entity systemuser = sourceService.Retrieve("systemuser", userId, new ColumnSet(true));
return systemuser;
}
代码示例2:(我尝试实现using语句)
private static void CreateSourceServiceObjects(string connectionString)
{
var connection = CrmConnection.Parse(connectionString);
//Added a Using block to auto dispose OrganizationService and OrganizationServiceContext
using(sourceService = new OrganizationService(connection))
using (sourceContext = new OrganizationServiceContext(sourceService))
{
//should there be any code in here?
}
}
【问题讨论】:
标签: c# dynamics-crm idisposable using-statement xrm