Showing posts with label Web service AIF. Show all posts
Showing posts with label Web service AIF. Show all posts

Wednesday, 18 March 2015

Consuming a Web Service in AX 2012 is easy

So there might be a little threshold before you know how to consume your first Web Service, but I encourage any AX developer to set aside at least half an hour to just play with this. There are a lot of good content out there describing how to consume Web Services in AX 2012 (TechNet, White Paper, Mukesh Blog, Joris Blog) so instead of repeating all of those good reads, I wanted to equip you with a dummy service of your own. Then it will be easy to consume it and you will have complete control over both the service providing data and the code in AX consuming the data.

I will assume you have the following installed:

  • Internet Information Server
  • Visual Studio 2010
  • Dynamics AX 2012 Client
  • Dynamics AX 2012 Visual Studio Tools
First, let us create a new Project in Visual Studio. I'm want something easy and swift, so let's choose .Net 3.5 and ASP.Net Web Service Application. I'm naming the Project "PizzaSearchService" and this will automatically be part of the namespace for this application. 


The project loads and you will be presented with the code for "Service1". Now just copy the code underneath and paste it in.
using System.ComponentModel;
using System.Collections.Generic;
using System.Web.Services;

namespace PizzaSearchService
{
    public class PizzaInfo
    {
        public string Name;
        public string Description;
        public double Prize;
    }

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class Service1 : WebService
    {
        [WebMethod]
        public List< PizzaInfo > SearchPizza(string query)
        {
            return new List< PizzaInfo > { 
                new PizzaInfo{ 
                    Name = "AX Beef Extreme", 
                    Description = "One of our favourites with mushroom, beef, onion and mozzarella cheese.", 
                    Prize = 12.99 
                }, 
                new PizzaInfo{ 
                    Name = "AX Regular Meatlover", 
                    Description = "The good old classic with mushroom, meat, pepperoni, peppers and mozzarella cheese.", 
                    Prize = 10.99 } 
            };
        }
    }
}

This is just a very simple service that takes a string as an input for a query for pizzas. It then returns a list of two pizzas. I love pizza, so I just couldn't help myself. 

Now we need to build this and deploy it to Internet Information Server (IIS). Depending on your configuration of IIS you might need to prepare a site that runs on an Application Pool that uses .Net 2 instead of .Net 4. Let us quickly go through these steps. I am assuming you're not messing up the settings for some already existing Web Site. If that is the case, you might need to create a different Web Site where you can host this Web Service.

Open Internet Information Services Manager, right-click the Default Web Site and choose Manage Website and Advanced Settings...


Then click choose to select the correct Application Pool. By default IIS will have preconfigured some Applications Pools, and the one we want for now is the one named "Classic .Net AppPool", because it runs .Net 2, and our Web Service is of .Net 3.5 (built on .Net 2).


Having this set, you can head back to Visual Studio and Publish your built solution. Right-click the project and choose Publish...


Select "File System" as Publish method, and then choose a target Location.


Select Local IIS and your Default Web Site.


Now simply press Publish and your Service1.asmx and precompiled binaries will be copied to the location of your Web Site, normally under C:\inetpub\wwwroot\.


You should be able to test the Web Service by opening a browser and navigating to it. Try loading http://localhost/service1.asmx and see what happens. Unless something went horribly wrong, you should see this page listing service entry points and some extra textual description.


If you click the SearchService-link you will get a description of that service and since it takes a simple string you can invoke the service from here.


We already know the service returns the same result each time, so just press invoke and watch it open the result.



This only took you like 5-10 minutes and you're ready to consume this Web Service from within AX 2012. I recommend having a look at one of the blog posts linked above. In short, you need to do the following:

  • Create a new Visual Studio Project
  • Select .Net Framework 4
  • Select a template from Visual C# and Windows
  • Select the Class Library as template.
  • Give it a name like "DynamicsAXPizzaService".
  • Add a Service Reference and point to http://localhost/service1.asmx
  • Add the project to the AOT
  • Deploy!!
Now you are ready to consume it from within AX. You will have to restart the AX client, as already mentioned in the documentation. 

In order to get you started quickly, I wrote this main-method which you can just copy and paste to test if it works. 

public static void main(Args args)
{
    DynamicsAXPizzaService.WebService1.Service1SoapClient   wcfClient;
    DynamicsAXPizzaService.WebService1.PizzaInfo[]          pizzaInfoArray;
    DynamicsAXPizzaService.WebService1.PizzaInfo            pizzaInfo;

    System.ServiceModel.Description.ServiceEndpoint         endPoint;
    System.ServiceModel.EndpointAddress                     endPointAddress;
    System.Exception                                        ex;
    System.Type                                             type;
    int                                                     i, numOfPizzas;

    str name, description, prize;
    ;

    try
    {
        type            = CLRInterop::getType('DynamicsAXPizzaService.WebService1.Service1SoapClient');
        wcfClient       = AifUtil::createServiceClient(type);

        endPointAddress = new System.ServiceModel.EndpointAddress("http://localhost/service1.asmx");
        endPoint        = wcfClient.get_Endpoint();
        endPoint.set_Address(endPointAddress);

        pizzaInfoArray  = wcfClient.SearchPizza("mozarella");
        numOfPizzas     = pizzaInfoArray.get_Count();

        for(i = 0; i < numOfPizzas; i++)
        {
            pizzaInfo   = pizzaInfoArray.get_Item(i);
            name        = pizzaInfo.get_Name();
            description = pizzaInfo.get_Description();
            prize       = pizzaInfo.get_Prize();

            info(strFmt("%1 - %2 - %3", name, description, prize));
        }
    }
    catch(Exception::CLRError)
    {
        ex = CLRInterop::getLastException();

        while(ex)
        {
            info(CLRInterop::getAnyTypeForObject(ex.ToString()));
            ex = ex.get_InnerException();
        }
    }
}

