Creating a custom service in ax 2012

Hi Experts,

I just thought of updating AIF stuff. Let’s start on digging on creating custom service. Before that, let’s elaborate custom service.

A custom service in Dynamics AX allows any X++ logic to be exposed as a service. Custom services are normal X++ classes decorated with attributes, which allow any existing methods to be exposed as service operations without writing any additional code.

In this, we will create a new custom service with a single, simple operation. The operation will accept salesId of sales order and return sales name.

Following are the steps…

1. In the AOT, create a new class named CustomServiceDemo with the following code:

class CustomServiceDemo
{

}
[SysEntryPointAttribute]
Public SalesName getSalesName(SalesId _salesId)
{
return SalesTable::find(_salesId).SalesName;
}

9

2. Set the class properties as follows:

1

3. In the AOT, create a new service with the following properties:

2

4. Expand the newly created service and choose the Add Operation option from the Operations node’s right-click context menu.

5. On the Add service operations form, select the getSalesName line by marking the Add checkbox and clicking on OK:

3

6. The service in the AOT should look like the following screenshot:

4

7. In the AOT, create a new service group named CustomServiceSalesName.

8. In the service group, create a new service node reference with the following properties:

 5

9. Deploy the service group by selecting the Deploy Service Group option from its right-click context menu. The Infolog should display a number of messages about successful deployment.

10. Open System administration | Setup | Services and Application Integration Framework | Inbound ports to check the newly deployed service:

6

How it works…

In Dynamics AX, any class can be a custom service. Here, we create a new one with a single method that accepts salesId  and returns the sales name. To enable the method as a service operation, we specify the SysEntryPointAttribute attribute at the top of the method, which will ensure that the method is available in the service operation list when creating service nodes. We also set the class to run on the server.

Next, we create a new service node and add the newly created operation to it. In order to deploy it, we also have to create a new service group that includes the created service. Once deployed, a new record is created in the Inbound ports form.

If everything is successful the service will be ready to be consumed, which is explained in the next step.

Consuming a custom service

Custom services are consumed in a way quite similar to any other Dynamics AX service.

The difference is that each custom service can have a totally different set of operations, where the system or document services always expose the same list operations.

In this recipe, we will create a .NET console application to demonstrate how to consume a custom service. We will use the service created in the Creating a custom service recipe, which returns a sales name of the provided salesid.

How to do it…

Carry out the following steps in order to complete this recipe:
1. In Visual Studio, create a new Visual C# Console Application project named
ConsumeServiceSalesName.
2. Add a new service reference named CustomServiceSalesName to the project.
3. Copy the address from the WSDL URI field, from the Creating a custom service recipe, into the Address field:
7

4. Add the following code to the top section of the Program.cs file:

using customServiceSalesName.CustomServiceSalesName;

5. Add the following code to the Main() method:

CustomServiceSalesNameClient serviceClient = new CustomServiceSalesNameClient();
CallContext context = new CallContext{Company = “UK13”};
string SalesName = serviceClient.getSalesName(context, “SO000395276-UK13”);
Console.WriteLine(SalesName);
Console.ReadLine();

6. Run the program to display the results as shown in the following screenshot:

8

How it works…

We start this recipe by creating a new Visual C# Console Application project and adding a new service reference pointing to the address from the previous recipe. Then, we allow the use of the type in a namespace.

The code in the Main() method is very similar to the other recipes. Here, we create a new service client object and call its getSalesName() operation to find the sales name.

Hope you understood my stuff. Happy DaXing…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: