Updated: Jun 8
The inspiration for today's blog post came while creating .NET, c# console applications to fix data and perform repetitive admin tasks in a clients Dynamics 365 instance. Keep reading to find the underlying cause of System.IO.FileNotFoundException and how assembly binding will resolve the error.
A number of the console apps built failed at run time with the error;
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Xrm.Sdk, Version=126.96.36.199, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. (I wish I had taken a screen shot).
In general this error can occur when one assembly referenced in your visual studio project references a second assembly in your visual studio project how ever the second assembly is a different version. I know that is a heavy sentence to understand so there is an example in the next paragraph.
The error was occurring because the Microsoft.Crm.Sdk.Proxy (188.8.131.52) and Microsoft.Xrm.Sdk (184.108.40.206) assemblies were referenced in the visual studio project . The Microsoft.Crm.Sdk.Proxy (220.127.116.11) assembly references (or looks for) Microsoft.Xrm.Sdk (18.104.22.168), which does not exist in the project therefore the System.IO.FileNotFound exception occurs.
The quick fix is to add the below XML to the console applications app.config. This configures assembly binding which effectively is saying use Microsoft.Xrm.Sdk version 9 when trying to find Microsoft.Xrm.Sdk version 0 through to 9.
The below links from Microsoft will do a far better job than I could at explaining assembly versioning.