Friday, 2 August 2013

WCF worker session inactivity timeout

One of the new features of Dynamics AX2012 was introduction of WCF services. Our organisation's current website uses BC.Net to integrate with AX. We are in the process of porting it to WCF.

When the customers are placing orders on our website one of the step is to show the product they regularly buy combined with the most popular products. To provide a fast experience to the customers we decided to use multiple simultaneous calls to AX to get the product details along with the stock check already done on these products. As these customers are external to our organisation hence we create these customers as Claim based users in AX (another new feature in AX2012) and apply xds security policy to filter out only the data that a particular customer is allowed to see.

This worked very well until we noticed that these simultaneous calls, for a customer, results in creation of multiple worker sessions in AX. AOS does reuses these sessions if they are free. If the session is busy serving a request AOS creates another session. In our case the duration of each request for an item, with its details, is around 200 milliseconds, so a page with 100 items resulted in 10-15 worker sessions. What we observed was that these sessions lasted for 30 minutes despite the request being completed in milliseconds.

This was a potential problem for us. With a customer base of more than 70 thousand customers, the AOSs could potentially be flooded with sessions that are not being used and could hit the upper limit of maximum concurrent sessions on an AOS very quickly (by default set at 2000, but can be changed). Once that limit is hit AOS refuses any more connection request and throws an error.

We tried to find whether this 30 minute inactivity time out can be changed to a value which is more convenient for us but failed to find any such setting. So we contacted Microsoft and they recommended the following change. To configure the WCF worker session inactivity time out on an AOS add the line

 <add key="SessionPoolFlushSeconds" value="180" />

to the <appSettings> section of file Ax32Serv.exe.config located at

C:\Program Files\Microsoft Dynamics AX\60\Server\<AOS NAME>\bin

A restart of AOS is required for the change to take effect. After the change the file should look similar to


As can be seen from the file we set it to clean up after 3 minutes. The clean up happens 3 minutes after the time the worker session is last used. So if a worker session is first created at 12:00 and then reused at 12:02, it will be cleaned after 12:05.

Note: Please be very cautious in making this change. Proper testing should be done before deploying the change to the production AOS(s). Setting a very low value could result in poor performance as AOS will have to recreate the session which is a time consuming task.

This posting is provided "AS IS" with no warranties. Use code at your own risk.

Saturday, 1 June 2013

Kerberos authentication / Double hop issue

I'm sure there was or will be a time when one might have to configure Kerberos authentication between SQL servers to overcome the double hop issue. We had the pleasure of doing so last week. Let me give a brief description of our environment.

Client: A normal windows 7 machine with SQL server management studio.
SQL Server (Hop 1): A named instance of SQL server 2012.
Linked SQL Server (Hop 2): A named instance of SQL server 2008 R2.

We use only windows authentication on the SQL servers and being in an enterprise environment SQL servers run under an active directory service account. As we always use the named instances of SQL server so the servers always use a dynamics port. Now to configure Kerberos authentication all you have to do is perform the following four steps.

1. Allow the service accounts / SQL server computers to set SPN in the active directory.
2. Allow the server computers to be trusted for delegation in active directory.
3. Create the linked server.
4. Restart the SQL servers so that they can set the SPNs(The SPNs can be set manually as well).

The best resource that we could find for setting up Kerberos authentication is from this post on MSDN blog. Including the previously mentioned post, we followed almost every blog/white paper out there on the internet and after performing all the steps, none of it worked. We even used the newly released tool "Microsoft Kerberos Configuration Manager for SQL Server", which showed that everything is fine but still it was not working.

We could not figure out why it was not working until one of my colleague suggested to do the same setup between two SQL servers running same version. So we replaced the middle server (2012) with the one running SQL server 2008 R2 and it worked. so obviously we concluded that there is something wrong with our SQL server running 2012. But still we could not find what's wrong.

It was just by chance that my colleague suggested to change the port under which this SQL was running and  it worked. Later investigation found that there was a duplicate entry in active directory for the SPN which was the cause of the issue. So a tip from us, if its not working consider changing the port of sql server.


This posting is provided "AS IS" with no warranties. Use code at your own risk.

Saturday, 11 May 2013

Security issue for tables with inheritance

One of my colleague is working on a project where they require to integrate using WCF service. He came across an issue where they were not getting the Product name. The issue was that on a restart of the AOS their WCF call would not return the product name.  However after someone accesses the product from within AX, the WCF would return the name.

They asked for my assistance in getting the issue resolved. The symptoms that they described were hinting that it is a security related issue. The fact that product name was returned once someone accesses the product from within AX clearly showed that the AOS is caching the item name. So after cache was filled in the WCF call will get the name from the cache instead of getting it directly from the table. Usually AX will throw an error if the user did not have access to the table. But in this case there was no error.

They were using InventTable.itemName() method to return the product name. We looked at the security role/duties/privileges developed by them and all looked fine. We then decided to do things one step at a time to find out the exact place where things stopped working. We looked at the call stack of method itemName and started replacing the method with the code inside the method.

Eventually we found the code that was causing the issue. The code is in InventTable.productName.

   return EcoResProduct::find(productRecId).productName(_languageId);  

When we replaced this call with the code inside the EcoResProduct.productName it started working. The developer did gave access of EcoResProduct to the role/duty. So it was really confusing why this was not working. Then I realised that this is a SuperType/SubType table and EcoResProduct is not the only table that requires access. Type hierarchy browser indicated that we need access to two more tables. Once the access was granted product name was accessible even if was not cached.

The question to ask is "Why AX does not throw error in tables involving hierarchy when the user does not have access".

This posting is provided "AS IS" with no warranties. Use code at your own risk.

Friday, 10 May 2013

Financial dimension sets balance update issue

Recently we noticed that the one of our Financial dimension sets balance is not being updated. When we opened the form and looked at the record, "Rebuild balances" was greyed out as shown below


Running the "Update balances" resulted in the following error.


What this error really means is that the rebuild/update process is already running. Apparently the rebuild balance process never finished on its previous run (maybe due to process being killed by someone/the AOS crash or electricity failure etc.). We could not determine why this happened. Folks at Microsoft helped us in  resetting the status and we were able to rebuild the balances. Following job was used.

 static void ResetFinDimState(Args _args)  
 {   
   DimensionHierarchy dimensionHierarchy;  
     
   update_recordSet dimensionHierarchy  
     setting FocusState = DimensionFocusBalanceInitializationState::Initialized  
     where dimensionHierarchy.FocusState == DimensionFocusBalanceInitializationState::InProcess &&  
        dimensionHierarchy.StructureType == DimensionHierarchyType::Focus &&  
        dimensionHierarchy.DeletedVersion == 0;  
 }  

This posting is provided "AS IS" with no warranties. Use code at your own risk.

Sunday, 5 May 2013

SQL script to rebuild all indexes for a table

Ideally I would recommend to use Ola Hallengren's SQL Maintenance Solution but in some cases you may have to rebuild indexes on individual tables manually. Following script will allow you to do that. Just replace <TableName> with the actual table name and if you want to build the index offline (might cause locks) set @Build_Online to 0.

 DECLARE @IndexName VARCHAR (100);  
 DECLARE @TableName VARCHAR (100);  
 DECLARE @IndexId int;  
 DECLARE @Build_Online int;  
   
 SET @TableName = '<TableName>';  
 SET @Build_Online = 1;  
   
 DECLARE indexes_cursor CURSOR FOR SELECT  
      si.index_id,  
      si.name  
 FROM sys.objects so  
 INNER JOIN sys.indexes si  
      ON so.object_id = si.object_id  
 WHERE so.name = @TableName;  
   
 OPEN indexes_cursor;  
   
 FETCH NEXT FROM indexes_cursor  
 INTO @IndexId, @IndexName;  
   
 WHILE @@FETCH_STATUS = 0  
 BEGIN  
   
      PRINT 'Defragmenting ' + @TableName + ' --> ' + @IndexName  
   
      IF @Build_Online = 1  
      BEGIN  
           EXEC ('ALTER INDEX ' + @IndexName + ' ON ' + @TableName + ' REBUILD WITH (ONLINE = ON)')  
      END  
      ELSE  
      BEGIN  
           EXEC ('ALTER INDEX ' + @IndexName + ' ON ' + @TableName + ' REBUILD')  
      END  
        
      SELECT  
           *  
      FROM sys.dm_db_index_physical_stats  
      (  
      DB_ID('AxaptaLive'),  
      OBJECT_ID(@TableName),  
      @IndexId,  
      NULL,  
      NULL  
      );  
   
      FETCH NEXT FROM indexes_cursor  
      INTO @IndexId, @IndexName;  
   
 END  
   
 CLOSE indexes_cursor;  
 DEALLOCATE indexes_cursor;  
 GO  

This posting is provided "AS IS" with no warranties. Use code at your own risk.