Microsoft sample code for accessing Dynamics often looks like this:
static void Main(string[] args)
{
try
{
string connectionString =
"Url=https://myorg.crm.dynamics.com; Username=me@myorg.com; Password=******; authtype=Office365";
using (CrmServiceClient conn = new CrmServiceClient(connectionString))
{
// Cast the proxy client to the IOrganizationService interface.
IOrganizationService orgService = (IOrganizationService)conn.OrganizationWebProxyClient ??
conn.OrganizationServiceProxy;
Console.WriteLine("Microsoft Dynamics CRM version {0}.", ((RetrieveVersionResponse)orgService.Execute(new RetrieveVersionRequest())).Version);
}
}
catch (FaultException<OrganizationServiceFault> osFaultException)
{
Console.WriteLine("Fault Exception caught");
Console.WriteLine(osFaultException.Detail.Message);
}
catch (Exception e)
{
Console.WriteLine("Uncaught Exception");
Console.WriteLine(e);
}
}
}
But it is equally possible (and simpler) to use the Crm Service Client directly, like this:
class Program
{
static void Main(string[] args)
{
try
{
string connectionString =
"Url=https://myorg.crm.dynamics.com; Username=me@myorg.com; Password=******; authtype=Office365";
using (CrmServiceClient conn = new CrmServiceClient(connectionString))
{
Console.WriteLine("Microsoft Dynamics CRM version {0}.", ((RetrieveVersionResponse)conn.Execute(new RetrieveVersionRequest())).Version);
}
}
catch (FaultException<OrganizationServiceFault> osFaultException)
{
Console.WriteLine("Fault Exception caught");
Console.WriteLine(osFaultException.Detail.Message);
}
catch (Exception e)
{
Console.WriteLine("Uncaught Exception");
Console.WriteLine(e);
}
}
}
My question: Why use that IOrganizationService property ever? It seems as if it has only a subset of the functionality of the CrmServiceClient. And CrmServiceClient used directly seems both faster, simpler, more efficient, and more feature-rich.
Any idea about why the sample code always has this additional layer of indirection?
Thanks.