Friday, November 16, 2012

Lawson Web Services and .Net - a guest post

My friend and colleague Paul Grooby at Resene Paints in Wellington was kind enough to contribute the post below about his trials and tribulations with consuming Lawson Web Services in .Net.



Using Visual Studio .Net and Lawson Web Services.
Paul Grooby – 2012-11-02.

Using Lawson WebServices is a doddle, once you understand how it fits together. And if you want to generate simpler data entry and display screens (without SQL and wrapping API’s then this is the ticket).  Once you've created your web-services in Lawson Web Services and published these you can see them in a browser before you start testing them using SOAPUI.


Click on the WSDL file link above – this will load the XML file in the browser that you can then save.


Choose File ->Save from the menu to save the file as a WSDL file on your system.

Now a caveat – while Lawson Web Services generated xsd:datetime definitions for some of the API fields, it doesn't accept accept xsd:datetime inputs to them – so open the saved WSDL file in a text editor and do a find and replace on any datetime fields.


Replace the datetime fields with a string datatype. Once you've checked the remainder of the WSDL file its time to do the cool stuff and fire up Visual Studio (we’ll need this WSDL file later).  I'm going to create a blank web application for this demo (it could as easily be a console application or windows application).

I normally add the external WSDL files to the application and place them in their own folder in the project.  Locate the file and add to the project.


From here select the file and in the properties box of the studio project select and copy the file path.

With the file path copied to the clipboard, select the option to add a service reference to the application..

The following dialog box will be displayed:
Paste the file path into the Address bar – Don’t click Ok just yet – click the advanced button to check a couple of important options.

 Ensure that these two check boxes are checked and then click the OK button.

Give the webservice a relevant namespace. As a naming convention I tend to leave these the same name as the API / Webservice so that these can be traced back.

Once you click the Ok button a list of service endpoints will be displayed.

Click Ok – Visual Studio will do its thing and generate the necessary references and stubs for the program.

The project will now look like the following 

If your project doesn't show all of the files then click on the button highlighted to see them.

The two files with the extension 'svcinfo' contain details of where the endpoint of the service is located. This is important in that you’d normally develop, test and then deploy in the different environments. If you don’t remember that the endpoints are embedded in these files you could be pointing at the wrong environment.

However to save a little hassle, and this is why we copied the file locally we can edit our WSDL file that's contained in the project.

The endpoint is embedded in the WSDL – if you change this to point to say PROD and regenerate the files, the files with the svcinfo extension will be updated – follow this process here:

1: Alter the end point in the WSDL file. Save.

2: Against the service select the option to configure the service reference
This will display the follow dialogue box:
Make sure the address is pointing to your local file then click Ok.  

From the project menu for the service select the option to update the service reference:

You can check that the service has taken by opening the svcinfo files and checking the end point.

This process also updates the web.config file in this case so we just need to check that this has been updated.

If we see two endpoints with the same contract we’re in trouble – delete the incorrect one as the process will not work.

So that’s all there is to it for the adding of a reference. If you want to use the interactions you’ll need to write a bit of code. The following section has a simple class that consumes the objects used above.

The following class is used to interact with the webservice (I won’t teach you about programming – suffice to say this works a treat).  It creates a 'Customer' object, calls the various methods to populate the relevant bits of information and retains that information.

using System;
using SampleLWSApplication.CRS610MI;

namespace SampleLWSApplication
{
    public class Customer
    {
        #region PrivateVariables
        private String _company = "";

        // also based on this customer we should be able to also use the GetBasicData
        private String _customername = "";

        private String _customernumber = "";
        private String _customerpassword = "";
        private String _customersordernumber = "";
        private String _division = "";
        private String _facility= "";

        // get the other details such as order number , etc
        private String _ordertype = "";
        private String _password = "";
        private String _payer = "";
        private DateTime _requesteddeliverydate ;
        private String _transactionreason= "";
        private String _username = "";
        private String _warehouse= "";
        private String _emailaddress = "";
        private String _specialnstructions = "";
        #endregion

