Hi
There!
As
promised, in this post we’ll see how to create a simple demo service using AX
2012. Please refer to my last post about AX 2012 Service Documents Types and A Simple Introduction to AX 2012 Services Architecture to get a more
detailed explanation to AX 2012 Services.
Also,
I would like to point out that AX 2012 services are hosted on the AOS, and that
both internal and external applications will be authenticated through the AOS. With this being said, the following example
will be an internal application that will get the customer name from AX
services.
If I wanted to have an external application executing the same operation
I would have to host the services in IIS. For more information on services
please refer to this white paper.
Further,
when working with AX 2012 WCF services, we will
have to dive into data contracts. In this sample service will use a DataContractAttribute. The string DataContractAttribute indicates that this class is a Data Contract. The square brackets [] indicate that this string is an attribute. We are also going
to be using Data Members, which would
help us define the default values for our data
contracts.
If
you are new to this, don’t worry, you can find a lot of information about Data Contracts and Data Members out there.
Another
concept that we’ll be exposed on this example is the SysEntryPointAttribute, which helps with
authorization checks and how they are performed for a method that is called on
the server. Further, this is a very important class when working with Services
in AX 2012 as we have to use it for
all service operations.
So,
let’s start working on our simple service. (Please note that I’m working on a
project that involves a lot more detail. When done I will share it with you, so
we can understand many other concepts about AX 2012 that won’t be covered in
this post)
1. Open the AOT.
2. Create a new
class.
3. Enhance the class
by adding the [DataContractAttribute]
attribute.
4. Create a new
method.
5. Enhance the class
by adding the [DataMemberAttribute] attribute.
6. Create another
class.
7. Create a method
that instantiates the class created in Step 2.
8. Enhance the class
that uses the [SysEntryPointAttribute]
attribute.
9. Right-click the
Services node in the AOT and select New Service.
10. Enter a name into
the Name property.
11. Set the Class
property to the class created in Step 6.
12. Expand the new
service node.
13. Right-click the
Operations node and select Add Operations.
14. Right-click the
Services Group node in the AOT and select New Service Group.
15. Enter a name into
the Name property.
16. Drag-and-drop the
service created in step 9 under the Service Group node.
17. Right-click the
service group and select Deploy service group.
Ok, so at this point we should have our new AX 2012 service already
working. The next step is to learn how to use this service from Visual Studio
2010. Please refer to this article if
you are new to adding services references to Visual Studio. It will help you
understand the underlying concepts of this operation.
1. Select File > New > Project to create a new project of type Console Application.
2. Enter a name into
the Name field and then click OK.
3. Right-click the
Service reference node under the project.
4. Select Add Service
Reference.
5. Enter the address
and then click GO.
6. Select the service
and then click OK.
NOTE: The address format in step 5 is http://<machinename>:8101/DynamicsAx/Services/Xpp.
7. Invoke the service
from the client.
8. Click F5 to run
the service.
As
you can see, when you run the service, the first customer name that are available
in your system will be presented as an output. There is a lot more to services and you can
start learning more about it by going to partner source and/or downloading the
new AX 2012 white papers focusing on services.
I
will be posting a lot more on this later soon, so keep reading my blog as there
is a lot more to come.
Take
Care and have a great weekend!