Showing posts with label WebService. Show all posts
Showing posts with label WebService. Show all posts

Wednesday, 26 October 2016

How to create AIF AX custom service

In this post I will demonstrate the creation of custom service. Custom service is often designed or created for performing the business logic triggered from third party applications. In this post I will demonstrate that how a sales order confirmation can be triggered.

There are multiple scenarios where we need custom services to be implemented and this is one of the simple scenarios .

Basics


1. We need a contract class, contract class is specifically needed when the service deals with multiple parameters or if the parameters passed by third party application are user defined data types or collection types.

2. We need a service class to perform the business logic.

3. We need to create a Service from service node and then add the required service operation to service. Only the methods which were marked as SysEntryPointAttribute{true}, can be exposed to service.

4.Register the service. Service registration is need in case we need enhanced port. (Enhance port is covered in earlier posts)

5. Create a service group with the above service node and deploy the service group.

6. Write C# code to test the service.

Steps to be followed

Create custom service


1. Create contract class

[DataContractAttribute]
class SalesConfirmationContract
{
    SalesId salesId;
}

[DataMemberAttribute('SalesID')]
public SalesId parmSalesId(SalesId _salesId = salesId)
{
    salesId = _salesId;

    return _salesId;
}

2. Create service class
public class SalesConfirmationService
{
}

[SysEntryPointAttribute(true),
AifCollectionTypeAttribute('contract', Types::Class, classStr(SalesConfirmationContract))
]
public void  confirmSO(SalesConfirmationContract contract)
{
    SalesFormletter SalesFormletter;
    SalesTable      SalesTable;
    SalesId         salesId = contract.parmSalesId();

    try
    {
        ttsBegin;

        SalesFormletter = SalesFormletter::construct(DocumentStatus::Confirmation);
        SalesTable.clear();
        SalesTable = SalesTable::find(salesId);
        SalesFormletter.update(SalesTable,
                               systemDateGet(),
                               SalesUpdate::All,
                               AccountOrder::None,
                               false,
                               false);

        ttsCommit;
    }

    catch (Exception::CLRError)
    {
        throw error(CLRInterop::getLastException().ToString());
    }
}

3. Generate incremental CIL.

4. Create new service from service node and add the class to service created on step 3.


5. Right Click the operation node and add the service operation.
6. Right click on the service and register the service.

7. Create a service group and add the new service to this service group.



8. Right click and deploy the service group.

Test Custom service

1. Copy the WSDL from inbound port.

2. Create new console application in VS.

3. Add service reference.


4. Copy and paste below code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConfirmSO.SalesSR;
namespace ConfirmSO
{
class Program
{
static void Main(string[] args)
{
try
{

SalesConfirmClient client = new SalesConfirmClient();
CallContext context = new CallContext();
context.Company = "EAM";
context.Language = "en-au";
SalesConfirmationContract contract = new SalesConfirmationContract();
contract.SalesID = "SO-000001";
client.confirmSO(context, contract);

Console.WriteLine("Sales order confirmed");
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadLine();
}
}
}
}

5. Build and Run the VS C# code in order to confirm the sales order. 



Happy daxing... :)









        1. Tuesday, 25 October 2016

          How to create and test the DLL of AX2012 service

          This post will demonstrate the creation of  DLL with AX2012 service and than testing the DLL from Visula studio.

          Hi guys this requirement is not very common and I could hardly found any post which share the details about this. I did a POC for a requirement where the third party software was not enough capable to directly consume the AX2012 SVC.

          In order to provide a solution we created DLL out of the AX2012  service and than tested the same DLL using as a reference in visual studio.

          Step: 1 Create DLL for your existing service.
          Goto Visual Studio --> New --> Project --> C# --> ClassLibrary --> Add service reference
            
          Step 2: Set your project to release mode.


          Step 3: Build the project and copy the dll from release folder.
          Release folder path
          Step 4: Go to config in same VS project and copy the endpoint address and save it for later use.
          Now close the VS solution. Your DLL is ready to be used copy this dll to any other location on your system.

          Step 5: To test the same dll we need to create another VS solution.
          Goto --> File --> New --> Projects --VisualC# --> ConsoleApplication.
          Step 6: Add reference to you dll.
          Goto --> Solution Explorer --> Reference --> Right click --> Add new reference
          Step 7: Browse to the dll folder path and select dll file to be added.
          Step 8: Repeat step 6 and add one more service reference from Assembly.

          Step 9: Add below code to your console application
          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using System.Threading.Tasks;
          using StudentTableDll.StudentService;
          using System.Configuration;
          using System.ServiceModel;
          namespace TestDLLDemo
          {
          class Program
          {
          static void Main(string[] args)
          {
          EndpointAddress ep = null;
          System.ServiceModel.NetTcpBinding tcpb = new System.ServiceModel.NetTcpBinding();
          System.ServiceModel.ChannelFactory channelFactory = new System.ServiceModel.ChannelFactory<StudentTableService>(tcpb);

          // End Point Address taken from step 4 in same blog

          string strEPAdr = "net.tcp://ax2012r3dev:8201/DynamicsAx/Services/StudentTable";
          ep = new EndpointAddress(strEPAdr);
          StudentTableServiceClient client = new StudentTableServiceClient(tcpb, ep);
          AxdStudentTable axd = new AxdStudentTable();
          CallContext context = new CallContext();
          context.Company = "EAM";
          context.Language = "en-au";
          AxdEntity_StudentTable studentTable = new AxdEntity_StudentTable();
          studentTable.Name = "Kumar";
          studentTable.Standard = "Eight";
          studentTable.RollNumber = 01;
          axd.StudentTable = new AxdEntity_StudentTable[1] { studentTable };
          try
          {
          EntityKey[] returnFloc = client.create(context, axd);
          EntityKey returnedFloc = (EntityKey)returnFloc.GetValue(0);
          Console.WriteLine("the record has been created RECID- " + returnedFloc.KeyData[0].Value);
          Console.ReadLine();
          }
          catch (Exception e)
          {
          Console.WriteLine(e.ToString());
          Console.ReadLine();
          }
          }
          }
          }

          Step 9: Before running the code please ensure the below highlighted points.


          Step 10: Run to code to create the record in student table.

          Happy daxing :)