D365FO - Data integration by OData (Part 1 of 5)
D365FO - Data integration by OData (Part 2 of 5)
D365FO - Data integration by OData (Part 3 of 5)
D365FO - Data integration by OData (Part 4 of 5) You are here!
D365FO - Data integration by OData (Part 5 of 5)
Create OData client application
So now, it's time to create ODataUtility.
Why we need this?
This utility will help us to generate all exposed OData service endpoints in D365FO (including one we created before) to be the proxy classes. Then we can connect to those D365FO data entities easily by C# code.
Create ODataUtility
First, we create a C# console application project. Name it as 'ODataUtility'.
Next, use 'Manage NuGet Packages...' to add the following reference.
- Microsoft.IdentityModel.Clients.ActiveDirectory
- Microsoft.OData.Client
Then, at Visual Studio go to Tools -> Extensions and Updates and search by 'OData Client code'.
Download and install 'OData v4 Client Code Generator'.
Next, add a 'ODataClient.tt' OData Client into the project.
You can click here OK or Cancel. If click ok, it means the first time it generates without the MetadataDocumentUri. The result will be the same eventually.
Then we put this value https://usnconeboxax1aos.cloud.onebox.dynamics.com/data/$metadata to the MetadataDocumentUri
Before
After
Then, save ODataClient.tt file, and now it's time to click OK and generate the template (proxy classes).
**Tip: The generate process take few seconds, do not interrupt Visual Studio before the generating finished.
The new created template is ODataClient.cs with around 70 MB file size.
So, the last step is to build the project.
Actually, we should get it finish here,
however there are some bug from OData V4 client code generator including the problem that the current version of Visual studio in OneBox machine is VS 2015 which cannot handle such a huge file like ODataClient.cs.
So, when we build the project, we get the error message as follows.
Error CS8103 Combined length of user strings used by the program exceeds allowed limit. Try to decrease use of string literals.
**Thanks toryb comment from this link https://github.com/OData/odata.net/issues/1220 His method works and very useful!
Here is my workaround guideline as per toryb's comment.
1. Close Visual Studio.
2. Copy ODataClient.cs file from VS project folder to another place.
3. Do backup that file in your own way.
4. Open ODataClient.cs by your desired text editor.
5. Search and list with 'Edmx' keyword.
You will find that area of Edmx variable are between line 50,995th and 325,713th !!
6. Cut the XML part (start at
<edmx:Edmx... and
...</edmx:Edmx> at the end) and paste to the new file.
7. In the new file, replace all "" (2 double quote) with " (single double quote) because they were generated incorrectly. This step will take some minutes.
8. Save edmx.xml file.
9. Back to ODataClient.cs file, put the file name as the value of Edmx variable like this.
private const string Edmx = @"edmx.xml";
10. Search by 'CreateXmlReader' keyword and create an additional overload of CreateXmlReader() that does not take any parameters as follows.
Before
After
11. Replace
global::System.Xml.XmlReader reader = CreateXmlReader(Edmx);
with
global::System.Xml.XmlReader reader = CreateXmlReader();
12. Save ODataClient.cs file.
13. Copy ODataClient.cs and Edmx.xml back to VS project folder.
14. Launch Visual Studio
15. Add Edmx.xml file into the project and update it's Build Action is "Content" and the Copy to Output Directory is set to "Copy if Newer" or "Copy Always". This will make sure the xml file is distributed with the library / application.
So now, rebuild the project again. And the result should be ok as follows.
=== End of Part 4 ===