Creating custom service based on AifXmlSerializable class

AifXmlSerializable class

class VKOVAifXmlStr implements AifXmlSerializable
{
    str             xmlStr;

    #define.MyDataObjectNS('http://schemas.contoso.com/samples/VKOVAifXmlStr')
    #define.MyDataObjectRoot('VKOVAifXmlStr')
}

/// <summary>
///    Deserializes the xml to initialize the object.
/// </summary>
/// <param name="xml">
///    The xml to deserialize.
/// </param>
void deserialize(AifXml xml)
{
    XmlTextReader xmlReader;
    ;

    xmlReader = XmlTextReader::newXml(xml);

    // turn off Whitespace handling to avoid extra reads
    xmlReader.whitespaceHandling(XmlWhitespaceHandling::None);

    xmlReader.moveToContent();
    while ((xmlReader.nodeType() != XmlNodeType::Element) && !xmlReader.eof())
    {
        xmlReader.read();
    }

    xmlReader.readStartElement3(#MyDataObjectRoot, #MyDataObjectNS);
    if (!xmlReader.eof() && xmlReader.isStartElement())
    {
        xmlStr = xmlReader.readElementString3('xml', #MyDataObjectNS);
    }

    xmlReader.readEndElement();
    xmlReader.close();
}

public void fillXML(XmlTextWriter xmlTextWriter)
{
    xmlTextWriter.writeElementString('xml', xmlStr);
}

/// <summary>
///    Returns the name of the root element of the xml document.
/// </summary>
/// <returns>
///    The name of the root element of the xml document.
/// </returns>
public AifDocumentName getRootName()
{
    return #MyDataObjectRoot;
}

/// <summary>
///    Gets the schema of the xml document.
/// </summary>
/// <returns>
///    The xml schema that describes the document.
/// </returns>
AifXml getSchema()
{
    str schema =
    @'<?xml version="1.0"?>
    <xsd:schema xmlns="http://schemas.contoso.com/samples/VKOVAifXmlStr"
    targetNamespace="http://schemas.contoso.com/samples/VKOVAifXmlStr"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">
    <xsd:complexType name="VKOVAifXmlStrType">
    <xsd:sequence>
    <xsd:element name="xml" type="xsd:string" />
    </xsd:sequence>
    </xsd:complexType>
    <xsd:element name="VKOVAifXmlStr" type="VKOVAifXmlStrType"/>
    </xsd:schema>'
    ;

    return schema;
}

public void new()
{
    xmlStr = @'<?xml version="1.0" encoding="UTF-8"?>
<Message>
    <Activity>
        <Id>ConfigurableProducts</Id>
        <Language>EN-US</Language>
    </Activity>
    <Data>
    </Data>
</Message>';
}

public str parmXml(str _xml = xmlStr)
{
    xmlStr = _xml;

    return xmlStr;
}

public str parmXmlAdd(str _xml = '')
{
    if (_xml)
    {
        xmlStr = _xml + xmlStr;
    }

    return xmlStr;
}

/// <summary>
///    Serializes an object into xml.
/// </summary>
/// <returns>
///    The xml that represents this object instance.
/// </returns>
AifXml serialize()
{
    str xml;
    XmlTextWriter xmlTextWriter;
    ;

    #Aif

    xmlTextWriter = XmlTextWriter::newXml();

    // turn off indentation to reduce file size
    xmlTextWriter.formatting(XmlFormatting::None);

    // initialize XML document
    xmlTextWriter.writeStartDocument();

    // write root element
    xmlTextWriter.writeStartElement2(#MyDataObjectRoot, #MyDataObjectNS);

    // write custom data
    this.fillXML(xmlTextWriter);

    // serialize XML document into XML string
    xmlTextWriter.writeEndDocument();
    xml = xmlTextWriter.writeToString();
    xmlTextWriter.close();

    return xml;
}

Service class

RunOn property of class should be set to Server

class VKOVAifXmlStrService
{
}

[SysEntryPointAttribute(true)]
public VKOVAifXmlStr makeRequest(str _xmlStr)
{
    VKOVAifXmlStr      xmlTest = new VKOVAifXmlStr();

    xmlTest.parmXmlAdd(_xmlStr);

    return xmlTest;
}

Service

Create service VKOVAifXmlStrService with following properties:
1. Namespace: http://schemas.contoso.com/ServiceContracts
2. Class: VKOVAifXmlStrService
Expand Operations node and Add operations, which is makeRequest.

Service Group

1. Create new one with name VKOVCustomServicesXml.

2. Right click and New Service Node Reference

3. Select VKOVAifXmlStrService as Service property and name

4. Deploy Service group

Check service Inbound ports: System administration -> Setup -> Services and Application Integration Framework -> Inbound ports

Test service

1. Run Visual Studio x64 Win64 Command Prompt 2010 / Open VC2012 x64 Native Tools Command Prompt
2. Run wcftestclient in Command Prompt
3. File -> Add service. Paste WSDl URI and click OK
4. Invoke test request
You also can use SoapUI

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

Blog Tags