CrmService Operations Using a Data Context (UsingService)
![]() |
Given a data context, you can perform all operations supported by the Microsoft Dynamics CRM SDK by using the UsingService method on the data context. This method has two forms. The first form accepts an argument of type Func<IOrganizationService, T>. (Note that IOrganizationService is a proxy for the Microsoft Dynamics CRM SDK service, and supports the same operations.) It can be used to return a value of generic type T in a single expression.
var context = new XrmDataContext();
var reponse = context.UsingService(
service => (WhoAmIResponse)service.Execute(new WhoAmIRequest()));
Console.WriteLine(reponse.UserId);
The second form accepts an argument of type Action<IOrganizationService>, and allows the use of the service in non-expression form:
var context = new XrmDataContext();
ontext.UsingService(service =>
{
var response = (WhoAmIResponse)service.Execute(new WhoAmIRequest());
Console.WriteLine(response.UserId);
});
© 2010 Microsoft Corporation. All rights reserved.
