Microsoft Dynamics AX 2012 Services ready!

Hi to all of you!

It has been a while since I have posted some useful content here. Mainly to the fact that I was focussing on getting the content in the book.

And now we can finally say that it is finished, reviewed and approved! So as we speak the presses are rolling to get it printed. All of you who have pre-ordered a copy, thanks for your patience, it will be rewarded 🙂

Book cover

Book cover

AIF: Service actions lost after recreating service

When you delete service objects (classes, macros, service node…) and recreate the service you may have run into the issue that none of the service operations were visible.
Here’s how I solved it today when having the same issue. (We had to remove everything or else the problem was not solved)  

The problem is that the actions are not gone, but they were linked incorrectly. The AIFAction table contains the operations and also contains the classId of the class where the methods to handle the actions are.  

  

Well as the service objects were deleted and afterwards recreated, the class ID of the service class had been altered. Due to this changed ID, none of the operations were visible on the service.  

To fix this, just delete the current records in the AIFAction table for the corresponding service and then we can recreate the actions by calling the AIFServiceGenerationManager class,
method RegisterService.  

AIFServiceGenerationManager::registerService("NVMPPointBalanceService");

Then we can see the operations again in the services form.  

AIF : Enable net.tcp binding on WCF service

Today I wanted to check out what the impact of changing the binding method to net.tcp instead of wsHttpBinding was. But to be able to use net.tcp as a binding, some things need to be done.

IIS Setup

The first thing to do is making sure everything is in place on the IIS 7 side.
For tcp.net being enabled as binding method, make sure the following feature is installed.

By installing this, you can add net.tcp bindings in IIS. So right now we can start by going to the IIS manager and add net.tcp as a binding method on the Dynamics Ax AIF Application.

After adding the site binding, enable the net.tcp binding as protocol on the Application

Dynamics Ax AIF Webservice setup

Once the IIS is ready to pass net.tcp requests, we can configure the Dynamics Ax Web Service to work with net.tcp binding.

Inside Dynamics Ax, go to Basic à Setup à Application Integration Framework à Services and click the configure button. Now the configuration editor will start. Here you can select the service you want to alter and change the binding method to net.tcp. You also have to specify the address here. In this case it is: net.tcp://hostname:808/DynamicsAX50AIFServices/nvmpconfigtableservice.svc

Once you have done this, you can go ahead and regenerate the service artifacts and refresh the services on the calling applications.

Dynamics AIF : Query Update Stacktrace

Today, when updating some records through AIF I got the following error :

AIF Stacktrace

This is definitely not a clear error 🙂 since the only thing was wrong is that the Update property was set to No instead of Yes on the Query’s datasource.

Update Property on the query's datasource

Thanks to a collegue of mine (from artofcreation) to point this one out !

Dynamics AIF : Null vs Empty string

Today I experienced that when using AIF through web services, you have to pay attention to the difference between an empty string and a null value.

We created a service for inventory locations and we had the InventLocationId filled in by the AIF. But when calling the service we received an error everytime telling us that the InventLocationId was mandatory but missing. (Although it was in the exemption list and there was code to fill it in)

Then we discovered that the calling .NET application specified the “” value on the inventLocationId instead of the NULL value. This results in a different XML file and the AIF thinks that the InventLocationId is filled in but with an empty value.

XML file when using null value :

AIF NULL Value

XML file when using empty string value :

AIF Empty String Value

Dynamics AIF : Working Times service

Today I created an AIF web service to expose the Working times functionality in Ax 2009. The service itself is quite simple but there was the issue with the workTimeId. The application on the other side doesn’t care about the Id of the working times and wants Dynamics to use a number sequence for the workTimeId field. This post will not deal with the creation of a number sequence reference but shows how you can fill in the WorkTimeId automatically and especially how you can link the WorkTimeLine to the WorkTimeTable via the WorkTimeId.

First things first, we create a query which contain the two tables.

AxdWorkTime Query Object

Since we are planning on updating / deleting through the service, we must not forget to set the properties on the datasources. Properties Delete and Update must be set to Yes.

Then we create the AIF service by using the Wizard available in AX 2009. Afterwards we have the following objects available :

  • AIF Service object
  • AIF Service class
  • AxWorkTimeTable entity class
  • AxWorkTimeLine entity class
  • AxdWorkTime document classWorkTimeDocument / WorkTimeDocument_WorkTimeTable / WorkTimeDocument_WorkTimeLine Classes
  • Some datacontainertype macros stuff

Created project

Now we are going to make sure the WorkTimeId is fetched from a number sequence and inserted automatically. For this we need to do 2 things :

  • Set the WorkTimeId as not mandatory (Both on the axWorkTimeTable and axWorkTimeLine entities)
  • Modify the setWorkTimeId method on the axWorkTimeTable entity to use the number sequence
