Thursday, January 20, 2011

CRM 2011 SDK: WinOpportunityRequest and ArgumentNullException

In several of the Dynamics CRM 2011 SDK sample C# files, you'll see this comment and code line:

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


You might think that if you're writing late-bound code, like I was doing earlier today for a data migration project, that you wouldn't need that line of code. That's true if all you're doing is creating Entity instances, setting attributes, and executing CRUD operations (e.g., _serviceProxy.Update(myEntity)).

What I learned the hard way today is that you need that line of code if you're calling the Execute method for the service proxy with an instance of WinOpportunityRequest, LoseOpportunityRequest, and the other "Request" classes. If you don't run that code then you'll probably run into the ArgumentNullException after invoking Execute on the service proxy.

In hindsight, the "Opportunity" part of the name "WinOpportunityRequest" should've led me to think "maybe that's an early-bound class and therefore I need to set that ProxyTypesBehavior thing I read about". But I was convinced I was operating late-bound only. Apparently not. Another lesson learned.

No comments:

Post a Comment