Simple custom service implementation example d365

Here is a simple calc custom service implementation for Dynamics 365 FO / F&SCM
Main contract:
[DataContractAttribute("Calc")]
class VKOCalcContract
{
    int                 arg1;
    int                 arg2;
    int                 operation;
    VKOCalcSubContract	subCalc;
    List                subList;

    [DataMemberAttribute("arg1")]
    public int parmArg1(int _arg1 = arg1)
    {
        arg1 = _arg1;
        return arg1;
    }

    [DataMemberAttribute("arg2")]
    public int parmArg2(int _arg2 = arg2)
    {
        arg2 = _arg2;
        return arg2;
    }

    [DataMemberAttribute("operation")]
    public int parmOperation(int _operation = operation)
    {
        operation = _operation;
        return operation;
    }

    //This parm is just for demonstration on how you can use nested contracts and lists
    [DataMemberAttribute("list"),
        AifCollectionTypeAttribute("_list", Types::Class, classStr(VKOCalcSubContract)),
        AifCollectionTypeAttribute("return", Types::Class, classStr(VKOCalcSubContract))]
    public List parmSubList(List _subList = subList)
    {
        subList = _subList;
        return subList;
    }

    //This parm is just for demonstration on how you can use nested contracts and lists
    [DataMemberAttribute("subCalc")]
    public VKOCalcSubContract parmSubCalc(VKOCalcSubContract _subCalc = subCalc)
    {
        subCalc = _subCalc;
        return subCalc;
    }
}
Sub contract. It is not used by the service, only to show the possibilities:
[DataContractAttribute("SubCalc")]
class VKOCalcSubContract
{
    int a;

    [DataMemberAttribute("a")]
    public int parmA(int _a = a)
    {
        a = _a;
        return a;
    }
}
Response contract:
[DataContractAttribute]
class VKOCalcResponseContract
{
    real result;

    [DataMemberAttribute("result")]
    public real parmResult(real _result = result)
    {
        result = _result;
        return result;
    }
}
Service:
class VKOCalcService
{
    public VKOCalcResponseContract calc(VKOCalcContract _calcContract)
    {
        VKOCalcResponseContract response    = new VKOCalcResponseContract();
        real                    result;
        int                     a1          = _calcContract.parmArg1(),
                                a2          = _calcContract.parmArg2();

        switch (_calcContract.parmOperation())
        {
            case 0:
                result = a1 + a2;
                break;
            case 1:
                result = a1 - a2;
                break;
            case 2:
                result = a1 * a2;
                break;
            case 3:
                result = a1 / a2;
                break;
        }

        response.parmResult(result);
        
        return response;
    }

}
Additionally, you will need to
- create service (VKOCalcService) pointing to the service class with service operation pointing to the calc method of service class.
- create service group (VKOServiceGroup) holding created service

How to test custom services using RAW http.

Example of JSON request:
{
"_calcContract":
    {
         "arg1": 2,
         "arg2": 2,
         "operation": 0,
         "list": [{
             "a": 0,
         },
         {
             "a": 1,
         }],
         "subCalc": {
             "a": 5
         }
    }
}
Example of answer:
{"$id":"1","result":4.0}

SOAP:

WSDL URL: https://Host/soap/services/VKOServiceGroup?wsdl
Requests should be sent to the service group URL (POST): https://Host/soap/services/VKOServiceGroup
GET request will give information about the service group.

Example of request:
Headers:
Content-Type: text/xml
Authorization: Bearer AccessToken
SOAPAction: http://tempuri.org/VKOCalcService/Calc
XML:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:dat="http://schemas.microsoft.com/dynamics/2013/01/datacontracts" xmlns:tem="http://tempuri.org" xmlns:dyn="http://schemas.datacontract.org/2004/07/Dynamics.AX.Application">
   <soapenv:Header>
   </soapenv:Header>
   <soapenv:Body>
      <tem:Calc>
         <tem:_calcContract>
            <dyn:List>
               <dyn:SubCalc>
                  <dyn:a>1</dyn:a>
               </dyn:SubCalc>
            </dyn:List>
            <dyn:arg1>2</dyn:arg1>
            <dyn:arg2>2</dyn:arg2>
            <dyn:operation>0</dyn:operation>
            <dyn:subCalc>
               <dyn:a>1</dyn:a>
            </dyn:subCalc>
         </tem:_calcContract>
      </tem:Calc>
   </soapenv:Body>
</soapenv:Envelope>
Response:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
        <Infolog xmlns="http://schemas.microsoft.com/dynamics/2013/01/datacontracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <Entries/>
        </Infolog>
    </s:Header>
    <s:Body>
        <CalcResponse xmlns="http://tempuri.org">
            <result xmlns:a="http://schemas.datacontract.org/2004/07/Dynamics.AX.Application" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <a:result>4</a:result>
            </result>
        </CalcResponse>
    </s:Body>
</s:Envelope>




 

Search

About

DaxOnline.org is free platform that allows you to quickly store and reuse snippets, notes, articles related to Dynamics AX.

Authors are allowed to set their own AdSense units and "buy me a coffee" link.
Join us.

Blog Tags