The output when running this class should be this:


Now that you have this working, you can start tamper with it and make it break and learn how the pieces fits together. Here are a couple of things you might want to try understand:
  • What dll is being used when the X++ code is running client side?
  • Tip: have a look at this path: "%localappdata%\Local\Microsoft\Dynamics AX\VSAssemblies\"
  • What dll is being used when the X++ code is running server side?
  • Tip: find the location where your AOS Server files are installed and look for the VSAssemblies-folder under the bin-folder.
  • What about when you activate hot-swapping of assemblies on the AOS?
  • What happens if you deploy new versions of these dlls and you want the client or the AOS to load this new version?
  • Either restart the client or restart the AOS, depending on what dll you want reloaded.
  • What if you plan to have the dll run only server side and never client side, but you need intellisense while developing the X++ code?
  • You need the dll deployed client side on the developer machine. :-)
Finally, I wanted to show you a neat little tool by JetBrains named dotPeek. If you take any of the dlls you just created and drop them into this tool, you can explore the content and even browse the code. I have used this tool in many different scenarios to peek inside managed assemblies.


If you have any concerns or you bump into any issues while trying to follow the steps in this article, please leave a comment underneath.

Thursday, 11 September 2014

Step to Installation and Configuration IAIF Ax 2012 ,2012R3

Web Services on IIS (AIF) Step by Step Installation and Configuration

Hi Folks,
Please find the below steps to install and configure the AX2012R3 WebServices on IIS(Application Integration Framework).
Webserver role must be installed in a server to install the AX Web services on IIS.
Step 1:- Run the AX2012R3 installation media, click on the Microsoft Dynamics AX components.
Step 2:- Please click on the Next button to progress the installation.
Step 3:- Please click on the Add or modify components, then go with the Next button.
Step 4:- Select the Web Services on IIS Components, then go with the Next button.\
Step 5:- Prerequisite validations will guide to configure the tools,please click on configure and revalidate, then go with the Next button.
Step 6:- Please enter the Business connector user account password, then go with the Next button.
Step 7:- Please select the Default website or Custom Site from the dropdown.
Step 8:- Add the relevant users to get the permissions to get the AIF virtual directory access.
Step 9:- Please check for the prerequisite validation again before installing the components, then go with the Next button.
Step 10:- Please verify the selected Webservices on IIS component, then go with the Install button.
Step 11:- Post installation configuration tasks will be performed.
Step 12:- Make sure the Webservices on IIS component is installed successfully.
Step 13:- To double check the Webservices installation, please navigate to System administration-->Setup-->Services and AIF-->Websites-->Validate and check the configuration.
Many Thanks,
Kanha
"Helping others without any expectation will always made the others to help you"

Saturday, 6 September 2014

Web Service Basic

Web Service Basic

Introduction
We all talk about webservices, webservices can do this and webservices can do that. But when we are asked to make one, we hesitate. Maybe it's because we never made a webservice before, and all the time playing with Webforms and Windows Forms or even Console Applications. By the way, I love Console applications. In this article, I will show you how to create a simple webservice that is consumed by a Console application client.
Making the WebService:
First, start your Visual Studio .NET, and in the project type, select ASP.NET WebService. In the left pane, choose the language of your choice. In this example, I will be using Visual C#.NET. Once you select the project, a page will appear which will be more like a design page, switch to its code view. In the code view, you can see lot of comments and C# code already written for you. You will also see that at the bottom, there is a method HelloWorld which is written for you by default, so you can test your service and of course say hello to the world. After removing the unwanted code and comments, your code will look like this:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
namespace WebServiceExample
{
public class Service1 : System.Web.Services.WebService
{
public Service1()
{

InitializeComponent();
}

// Add the Description Attribute so you
// will know the purpose of your WebService
[WebMethod(Description="This Method prints HelloWorld")]
public string HelloWorld()
{
return "Hello World";
}
}
}


Let's dig into this small code. [WebMethod] Attribute denotes that this method will be used by the clients, also this method has to be public in order for a client to use it. Description inside the WebMethod Attribute just gives the method more meaning. Don't worry about the InitializeComponent() method since it's written by default.
Running the WebService:
OK, now you have made your first kick ass WebService (without even writing a single line of code). Let's run it and check whether it gives the correct result or not. In the Solution Explorer, right click on the .asmx file, and select View in Browser


