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=9.0.0.0, 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 (8.0.0.0) and Microsoft.Xrm.Sdk (9.0.0.0) assemblies were referenced in the visual studio project . The Microsoft.Crm.Sdk.Proxy (8.0.0.0) assembly references (or looks for) Microsoft.Xrm.Sdk (8.0.0.0), 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.
Interestiing read