How to: Add a Service Operation to a Service

This topic describes how to add a custom service operation to a service in Application Integration Framework (AIF). This topic shows you how to add a service operation to one of the existing document service classes. However, you follow the same method to add a service operation to any class that's exposed as a service in Microsoft Dynamics AX. An example of a custom method on one of the document service classes that are included with Microsoft Dynamics AX is the TrvExpenseService.Submit method.

Each document service class can contain one or more of the following six methods.

  • create

  • delete

  • find

  • findKeys

  • read

  • update

These methods support document functionality that enables you to perform create, read, update, and delete operations on data in Microsoft Dynamics AX. For more information, see Document Class Service Operations.

When you add a custom service operation to a service in Microsoft Dynamics AX, the service operation must follow these rules:

  • The method that is exposed as a service operation must be public.

  • The method that is exposed as a service operation should not be static. An X++ class that is exposed as a service can contain a static method. However, if that static method is exposed as a service operation, it will generate an exception when the service is generated.

  • If the service operation takes an object as a parameter or returns an object, that object class must implement the AifXmlSerializable interface.

  • Parameters or return values that are not objects must be one of the primitive types that include the following: str, date, utcdatetime, guid, int, int64, enum, real, or void.

  • The service operation name should not be a .NET Framework language keyword, otherwise an error is generated. This is because a .NET Framework Web services proxy class is generated for your service.

When adding a service operation, the first step is to add the method to the service class. As an example, we'll add a method to the CustCustomerService class. The new method adds two integers and returns the result.

  1. Open the AOT, expand the Classes node and navigate to the CustCustomerService class.

  2. Right-click the class, and then select New Method.

  3. In the Code Editor, enter the following code.

    public int addIntegers(int _int1, int _int2)
    {
        return _int1 + _int2;
    }
    
  4. Press F7 to compile and press CTRL+S to save your changes.

  1. Open the AOT, expand the Services node, and navigate to the CustCustomerService service.

  2. Expand the service, right-click Operations, and then select Add Operation.

  3. In the Add service operations form, select the addIntegers method in the grid and select Add.

  4. Click OK.

  5. Press CTRL+S to save your changes.

After you have added the method and exposed it as a service operation, you must refresh the service. This can be one in the AOT or in the Services form. Refreshing the service in the AOT is faster because the Services form refreshes service operations for all the services.

Refresh the Service in the AOT

  1. Open the AOT, expand the Services node, and then navigate to the CustCustomerService service.

  2. Right-click the service, select Add-Ins, and then select Register actions.

Refresh the Service in the Services Form

  1. On the Setup pane in the Basic module, expand Application Integration Framework, and then click Services.

  2. Click Refresh. If you click Service Operations, you should see the new CustCustomerService.addIntegers method.

If you are exposing the service as a Web service, you must regenerate the Web services.

  1. On the Setup pane in the Basic module, expand Application Integration Framework, and then click Services.

  2. Click Generate. For more information, see "Configure services" in the Server and Database Administration Help.

Community Additions

ADD
Show: