Chapter 4. Custom Services
The ability to develop custom services in Microsoft Dynamics AX is not new, but the way it is done in Microsoft Dynamics AX 2012 is. Developers can now create a WCF service in a way similar to how they would develop a WCF service in a language like C#. By using attributes to create data and service contracts, development is simplified as you don't have to worry about the technical details of serialization and deserialization. These things are all handled by WCF, which allows you to create powerful services, fast.
At the end of this chapter, you will have learned how to use attributes to create data and service contracts and how to use them to create custom services
. You will also be able to deploy the service and consume it using a WCF application.
The following topics will be covered in this chapter:
In the previous chapter, we discussed the key components of document services. When developing custom services, there are also a few concepts you should be familiar with, starting with attributes.
Attributes are classes that contain data just like normal classes, but the purpose of this data is different. Attributes contain metadata that describes targets. Targets can be of different types such as classes, interfaces, method, events, and so on.
Attributes can either be intrinsic
or custom
. Intrinsic attributes are part of the CLR (Common Language Runtime) and are contained in the .NET framework. Custom attributes are attributes that you can create yourself.
Because attributes contains metadata, they are only useful when reflection is used. An example of this is a DataContract
attribute. The service generation process uses reflection on the classes that the service class uses to determine which of these classes are data contracts.
The following code shows the usage...
In this section, we will discuss two custom services. One service focuses on exposing data from Microsoft Dynamics AX 2012, the other on exposing business logic.
We will use the CVRTitleService
service as an example to demonstrate how to create a simple yet powerful service. The service will allow an external program to do the following two things:
Let's start by creating a new class for the data contract that will contain the data for one title. Create a new class and name it CVRTitleContract
. In the class declaration, add DataContractAttribute
to specify that the class is a data contract. Also declare the variable's ID, name, and description as shown in the following code snippet:
Next, add three parameter methods...
To deploy our custom services, we will use a basic port. For this reason, we need to add the services that we want to deploy to a service group. We will add all of them to one service group: CVRCustomServices. Let us deploy our custom service by following these steps:
Open the AOT by pressing Crtl + D.
Right-click on the Service Groups node, and then click New Service Group.
Right-click on the newly created service group, and then select Properties.
Change the value of the Name property to CVRCustomServices.
Right-click on the service group and select New Service Node Reference.
Select the service node reference that was added and change the Service property to CVRTitleService.
And finally, right-click the service group and click Deploy Service Group to deploy the service.
Now that you have completed these steps, go to System administration | Setup | Services and Application Integration Framework | Inbound ports. The CVRCustomServices inbound port is available there as a basic...
Now that we have created and exposed our custom services, they are ready to be consumed. To do this, we use Visual Studio and write two code samples.
Example 1 – Retrieving titles
This first example of consuming the service deals with the retrieval of a title list. We want to be able to write a list of titles to the console window.
Adding the service reference
To add the service reference, perform the following steps:
In Visual Studio, create a console application.
Right-click on the Service References node and select the Add Service Reference… button. The Add Service Reference window opens.
In the Address drop-down box, specify http://DYNAX01:8101/DynamicsAx/Services/CVRCustomServices as the address for the service and then press Go. The address is queried and the services and operations that are available are listed.
In the Namespace dialog box, specify the namespace that you want to use: AxCustomServicesRef.
After performing these steps, the Add Service Reference window should...
It should be clear that custom services provide a fast and powerful way of exposing data and business logic. Custom services are capable of exposing both simple and complex entities. This makes them an alternative to document services. One aspect custom services are far superior in, is in exposing business logic. This will probably make custom services the preferred method of integration in many of your implementations.
In the next chapter, we will see how we can use custom service and data contracts in the SysOperation
framework.