protected void initMandatoryFieldsExemptionList()
{
    super();
    this.setParmMethodAsNotMandatory(methodstr(AxWorkTimeTable,parmWorkTimeId));
}
protected void setWorkTimeId()
{
    NumberSequenceReference numberSequenceReference;
    ;

    if (this.isMethodExecuted(funcName(), fieldNum(WorkTimeTable, WorkTimeId)))
    {
        return;
    }

    if (this.isSetMethodsCalledFromSave())
    {
        if (this.isFieldSetExternally(fieldnum(WorkTimeTable, WorkTimeId)))
        {
            if (!this.workTimeTable())
            {
                numberSequenceReference = NumberSeqReference::findReference(typeId2ExtendedTypeId(typeid(WorkTimeId)));
                this.checkNumber(numberSequenceReference.numberSequenceTable(),fieldNum(WorkTimeTable, WorkTimeId),this.parmWorkTimeId());
                if (numberSequenceReference.NumberSequence && numberSequenceReference.numberSequenceTable().Continuous)
                {
                    NumberSeq::newReserveNum(numberSequenceReference).reserve(this.parmWorkTimeId());
                }
            }
        }
        else
        {
            if (this.isFieldSet(fieldnum(WorkTimeTable, WorkTimeId)))
            {
                return;
            }

            if (!this.parmWorkTimeId())
            {
                this.parmWorkTimeId(NumberSeq::newGetNum(NumberSeqReference::findReference(typeId2ExtendedTypeId(typeid(WorkTimeId)))).num());
            }
        }
    }
}

And last but not least we need to link the AxWorkTimeLine to the AxWorkTimeTable via the WorkTimeId. The wizard already did some action to achieve this by putting the prepareForSave method on the AxdWorkTime document class.

public boolean prepareForSave(AxdStack _axdStack,  str _dataSourceName)
{
    AxWorkTimeTable   AxWorkTimeTable;
    AxWorkTimeLine    AxWorkTimeLine;
    ;
    switch (classidget(_axdStack.top()))
    {
        case classnum(AxWorkTimeTable) :
            AxWorkTimeTable = _axdStack.top();
            return true;

        case classnum(AxWorkTimeLine) :
            AxWorkTimeLine  = _axdStack.top();
            AxWorkTimeTable = AxWorkTimeLine.parentAxBC();
            AxWorkTimeLine.parmWorkTimeId(AxWorkTimeTable.parmWorkTimeId());
            return true;

        default :
            error(strfmt("@SYS88979",classId2Name(classidget(_axdStack.top()))));
            return false;
    }
    return false;
}

Since the parm method will call the setWorkTimeId method once, we still need to place some code there :
The code will call the parent entity’s parmWorkTimeId method.

protected void setWorkTimeId()
{
    if (this.isMethodExecuted(funcName(), fieldNum(WorkTimeLine, WorkTimeId)))
    {
        return;
    }

    if (!this.parmWorkTimeId())
    {
        this.parmWorkTimeId(this.axWorkTimeTable().parmWorkTimeId());
    }
}

The only thing still needed now is pointing out who is the parent entity. Therefore I’ve created a method that creates an axWorkTimeTable entity.

public AxWorkTimeTable axWorkTimeTable(AxWorkTimeTable _axWorkTimeTable = null)
{
    AxWorkTimeTable axWorkTimeTable;

    if (!prmisdefault(_axWorkTimeTable))
    {
        axWorkTimeTable = _axWorkTimeTable;
    }
    else
    {
        this.setWorkTimeId();
        axWorkTimeTable = WorkTimeTable::find(this.parmWorkTimeId()).axWorkTimeTable();
    }

    return axWorkTimeTable;
}
public AxWorkTimeTable axWorkTimeTable()
{
    AxWorkTimeTable axWorkTimeTable = AxWorkTimeTable::newWorkTimeTable(this);
    return AxWorkTimeTable;
}

If someone wants to add something, be my guest… comments are welcome !

Dynamics AIF : Tracing Web Service calls

When using AIF through web services, you can be in the dark when you want to know what’s really going on in there. Additionally you can have trouble debugging the thing when you are running a Windows 2008 box. (Contacted Microsoft last week and the response was that debuggin AIF webservices does not work on a Win2k8 box but they are developing a hotfix as we speak)

There is a possibility to have some tracing of what is happening there.  I found this on MSDN and will show an example here of what the result of the tracing shows. 

