10-Minute AX 2012 App: Windows Azure (Cloud)
Filed under: #daxmusings #bizapps
Powerfully simple put to the test. If you missed the first article, I showed you how to create a quick WPF app that reads data from AX 2012 queries and displays the results in a grid. In this article, I will continue showing you how easy it is to make use of AX 2012 technology in conjunction with other hot Microsoft technologies. Today, we will create a Windows Azure hosted web service (WCF), to which we’ll have AX 2012 publish product (item) data. Again, this will take 10 minutes of coding or less. Granted, it was a struggle to strip this one down to the basics to keep it a 10-minute project.
Windows Azure is Microsoft’s cloud services platform. We’ll be creating a web role here, hosting a WCF service. The idea I’m presenting here is an easy push of data out of AX to an Azure service. There, one could build a cloud website or web service, for example to serve mobile apps (Microsoft has an SDK to build iOS, Android and Windows Phone 7 apps based on Azure services). We’ll spend most time setting up the Azure WCF service (and a quick webpage to show what’s in the table storage), and finally we’ll create a quick AX 2012 VS project to send the data, and hook that into AX using an event handler! So, we’ll build an Azure WCF service using Table storage (as opposed to BLOB storage or SQL Azure storage). Make sure to evaluate SQL Azure if you want to take this to the next level. So, our ingredients for today will require you to install something you may not have already…
Ingredients:
- Visual Studio 2010
- Windows Azure SDK (download here)
- Access to an AX 2012 AOS
Now, the Azure SDK has some requirements of its own. I have it using my local SQL server (I did not install SQL Express) and my local IIS (I did not install IIS express).
Ok, everything installed and ready? Start the timer! You need to start Visual Studio 2010 as administrator. So right-click Visual Studio in your start menu and select “Run as Administrator”. This is needed to be able to run the Windows Azure storage emulators. We’ll start a new project and select Cloud > Windows Azure Project. I’ve named this project “DAXMusings.Azure”. Once you click OK, Visual Studio will ask you for the azure role and type of project. I’ve select “WCF Service Web Role”. Click the little button to add the role to your solution. Also, click the edit icon (which won’t appear until your mouse hovers over it) to change the name to “AXCloud”.
Ok, first things first, setup the data connection for the table storage. Right-click on the “AXCloud” under “Roles” and select properties.
Under settings, click the “Add Setting” button, name it “DataConnectionString”, set the drop down to… “Connection String”, and click the ellipsis. On that dialog screen, just select the “Use the Windows Azure storage emulator” and click OK.
Hit the save button and close the properties screen. So, what we will be storing, and communicating over the webservice, is item data. So, let’s add a new class and call it ItemData. To do this, we right-click on the AXCloud project, and select Add > New Item. On the dialog, select Class and call it “ItemData”.
So, here’s the deal. For WCF to be able to use this, we need to declare this class as a data contract. For Azure table storage to be able to use it, we need three properties: RowKey (string), Timestamp (DateTime) and PartitionKey (string). For item data, we will get an ITEMID from AX, but we’ll store it in the RowKey property since that is the unique key. We’ll just add a Name property as well, to have something more meaningful to look at. To support the data contract attribute, you’ll need to add the namespace “System.Runtime.Serialization” and for the DataServiceKey you need to add the namespace “System.Data.Services.Common”. The PartitionKey we’ll set to a fixed value of “AX” (this property has to do with scalability and where the data lives in the cloud; also, the RowKey+PartitionKey technically make up the primary key of the data…). We’ll also initialize the Timestamp to the current DateTime, and provide a constructor accepting the row and partition key.
Click here for screenshot or copy the code below:
namespace AXCloud
{
[DataContract]
[DataServiceKey(new[] { "PartitionKey", "RowKey" })]
public class ItemData
{
[DataMember]
public string RowKey { get; set; }
[DataMember]
public String Name { get; set; }
[DataMember]
public string PartitionKey { get; set; }
[DataMember]
public DateTime Timestamp { get; set; }
public ItemData()
{
PartitionKey = "AX";
Timestamp = DateTime.Now;
}
public ItemData(string partitionKey, string rowKey)
{
PartitionKey = partitionKey;
RowKey = rowKey;
}
}
}
Next, we’ll need a TableServiceContext class. We’ll need to add a reference to “System.Data.Services.Client” in our project. Just right-click on the “References” node of the AXCloud project and select “Add Reference”. On the .NET tab, find the “System.Data.Services.Client” assembly and click OK.
For easy coding, let’s add another using statement at the top as well, for the Azure storage assemblies.
And we’ll add the Context class. This will provide a property that’s Queryable, and an easy way to add an item through an AddItem() method.
Click here for screenshot or copy the code below:
public class ItemDataServiceContext : TableServiceContext
{
public ItemDataServiceContext(string baseAddress, StorageCredentials credentials)
: base(baseAddress, credentials)
{
}
public IQueryable Items
{
get
{
return this.CreateQuery("Items");
}
}
public void AddItem(string itemId, string name)
{
ItemData item = new ItemData();
item.RowKey = itemId;
item.Name = name;
this.AddObject("Items", item);
this.SaveChanges();
}
}
</code>
</pre>
So, first time we use this app (or any time you restart your storage emulator), the table needs to be created before we can use it. In a normal scenario this would not be part of your application code, but we'll just add it in here for easy measure. Open the "WebRole.cs" file in your project, which contains a method called "OnStart()". At the top, add the using statements for the Windows Azure assemblies. Inside the onstart() method, we'll add code that creates the table on the storage account if it doesn't exist.
Click here for a full screenshot or copy the below code to add before the return statement:
CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSettingPublisher) =>
{
var connectionString = Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue(configName);
configSettingPublisher(connectionString);
}
);
CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
storageAccount.CreateCloudTableClient().CreateTableIfNotExist("Items");
Next, we'll create a new operation on the WCF service to actually add an item. Since we already have all the ingredients, we just need to add a method to the interface and its implementation. First, in the IService.cs file, we'll add a method where the TODO is located. You probably want to remove all the standard stuff there, but to keep down the time needed and to make it easier for you to see where I'm adding code, I've left it in these screenshots.
Now, into the Service1.svc.cs we'll add the implementation of the method, for which we'll need to add the Windows Azure using statements again:
Click here for a full screenshot or copy the code below:
public void AddItem(ItemData item)
{
CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSettingPublisher) =>
{
var connectionString = Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue(configName);
configSettingPublisher(connectionString);
});
CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
new ItemDataServiceContext(storageAccount.TableEndpoint.ToString(), storageAccount.Credentials).AddItem(item.RowKey, item.Name);
}
So now the last thing to do is create a class the consumes this service, which we can use as a post event handler for the insert() method on the InventTable table in AX... I am skipping through some of the steps here, but basically, we create a new class library project in Visual Studio (open a second instance of Visual Studio, since we'll need to keep the Azure service running!), and "Add it to AOT". For a walkthrough on how to do this, check this blog article. We're calling this class libary "DAXMusings.ItemSubscriber".
Alright, when you have the new project created and added to the AOT, we need to add a service reference to the Azure WCF service. Since we're running all of this locally, you'll need to make sure the Azure service is started (F5 on the Azure project). This will open internet explorer to your service page (on mine it's http://127.0.0.1:81/Service1.svc). If Azure opens your web site but not the .svc file (you probably will get a 403 forbidden error then), just add "Service1.svc" behind the URL it did open. If you renamed the WCF service in the project, you'll have to use whatever name you gave it.
So, we'll add that URL as a service reference to our subscriber project, and I'm giving it namespace "AzureItems":
Now that we have the reference, we will make a static method to use as an event handler in AX. First, we need to add references to XppPrePostArgs and InventTable by opening the Application Explorer, navigating to Classes\XppPrePostArgs and Data Dictionary\Tables\InventTable respectively. For each one, right-click on the object and select "Add to project" (for more information on the why and how, check this blog post on managed handlers).
Next, there's one ugly part I will have you add. Since the reference to the service is in a class library, which will then be used in an application (AX), the service reference configuration (bindings.. the endpoint etc) which gets stored in the App.config for your class library, will not be available to the AX executables. Technically, we should copy/paste these values into the Ax32Serv.exe.config (or the client one, depending where your code will run). However, this would require a restart of the AOS server, etc. So (.NET gurus will kill me here, but just remember: this is supposed to be up and running in 10 minutes!) for the sake of moving along, we will just hardcode a quick endpoint into the code...
Click here for full screenshot of the class.
public static void InventTableInsertHandler(XppPrePostArgs args)
{
InventTable inventTable = new InventTable((Microsoft.Dynamics.AX.ManagedInterop.Record)args.getThis());
AzureItems.ItemData itemData = new AzureItems.ItemData();
itemData.RowKey = inventTable.ItemId;
itemData.Name = inventTable.NameAlias;
System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding();
binding.Name = "BasicHttpBinding_IService1";
System.ServiceModel.EndpointAddress address = new System.ServiceModel.EndpointAddress("http://127.0.0.1:83/Service1.svc");
AzureItems.Service1Client client = new AzureItems.Service1Client(binding, address);
client.AddItem(itemData);
}
Notice how the URL is hardcoded in here (this is stuff that should go into that config file). One thing I've noticed, with the Azure project running emulated on my local machine, that it seems to change ports every now and then (I started and stopped it a few times). So you may need to tweak this while you're testing this stuff out.
So last thing to do is add this as a Post-Handler on the InventTable insert() method... (for more information and walkthrough on adding managed post-handlers on X++ objects, check this blog post)
Time to add an item and test! If you wish to debug this to see what's going on, remember the insert method will be running on the AX server tier so you need to attach the debugger to the AOS service.
How do you now know your item was actually added in the Azure table storage? Well, I will post some extra code tomorrow (a quick and dirty ASP page that lists all items in the table).
I am expecting you will run into quite a few issues trying to get through this exercise. I know I have, some of them were minor things but took me hours to figure out what I was missing. Feel free to contact me or leave a comment, and I'll try to look into each issue.
There is no comment section here, but I would love to hear your thoughts! Get in touch!
Blog Links
Blog Post Collections
- The LLM Blogs
- Dynamics 365 (AX7) Dev Resources
- Dynamics AX 2012 Dev Resources
- Dynamics AX 2012 ALM/TFS
Recent Posts
-
GPT4-o1 Test Results
Read more... -
Small Language Models
Read more... -
Orchestration and Function Calling
Read more... -
From Text Prediction to Action
Read more... -
The Killer App
Read more...