Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Microsoft Dynamics AX 2012 Services

You're reading from  Microsoft Dynamics AX 2012 Services

Product type Book
Published in Dec 2012
Publisher Packt
ISBN-13 9781849687546
Pages 196 pages
Edition 1st Edition
Languages

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:

  • Key components: Just as some components are specific to document services, there are also components that are specific to custom services. Most of these components...

Key components


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

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...

Creating custom services


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.

The Title service

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:

  • Retrieve details of a title based on its ID

  • Retrieve a list of all titles

The Title data contract

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:

[DataContractAttribute('Title')]
public class CVRTitleDataContract
{
  CVRTitleId      id;
  CVRTitleName    name;
  Description     description;
}

Next, add three parameter methods...

Deploy the service


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:

  1. Open the AOT by pressing Crtl + D.

  2. Right-click on the Service Groups node, and then click New Service Group.

  3. Right-click on the newly created service group, and then select Properties.

  4. Change the value of the Name property to CVRCustomServices.

  5. Right-click on the service group and select New Service Node Reference.

  6. Select the service node reference that was added and change the Service property to CVRTitleService.

  7. 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...

Consuming the service


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:

  1. In Visual Studio, create a console application.

  2. Right-click on the Service References node and select the Add Service Reference… button. The Add Service Reference window opens.

  3. 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.

  4. In the Namespace dialog box, specify the namespace that you want to use: AxCustomServicesRef.

After performing these steps, the Add Service Reference window should...

Summary


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.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Microsoft Dynamics AX 2012 Services
Published in: Dec 2012 Publisher: Packt ISBN-13: 9781849687546
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}