        public Customer()
        {
            // create the class
        }
        /// <summary>
        /// Get CustomerData is to pull the data through for the customer
        /// </summary>
        /// <param name="CustomerNumber"></param>
        public void GetBasicData(String CustomerNumber)
        {
            // code to do a lookup against the customer and retrieve the basic data from the system
            // always need a client
            CRS610MI.CRS610MIClient crs610MIClient = new CRS610MIClient();
            // create the soap header
            CRS610MI.headerType mwsheader = new CRS610MI.headerType();
            mwsheader.user = _username; // user name for the API from the configuration file
            mwsheader.password = _password; // password for the API from the configuration file
            mwsheader.company = _company;
            mwsheader.division = "xxx";
            // create a new object to hold the details
            CRS610MI.GetBasicDataItem getBasicData = new GetBasicDataItem();
            // set the variables for the process
            getBasicData.Company = 100;
            // set to the passed
            getBasicData.CustomerNumber = CustomerNumber.ToUpper(); // force to upper case

            CRS610MI.GetBasicDataCollection collection = new GetBasicDataCollection();
            collection.maxRecords = 1000;
            collection.GetBasicDataItem = new GetBasicDataItem[1];
            collection.GetBasicDataItem[0] = getBasicData;

            CRS610MI.GetBasicDataRequest crs610MIRequest = new GetBasicDataRequest(mwsheader, collection);

            try
            {
                // pul back the data responses
                CRS610MI.GetBasicDataResponse responseItem = crs610MIClient.GetBasicData(crs610MIRequest);
                // turn this into an array
                CRS610MI.GetBasicDataResponseItem[] ri = responseItem.GetBasicDataResponse1;
                // loop through the array
                for (int i = 0; i < ri.Length; i++)
                {
                    // output the data
                    // set the values
                    _customername = ri[i].CustomerName;
                    _transactionreason = ri[i].FreeField3;
                    _division = ri[i].Division;
                }
            }
            catch (System.ServiceModel.FaultException ex)
            {
                Console.Write(ex.Message);
            }
        }

        /// <summary>
        /// Get CustomerData is to pull the data through for the customer
        /// </summary>
        /// <param name="CustomerNumber"></param>
        public void GetOrderInfo(String CustomerNumber)
        {
            // code to do a lookup against the customer and retrieve the basic data from the system
            // always need a client
            CRS610MI.CRS610MIClient crs610MIClient = new CRS610MIClient();
            // create the soap header
            CRS610MI.headerType mwsheader = new CRS610MI.headerType();
            mwsheader.user = _username; // user name for the API from the configuration file
            mwsheader.password = _password; // password for the API from the configuration file
            mwsheader.company = _company;
            mwsheader.division = "xxx";
            // create a new object to hold the details
            CRS610MI.GetOrderInfoItem getOrderInfo = new GetOrderInfoItem();
            // set the variables for the process
            getOrderInfo.Company = 100;
            // set to the passed
            getOrderInfo.CustomerNumber = CustomerNumber.ToUpper(); // force to upper case

            CRS610MI.GetOrderInfoCollection collection = new GetOrderInfoCollection();
            collection.maxRecords = 1000;
            collection.GetOrderInfoItem = new GetOrderInfoItem[1];
            collection.GetOrderInfoItem[0] = getOrderInfo;

            CRS610MI.GetOrderInfoRequest crs610MIRequest = new GetOrderInfoRequest(mwsheader, collection);

            try
            {
                // pull back the data responses
                CRS610MI.GetOrderInfoResponse responseItem = crs610MIClient.GetOrderInfo(crs610MIRequest);
                // turn this into an array
                CRS610MI.GetOrderInfoResponseItem[] ri = responseItem.GetOrderInfoResponse1;
                // loop through the array
                for (int i = 0; i < ri.Length; i++)
                {
                    // output the data
                    // set the values
                    _facility = ri[i].Facility;
                    _warehouse = ri[i].Warehouse;
                    _payer = ri[i].Payer;
                    _division = ri[i].Division;
                    _ordertype = ri[i].CustomerOrderType;
                }
            }
            catch (System.ServiceModel.FaultException ex)
            {
                Console.Write(ex.Message);
            }
        }

        // Properties here

        #region Properties

        public String SpecialInstructions
        {
            get { return _specialnstructions; }
            set {_specialnstructions = value;}


        }
        public String Company
        {
            get { return _company; }
            set { _company = value; }
        }

        public String EmailAddress
        {
            get { return _emailaddress; }
            set { _emailaddress = value; }
        }
        public String CustomerName
        {
            get { return _customername; }
            set { _customername = value; }
        }