Once you click on "View in Browser", the next screen you will see will be something like this:

I have erased most of the stuff, so you can only see the method that you need in this example. Below, you can see the method HelloWorld and the description you wrote for the method.
Now, click on the HelloWorld method. I don't want to scare you with all the SOAP and HTTP code produced, so I am only going to paste the screen shot which will be relevant to this example.

Alright, so far so good. Now, just press the Invoke button to see the result of your method named HelloWorld().

This is cool. You have just tested your first webservice and it ran since you didn't coded it. I know what you are thinking right now. Is the client going to see the result like this strange format (this is XML format). Well, of course not. That's why you need to make a Proxy class which consumes this service.
Making a Proxy Client:
Let's make a Console application which consumes this service. You can use any language and platform to consume this service, that's the purpose of XML WebService. Now, this procedure requires some mouse clicking :). So I will write down the steps instead of pasting the screen shots.
  • Start a new project which will be a Console Application in Visual C#.NET.
  • Once you see the code view in the Console application, right click on the project name from the Solution Explorer. Remember that project name will be written in bold.
  • Click on "Add Web Reference".
  • Paste the URL of your WebService. You can get the URL of your WebService when you view your webservice in IE or any other browser.
  • Click GO.
  • Your webservice will be loaded. In the Web Reference Name textbox, write "MyService" and click Add Reference.
  • You will see that the web reference has been added in your Solution Explorer, meaning that webservice is ready to kick some butt.
Now, all you have to do is to make the instance of the WebService class using the reference name that you provided, which is "MyService".
using System;
namespace MyClient
{
class Class1
{

[STAThread]
static void Main(string[] args)
{
// Make an instance of the WebService Class
// using the Web Reference you provided
MyService.Service1 service = new MyService.Service1();
// Assign message what ever is returned
// from HelloWorld in this case "HelloWorld"
string message = service.HelloWorld();
// Prints out the message on the screen
Console.WriteLine(message);
}
}
}
And that's it. You use the webservice class just like any other class. Something you need to keep in mind is that if you decide to make a new method in your webservice and want to make it available to the client, then always remember to build your webservice solution so that the assembly can be updated. If you don't build your webservice, you won't be able to see the methods on the client side.

Labels

#veryusefulcode (1) AIF (8) AOT Maps (1) Args (1) Ax 2009 Reports (2) AX 2012 navigation (1) Ax 2012 Interview Questions (1) AX 7 (2) AX Architecture (1) Ax Backup (1) AX Workflow (2) AX2012 (1) AX2012 R2 (1) Ax2012R3 (1) AX2012R3 Dynamics Connector Step by Step Installation and Configuration (1) AX2012R3 EP Step by Step Installation and Configuration EP R3 (1) AX2012R3 HelpServer Step by Step Installation and Configuration (1) AX2012R3 Rapid Start Connector Step by Step Installation and Configuration (1) AX2012R3 Report Server and Analysis Server Step by Step Installation and Configuration (1) AX7 (1) Best practices (1) Blocking user to enter (1) Collection Classes (1) Container (1) D365FO (3) Data Migration Frame Work ax 2012R3 (1) Deleting duplicate Record from Table – Ax2012 (1) Delivery due date notification workflow in Ax 2012 (1) Development Steps EP (1) Dimensions (1) DIXF (1) DMF in Ax 2012 R3 (1) Dynamics Ax 2012 Interview Questions (1) DYNAMICS AX 2012 INTERVIEW QUESTIONS PART 2 (1) DYNAMICS AX 7 (1) EDT relation Migration Tool (1) EP AX 2012 (1) Ep Lookup (1) Error (1) Event Handler (1) F5 (1) File Handling (4) Filter on AX2012 Listpage (1) filtering (2) financial dimensions in AX 2012 (3) form (1) images (1) Installation and Configration (4) Installation and Configuration (11) Installation of Management Reporter 2012 for AX 2012 (1) Interaction class in ax 2012 (1) Interview Question (1) Interview Questions For Ax 2012 (1) Invent DIm (1) Jobs (2) license (1) List page and form menuitem enable code (1) Methods (1) microsoft Dynamics AX 365FO (1) Number Sequence Generation – AX 2012 (5) Number Sequence2012 (1) OLTP-OLAP (1) Passing Args (1) Passing form caller and menu item caller in ax 2012 (1) Passing Multiple Records Args (1) Posting in Ax 2012 (1) POSTING PURCHASE ORDER (1) Query (1) Query Filter Form (2) Query Ranges in X++ (1) Question and Answer (1) Report (1) Reports Controller class (1) RLS in ax 2009 (1) SALES ORDER IMPORT/EXPORT FRAMEWORK BY DMF (1) Security (1) security roles (1) Security Sysqueryrangeutil (1) Sharepoint 2016 (1) SQL SERVER (1) SSRS (2) SSRS Reports Controller class (2) Table collections & Virtual company (1) Time (1) TIPS AND TRICKS (1) Web service AIF (3) Web Services on IIS (AIF) Step by Step Installation and Configuration (1) workflow ax2012 (1) Workflow installation (1) Workflow Method (3) X++ (1)