I write this article to summary how I instantiate the OrganizationServiceProxy and OrganizationServiceContext using CRM SDK Helper Code.
As research on MSDN about "Use the Early Bound classes in code", we all know that in order to access to the CRM Organization Web Services, we must using OrganizationServiceProxy or OrganizationServiceContext (For more information, please refer to MSDN : Use the Early Bound Entity Classes in Code).
In Microsoft Dynamics CRM 2011, you can use the OrganizationServiceProxy class to access the Web services. Alternatively, you can use the OrganizationServiceContext generated by the code generation tool to gain access to additional functionality. There instantiate approach seem to be various, but the following is the most common steps which get CRM SDK HelperCode involved:
1. Create Early Bound Entity Classes with the Code Generation Tool (CrmSvcUtil.exe) :
As preventing the duplicate topic, below is the command I used to create ContosoGenerated.cs file from Organization name: Contoso, including the ContosoServiceContextName class as the service context class.
CrmSvcUtil.exe /url:http://localhost:5555/Contoso/XRMServices/2011/Organization.svc/out:ContosoGenerated.cs /username:rdadmin /password:Password1 /serviceContextName:ContosoServiceContextName
2. Solution structure:
I created the Console C# project as the below structure:
- ContosoGenerated.cs: The generated class from CrmSvcUtil.exe
- crmservicehelpers.cs : Add existing class from <CRM SDK Directory>\sdk\samplecode\cs\helpercode\crmservicehelpers.cs
- deviceidmanager.cs: Add existing class from <CRM SDK Directory>\sdk\samplecode\cs\helpercode\deviceidmanager.cs
- Business.cs : The main business class.
- Reference Assembly: there are many assemblies need to reference from both .NET, Window Update and CRM 2011 SDK.
3. Instantiate OrganizationServiceProxy and OrganizationServiceContext
//Init the server configuration ServerConnection serverConnect = new ServerConnection(); ServerConnection.Configuration config = serverConnect.GetServerConfiguration();
The ServerConnection class provide the way to get the server connection and configuration. After the first successful run, The default path to the C:\Users\rd-admin.RD-CRM04\AppData\Roaming\CrmServer\Credentials.xml will store the server configuration, you can tailor it by modify the crmservicehelpers.cs class.
<?xml version="1.0" encoding="utf-8"?> <Configurations> <Configuration> <ServerAddress>localhost:5555</ServerAddress> <OrganizationName>Contoso</OrganizationName> <DiscoveryUri>http://localhost:5555/XRMServices/2011/Discovery.svc</DiscoveryUri> <OrganizationUri>http://rd-crm04:5555/Contoso/XRMServices/2011/Organization.svc</OrganizationUri> <HomeRealmUri> </HomeRealmUri> <Credentials> <UserName>rd-admin</UserName> <Domain>crm04</Domain> </Credentials> <EndpointType>ActiveDirectory</EndpointType> <UserPrincipalName> </UserPrincipalName> </Configuration> </Configurations>
Create Service Proxy using GetOrganizationProxy method:
//Init the OrganizationServiceProxy OrganizationServiceProxy serviceProxy = ServerConnection.GetOrganizationProxy(config); //This statement is required to enable early-bound type support serviceProxy.EnableProxyTypes();
Instantiate the Service Context class by pass an instance of the OrganizationServiceProxy class
//Instantiate the Service Context class by pass an instance of the OrganizationServiceProxy class ContosoServiceContextName context = new ContosoServiceContextName(serviceProxy);