        public String CustomerNumber
        {
            get { return _customernumber; }
            set { _customernumber = value; }
        }

        public String CustomerOrderNumber
        {
            get { return _customersordernumber; }
            set { _customersordernumber = value; }
        }

        public String CustomerPassword
        {
            get { return _customerpassword; }
            set { _customerpassword = value; }
        }

        public String Division
        {
            get { return _division; }
            set { _division = value; }
        }

        public String Facility
        {
            get { return _facility; }
            set { _facility = value; }
        }

        public String OrderType
        {
            get { return _ordertype; }
            set { _ordertype = value; }
        }

        public String Password
        {
            get { return _password; }
            set { _password = value; }
        }

        public String Payer
        {
            get { return _payer; }
            set { _payer = value; }
        }

        public DateTime RequestedDeliveryDate
        {
            get { return _requesteddeliverydate; }
            set { _requesteddeliverydate = value; }
        }

        public String TransactionReason
        {
            get { return _transactionreason; }
            set { _transactionreason = value; }
        }

        public String UserName
        {
            get { return _username; }
            set { _username = value; }
        }

        public String Warehouse
        {
            get { return _warehouse; }
            set { _warehouse = value; }
        }

        #endregion Properties

       
    }
}

A note or two about the code.

The basic structure for each call is the following:
  • Create a client
  • Create a mwsheader object (this is the SOAP Header)
  • Create a base item for the method to pass parameter
  • Create a collection of correct return types
  • Add the base item as array 0 to pass
  • Add a request of the correct type
  • Call the method and return a collection of responseitems of the correct type
  • Check for any errors and if none use the returned collection of types in any manner that you like – for 'Get' methods its likely that there is only one object returned, for List methods there is likely to be 0 to many.

Importantly – You’ll also find problems with the service if using a List type operation where the amount of data returned by the service exceeds the file size (not sure where this is set but it seems to be around 64K) – to get around this problems use the following syntax when creating the client.

Note that I've also added complexity by having this endpoint embedded in the web.config file – its' my style of programming.

String endPoint = System.Configuration.ConfigurationManager.AppSettings["INTERNETORDERSENDPOINT"];

WebOrders.InternetOrdersClient orderClient = new InternetOrdersClient(new asicHttpBinding(BasicHttpSecurityMode.None) { MaxReceivedMessageSize = 2147483647, MaxBufferSize = 2147483647 }, new EndpointAddress(endPoint));



In the above example we've set the return size to a huge number – this get around the limitation but still may not be enough – in which case you’d probably want to look at the fields being output from the Web service and whether you need them all.

Enjoy :-)


2 comments:

  1. Need help with an error I'm getting when I execute this method.

    CRS610MI.GetBasicDataResponse responseItem = crs610MIClient.GetBasicData(crs610MIRequest);

    I've tested the user name and password against the Web Service in MWS Designer and it worked ok. What am I missing?

    Here's the error.

    System.ServiceModel.Security.MessageSecurityException: The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic Realm="M3_Development"'. ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized.

    ReplyDelete
    Replies
    1. Hi, odds are the M3 MWS server has been set up to require authentication to be passed in the HTTP header rather than in the body of the document.

      Here's an example piece of code that may help:

      private PMS050_WS.JBC_ManufacturingClient CreateClient()
      {
      try
      {
      PMS050_WS.JBC_ManufacturingClient client = null;
      BasicHttpBinding bhbServiceReference = new BasicHttpBinding();
      bhbServiceReference.Name = "JBC_ManufacturingPort";
      bhbServiceReference.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
      bhbServiceReference.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
      bhbServiceReference.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
      bhbServiceReference.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;
      bhbServiceReference.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
      EndpointAddress endpointaddress = new EndpointAddress(_endPoint);
      client = new JBC_ManufacturingClient(bhbServiceReference , endpointaddress);
      client.ClientCredentials.UserName.UserName = _M3UID;
      client.ClientCredentials.UserName.Password = _M3PWD;

      return client;

      }

      The key parts should be:

      bhbServiceReference.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

      and

      client.ClientCredentials.UserName.UserName = _M3UID;
      client.ClientCredentials.UserName.Password = _M3PWD;

      Hope that helps,

      Al.

      Delete