How to use the Dynamics 365 Business Central API with C# in Service-to-Service scenarios
Business Central
Dynamics 365
Tech
A question that was raised more frequently than others lately is, how to use the Business Central API from C# in Service-to-Service scenarios?
Within the Business Central community, C# is the most frequently used language to code Azure Functions and Windows Services using DotNET core.
There are a few choices to make when deciding how to design your code and we’ll discuss the most important ones being security and structure of your classes.
Security
When you connect to Business Central using the API you have two choices. OAuth with tokens or Basic with a Web Service Access Key.
Using OAuth is the preferred solution which is promoted by Microsoft. The problem is that OAuth is only supported when the software calling into Business Central has the ability to provide a user interface.
This user interface is required to show a window to login and return the access token.
Business Central currently does not support OAuth in service-to-service scenarios except for the API that is handling the service tier itself.
That leaves you with the choice of going back to Basic authentication with a Web Service Access Key or to use Azure as the middle man.
Most examples that you can find online of authentication use the latter option and therefor this blog is about the other option that is hard to find information on.
Simplicity is the core value of Business Central and even though customers using Business Central have an Office 365 tenant, not everyone has their Azure tenant configured.
Using Azure as the middle man for security may be the easy way out for a developer, it requires each user of your solution to set this up in their Azure tenant.
The advice is to use Basic Authentication for Service-to-Service connections until Microsoft is finished with a final solution for OAuth in these scenarios.
Classes
There are two options when you design your solution to structure your classes. You can have them generated based on the metadata of the API endpoint or you can design your own classes.
Both options have pros and cons and the most important reason why the example repository creates its own classes is again simplicity and to explain clearly how the API works rather than working through a pile of generated code that will leave 95% unused.
In real life implementations, many developers will choose to generate the classes based on the endpoint, especially if the API has strong versioning like the one provided by Microsoft.
A strong exception would be if you only need a few API methods implemented or if the API is a custom API for a project and subject to change.
If changes to the API happen on a frequent basis, which is not recommended but happens often in real-life scenarios, you can be more flexible in your error handling if you define your classes yourself.
Rest Client
To send data back and forth your code needs a Rest Client and, in the example, we use the one from RestSharp. This is a very popular NuGet package. Another frequently used Rest Client is the Simple Client (Simple.OData.Client) which is also available as NuGet package.
Json Handling
Json is not native to C#. It was created for JavaScript and in order to use it in C# you can use either NewtonSoft or the new System.Text.Json from Microsoft. Both are created by the same person who works for Microsoft now.
You will most likely find out that both libraries have scenarios where they work and fail.
NewtonSoft Linq supports the use of JArray and JObject in a similar way as AL has the native JsonArray and JsonObject although you will find the NuGet libraries much more flexible and powerful.
Value
Each result that you get from Business Central has a wrapper with a tag called [Value]. In almost all cases you won’t need this in your code. The function ConvertToDataSet converts the result of an API call into a Json Array without the [Value] wrapper.
The API Endpoint
When Business Central is hosted by Microsoft the API endpoint has up to 6 variables that determine things like the Sandbox you are using (if any), the Tenant ID (obsoleted by OAuth) and the details about the API version, and group.
Resources & Next Steps
- QBS Workshop: APIs in Dynamics 365 Business Central
- QBS GitHub: API Exemples
- Kaufmann.nl: Deprecation of basic auth for Saas has been postponed to 2022
- Kaufmann.nl: Service-to-service authentification for automation APIs in Business Central
- Youtube: How to create and use a Busienss Central API
Sie möchten mehr zum Thema "APIs in Buisiness Central" erfahren?
Die QBS Academy bietet einen 6-stündigen Online-Workshop, der spezifisch die Einsatzmöglichkeiten von APIs in Dynamics 365 Business Central behandelt.
Mehr Artikel
Das könnte Sie auch interessieren
-
QBS Azure Services – SOCITAS GmbH & Co. KG
SOCITAS netrachtet seine Lieferanten-Kunden-Beziehungen als enge Partnerschaften, in denen sich beide Seiten gleichermaßen um den Geschäftserfolg kümmern, ist es für SOCITAS äußerst wichtig, seinen Kunden nur qualitativ hochwertige Dienstleistungen anzubieten. Dies gilt auch auf das Hosting von Dynamics NAV bzw. Business Central.
-
Business Central on premises: Essential- und Premium-Benutzer
Beim Umstieg von NAV auf Business Central on premises gibt es hinsichtlich des Funktionsumfanges (Essential vs. Permium) einiges zu beachten. Der Beitrag befasst sich mit den wichtigsten Aspekten des Up- oder Downgrades von Benutzerlizenzen.
-
Position NAV as stepping stone for more Dynamics 365 ‘Tenerife’ deals
Since the introduction of the Dynamics 365 Business Edition in the US and Canada late 2016, the position of Dynamics NAV is under pressure! Competitors took the chance to position NAV as an old-fashioned solution and that’s a direct threat to your NAV business!
Was andere über QBS group sagen
- Michael Preiß
Geschäftsführer
“Einfach der beste Preis, der je für das Hosting von Dynamics NAV bzw. Business Central gesehen wurde.”
- Marius Giovanelli - service4work IT Solutions GmbH
Geschäftsführer
"Wir fühlen uns durch QBS sehr wohl mit der Betreuung und gut in die Microsoft Welt aufgenommen."
- gbedv GmbH & Co KG - Georg Böhlen
Geschäftsführer
“Unsere Dynamics Upgrade Projekte schnell und zuverlässig umgesetzt.”
- Ferry Schrijnewerkers
Owner en Co-founder at MKB365
Since the start of our company 4 months ago, we have already completed 12 Dynamics 365 for Sales implementations.
- synko - Markus Köhler
Geschäftsführer
"Die QBS group hat uns vom ersten Tag an unterstützt und wir merken, dass die QBS möchte, dass wir erfolgreich werden."
- SONAPRO - Helmut Bernhart
Geschäftsführer
"An der Zusammenarbeit mit der QBS schätzen wir besonders den persönlichen Kontakt über unsere Ansprechpartnerin."
- ABILITY - Roland Wilhelm
Geschäftsführer
"Bislang wurden die Erwartungen und Hoffnungen in die Zusammenarbeit erfüllt. Das komplette QBS Team ist sehr zuverlässig."
- anaptis - Markus Thyen
Geschäftsführer
"Als wir das QBS group Konzept kennengelernt haben, hat man offene Türen eingerannt. Unsere Erwartungshaltung wurde voll erfüllt."
- acadon - Klaus Fander
CEO
"Ich kann mit gutem Gewissen sagen, dass jeder Partner für sich einen Nutzen aus der Partnerschaft mit QBS group ziehen kann."
- vinor - Rainer Vincke
Geschäftsführer
"Wir erhalten Informationen schneller und haben durch QBS Zugriff auf vielfältiges Know How auch aus anderen Branchen."
- Joaquim Antón Gimeno
GRUPO CHOICE
"Our relationship with QBS group allows us to improve our marketing strategy."
- Herke ICT Group- Karing Buzing
We are once again part of a NAV community.
- Owe Loonstra - Verito
Manager Sales & Marketing
The fact that we belong to a larger organisation gives our customers security.