First, head over to the folder where the Dynamics Ax web services are generated. There you should find a web.config file. To enable tracing you need to make sure this tag is present 

 <system.diagnostics>
    <!-- This is used for enabling tracing in retail and debug builds.
           AIF Service processing stack outputs and information
           messages used for debugging.
           Possible values for the switch are Off, Warning,
             Information, Critical, Verbose.
           Change the switchValue to Information to enable tracing.
      -->
    <sources >
      <source name="AifServiceProcessing" switchValue="Information">
        <listeners >
          <add name="TextWriterListener"
               type="System.Diagnostics.TextWriterTraceListener"
               initializeData="AifServiceTrace.webinfo" >
          </add>
          <remove name="Default" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>  

After invoking the service, there should be a file named AifServiceTrace.Webinfo present with the tracing information : 

The following information can be viewed :

  • UserId’s
  • Message ID
  • Source and destination endpoint
  • Key Data XML
  • Return Data from Ax

 

AIF WCF Tracing

Dynamics AIF : Invalid child element in namespace

A couple of days ago, I encountered this problem while calling the InventInventLocation AIF web service’s create operation.

System.ServiceModel.FaultException : Invalid document schema. The following error is reported : The element ‘InventLocation’ in namespace ‘http://schemas.microsoft.com/dynamics/2008/01/documents/InventLocation’ has invalid child element ‘InventLocationIdQuarantine’ in namespace ‘http://schemas.microsoft.com/dynamics/2008/01/documents/InventLocation’. List of possible elements expected: ‘InventLocationId’ in namespace ‘http://schemas.microsoft.com/dynamics/2008/01/documents/InventLocation’. 

The error itself may actually be confusing because in this case, the InventLocationIdQuarantine field is not actually the cause of the problem. In most cases this error will appear when the order of fields is different from the one expected by the document schema. So either the field ordering is different of there are some fields missing. In this case, I was using AIF webservices (WCF services) and you can view the actual XML that was communicated to the AIF by going to the document history.

Basic – Periodic – Application Integration Framework –  Document History 

AIF Document History

 There you can view the XML by clicking the XML button.   

XML InventLocation Create Operation

Here we can see that the inventlocation field is not present in the XML.
Do note that in this Ax application we have create custom code to initiate the InventLocationId field from a numbersequence when the record is created.   

To solve the error here, we need to tell the AIF engine that the InventLocationId field is not mandatory in our XML document and make sure the value gets defaulted.
This will be done here by adding defaulting logic in the Entity class but this could also be done by putting the defaulting “behavior” on the table using a strategy pattern. 

Setting the field as not mandatory :   

protected void initMandatoryFieldsExemptionList()
{
    super();
    this.setParmMethodAsNotMandatory(methodstr(AxInventLocation,parmInventLocationId));
}

Adding the defaulting logic

protected void setInventLocationId()
{
    NumberSequenceReference numberSequenceReference;
    ;

    if (this.isMethodExecuted(funcname(), fieldnum(InventLocation, InventLocationId)))
    {
        return;
    }

    if (this.isSetMethodsCalledFromSave())
    {
        if (this.isFieldSetExternally(fieldnum(InventLocation, InventLocationId)))
        {
            if (!this.inventLocation())
            {
                numberSequenceReference = InventParameters::numRefInventLocationId();
                this.checkNumber(numberSequenceReference.numberSequenceTable(),fieldnum(InventLocation, InventLocationId),this.parmInventLocationId());
                if (numberSequenceReference.NumberSequence && numberSequenceReference.numberSequenceTable().Continuous)
                {
                    NumberSeq::newReserveNum(numberSequenceReference).reserve(this.parmInventLocationId());
                }
            }
        }
        else
        {
            if (this.isFieldSet(fieldnum(InventLocation, InventLocationId)))
            {
                return;
            }

            if (!this.parmInventLocationId())
            {
                this.parmInventLocationId(NumberSeq::newGetNum(InventParameters::numRefInventLocationId()).num());
            }
        }
    }
}

Now we do not need to specify the inventLocationId and the field will be defaulted to a new number from the number sequence reference.

Dynamics Ax AIF Web Service reference credential problem

 

At a customer’s site, I was creating some extra AIF wcf services so that some extra tables would be externally available. For creating the WCF service, I was using the service wizard in Ax 2009 which is described here : http://msdn.microsoft.com/en-us/library/aa609947.aspx

The newly created service was deployed to the Internet Information Server but when the .Net application tried to reference the webservice, the following happened : The service was prompting for credentials and did not work properly.

WCF Service Reference Credential Prompt

After a certain amount of searching time and nearly giving up,  I was browsing the configuration file of the web services directory and discovered that the BindingMethod of the deployed services was configured to use basicHttpBinding.

Endpoint binding method basicHttpBinding

This was the actual problem because the wsdl looked like this : (negotiate authentication tags were added, …)

 

Wrong WSDL with negotiateAuthentication tag

Changing the binding method to wsHttpBinding creates the service without the negotiateAuthentication and takes care of our annoying credential problem.

Endpoint binding method wsHttpBinding