Geeks With Blogs

Pseudo Knowledge Base Useful stuff I've collected... Enjoy.
I have recently started to update a very successful SAP CRM integration I originally built 5 or 6 years ago. We have recently started the implementation of CRM 2011 so the business has decided to take the opportunity to change the data that they want to synchronize between the 2 systems. Luckily the integration is both modular and to a large degree dynamic. The core logic should remain relatively untouched and only the SAP and CRM connection dlls should need any real work.
The changes to the SAP connector were simple and took a matter of a couple of hours. Partially because I still have the virtual development machine I used to create the SAP proxy code.
The Crm connector has been a differnent story. There have been a number of changes to the CRM services since CRM 3. So like all developers I start exploring and testing the old and new ways to communicate with CRM. To that end I created a test project and started by creating a reference to the new CRM service (http://ServerName/OrgName/XRMServices/2011/Organization.svc) .  The first method call to retrieve an account worked without any issues.
 
private CrmService.OrganizationServiceClient service = new CrmService.OrganizationServiceClient();

private void GetAccount_Click(object sender, EventArgs e)
{
   QueryExpression query = new QueryExpression("account");
   ConditionExpression condition1 = new ConditionExpression();

   condition1.AttributeName = "accountnumber";
   condition1.Operator = ConditionOperator.Equal;
   condition1.Values.Add("5500");

   // Build the filter based on the conditions.
   FilterExpression filter = new FilterExpression();
   filter.FilterOperator = LogicalOperator.And;
   filter.Conditions.Add(condition1);

   // Set the Criteria field.
   query.Criteria.AddFilter(filter);

   query.ColumnSet.AllColumns = true;
   ColumnSet columns = new ColumnSet();
   columns.AllColumns = true;
   Entity accnt = service.RetrieveMultiple(query)[0];
}

I then explored the options around early binding and wrote the following method based on a MSDN article (http://msdn.microsoft.com/en-us/library/gg334754.aspx).
 
private void btnEarlyBinding_Click(object sender, EventArgs e)
{
   OrganizationServiceProxy serviceProxy;
  IOrganizationService orgService;

   Uri orgUri = new Uri(@"http://ServerName/OrgName/XRMServices/2011/Organization.svc");

   ClientCredentials credentials = new ClientCredentials();
   credentials.Windows.ClientCredential = new System.Net.NetworkCredential("CrmAdministrator", "XXXXX", "domain");

   serviceProxy = new OrganizationServiceProxy(orgUri, null, credentials, null);

   // This statement is required to enable early-bound type support.
   serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
   orgService = (IOrganizationService)serviceProxy;

   Guid guid = new Guid("f02b9ae2-1aab-db11-964f-000cf15bffae");
   ColumnSet columns = new ColumnSet();
   columns.AllColumns = true;
   object acc = (Account)orgService.Retrieve("account", guid, columns);
}

When this code ran it caused a Null Reference Exception when calling the Retrieve method of the OrganizationServiceProxy object.
I was not able to determine the cause of this problem and there are other options so I continued to test code against the CRM service reference. Unfortunately I hit another issue. My code as mentioned is dynamic and uses reflection to identify the attribute types and update attribute values. Unfortunately with the new CRM service only populated CRM attributes are returned and reflection gives you a null reference error. I was then directed to look at the Metadata service. To my disappointment that resulted in an Invalid Operation Exception - 'Collection was modified; enumeration operation may not execute.' on calling the Execute method on the OrganizationServiceProxy object. See http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/10273be7-22ec-4724-874f-a8f6bd133455 for more info on this issue.
At this point I started to doubt the stability of the environment and started to look for way to isolate the problem. I started to look at the SDK and opened one of the packaged solutions. To my surprise it worked fine. I copied the code that was failing to the SDK project and it also worked. So the code is fine so what is the problem??
I need to know the cause so started comparing all the different characteristics of the 2 projects. As soon as I removed the service reference to the CRM services the early binding code started to work.
When I added it back in I started to get exceptions again.
Hope this helps someone.
Geordie
Posted on Thursday, September 1, 2011 8:32 AM Coding | Back to top


Comments on this post: NullReferenceException when calling the Retrieve method of the OrganizationServiceProxy object

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Geordie | Powered by: GeeksWithBlogs.net