There is debate regarding whether to use early bound or late bound when developing integrations with Dynamics CRM. Here we will go into some background.
Early bound is a wrapper around CRM that can help with the speed of the development process. An example of early bound is:
Account account = new Account { Name = "Coffee Company" };
_accountId = _serviceProxy.Create(account);
You can see here we have the entity names available to us, and we can then access the attributes associated with that entity to develop our solution.
Late Bound is a generic way to integrate to CRM:
Entity e =new Entity();
e.LogicalName = "account";
e.Attributes["name"] = "Coffee Company";
_serviceProxy.Create(e);
There are no entities available while we are typing – we need to know what each of these entities and attributes are. You will want to ensure you are spelling names correctly, as these errors would not be picked up at compile time.
There are various debates around performance of the two, with late bound being the faster if at all. In some cases the performance may be about equal.
Let’s go through an example of creating an integration using early-bound.
Firstly, we will need to create classes using the CrmSvcUtil.exe tool. This is a utility in the CRM SDK that is located in the SDK\Bin folder. This will generate the strongly typed .NET framework classes you can use in your code:
To run the tool, open a command prompt and browse to the CrmSvcUtil.exe folder.
The command to run the tool is in the format:
CrmSvcUtil.exe /url:https://<organizationUrlName>.api.crm.dynamics.com/XRMServices/2011/Organization.svc /out:<outputFilename>.cs /username:<username> /password:<password> /namespace:<outputNamespace> /serviceContextName:<serviceContextName>
If you run into issues getting the username and password to recognize or an ACCESS DENIED issue, you can add /il to be prompted for the authentication.
Once this is run, you will get a success message:
filename.cs
You can then use that file in your Visual Studio solution. I have a project which connects to CRM and I want to use these new classes in the project. To do that, I add the file generated from the utility, in my case classes.cs:
You can see the file generated has the CRM schema with entities and attributes. Note if there are changed on the CRM side, you will need to regenerate this file. E.g. if a field schema name changes:
Next, add System.Runtime.Serialization.dll as a reference to the project.
You should now be able to add CRM code to the Visual Studio project.
Let’s do a simple example and add a new account:
If we go to CRM, we can see the Early Bound Test account has been created.
Let’s now do the same test with a late bound account. The code:
In CRM, we see our late bound test:
We can achieve the late bound insert without having to go through the CrmSvcUtil.exe code, so there is an advantage there.
You can decide for yourself given the situation and requirements which is the appropriate one to use.
I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.
IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.
THANK YOU, AND LET'S KEEP LEARNING TOGETHER.
CARL
Great article man! Was just searching something and you popped up. I always read your stuff if you popup in a search.
Hi Carl,
I have just user your tool to generate Latebound classes. It iws great. Thank You very much. Though I have one question. Your tool does not create constants for address fields on account, contact etc. Is there a possibility for you to have this fixed?
Early Bound may make development easier having strongly typed classes, but Late Bound is king when making code dynamic and future proof. This means our CRM developer can add new Entities and Attributes, but I never have to regenerate classes to work with it..
Early Bound may make development easier having strongly typed classes, but Late Bound is king when making code dynamic and future proof. This means our CRM developer can add new Entities and Attributes, but I never have to regenerate classes to work with it.
Also you can generate early-bound classes with the Unchase Dynamics365 Connected Service – https://marketplace.visualstudio.com/items?itemName=Unchase.unchasedynamics365connectedservice