View Single Post
  #4  
Old 09-11-2010, 10:28 AM
dkyle dkyle is offline
New Member
 
Join Date: Jul 2010
Location: The Raymond I.T. Group, Inc.
Posts: 2
Send a message via AIM to dkyle
Default Sample Code

I can give you some sample code of what I'm currently working on. It is a c#.net web application that will serve as a VAN administration portal for me (currently working on client connectivity/authentication).

I encapsulated the ECGridOS API calls into a series of objects that I make calls on (this way I can minimize the number of times I hit the API to gather info).

Here is a snippet of output code in an ASP.NET Web-site:

Code:
<div class="info" style="float: left;">
 <span class="infoHeader">User Information:</span>
 <br />
 <span class="infoItem"><b>ID: </b><%=ConnInfo.UserInfo.UserID %></span>
 <span class="infoItem"><b>Name: </b><%=ConnInfo.UserInfo.LoginName %></span>
 <span class="infoItem"><b>E-mail: </b><%=ConnInfo.UserInfo.EMail %></span>
</div>
Then, in the code behind page, I create an Object of the class I created.
Code:
private ECGridConnect _ecConn;
I also expose it so that the web-page can use it.
Code:
    public ECGridConnect ConnInfo {
        get {
            if (_ecConn != null) {
                return _ecConn;
            } else {
                _ecConn = new ECGridConnect();
                return _ecConn;
            } // end if/else
        }
    } // end property ConnInfo
In a different file, I have the class definition. Here is a snippet from that class (unfortunately I can't give full source).

Code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using net.ecgridos;
using System.Xml;
using System.Web.Services;
using System.Web.Services.Protocols;

/// <summary>
/// Summary description for ECGridConnect
/// </summary>
public class ECGridConnect {

    private String _sessionID;
    private int _iChangeQual;
    private String _interchangeID;
    private String _password;
    private String _userID;
    public event LoggingInEventHandler LoggingIn;
    public event LoggedInEventHandler LoggedIn;
    public event LoggingOutEventHandler LoggingOut;
    public event LoggedOutEventHandler LoggedOut;
    private ECGridOSAPIv2 _ecGrid;
    private ParcelIDInfo[] _pInfo;
    private UserIDInfo _uInfo;
    private SessionInfo _sInfo;
    private NetworkIDInfo _nInfo;


    /// <summary>
    /// Creates a new object of the ECGridConnect class.
    /// </summary>
    public ECGridConnect() {
        _ecGrid = new ECGridOSAPIv2();
        _pInfo = null;
        _uInfo = null;
        _sInfo = null;
        _nInfo = null;
        LoggingIn += new LoggingInEventHandler(ECGridConnect_LoggingIn);
        LoggedIn += new LoggedInEventHandler(ECGridConnect_LoggedIn);
        LoggingOut += new LoggingOutEventHandler(ECGridConnect_LoggingOut);
        LoggedOut += new LoggedOutEventHandler(ECGridConnect_LoggedOut);
    } // end default no-argument constructor

    private void Connect() {
        LoggingIn(_userID);
        _sessionID = _ecGrid.Login(_userID, _password);
        if (_sessionID.Length > 0) {
            //TODO: Nothing to do.  We have a successfull login.
        } else {
            throw new Exception(String.Format("Login failed for user {0}",_userID));
        } // end if/else
        LoggedIn(_userID,_sessionID);
    } // end method Connect

    private void Disconnect() {
        if (String.IsNullOrEmpty(_sessionID)) {
        } else {
            LoggingOut(_userID, _sessionID);
            _ecGrid.Logout(_sessionID);
            LoggedOut(_userID, _sessionID);
        } // end if/else
    } // end method Disconnect

    public void LoadUserInfo() {
        _uInfo = null;
        if(_sInfo != null) {
        } else {
            LoadSessionInfo();
        } // end if/else
        try {
            Connect();
            _uInfo = _ecGrid.UserInfo(_sessionID, _sInfo.UserID);
            Disconnect();
            //There is good data in the InnerXML
            //which can be parsed and used to processes specific exceptions
        } catch (SoapException ex) {
            XmlDocument doc = new XmlDocument();
            XmlNode node;
            doc.LoadXml(ex.Detail.OuterXml);
            node = doc.DocumentElement.SelectSingleNode("ErrorInfo");
            if (node != null) {
                HttpContext.Current.Response.Write(String.Format("SOAP Exception: ({0}) {1}", node.SelectSingleNode("ErrorCode").InnerText, node.SelectSingleNode("ErrorString").InnerText, "<br />\n"));
                HttpContext.Current.Response.Write(String.Format(" Error Item: {0}", node.SelectSingleNode("ErrorItem").InnerText, "<br />"));
                HttpContext.Current.Response.Write(String.Format(" Msg: {0}", node.SelectSingleNode("ErrorMessage").InnerText, "<br />"));
            } else {
                HttpContext.Current.Response.Write("ERROR: " + ex.Message.ToString() + "<br />" + ex.StackTrace + "<br />");
            } // end if/else
        } catch (Exception ex) {
            HttpContext.Current.Response.Write("Error:<br />");
            HttpContext.Current.Response.Write(ex.Message + "<br />");
            HttpContext.Current.Response.Write(ex.StackTrace + "<br />");
        } finally {
            Disconnect();
        } // end try/catch/finally
    } // end method LoadUserInfo

    public void GetInbox() {
        _pInfo = null;
        try {
            Connect();
            _pInfo = _ecGrid.ParcelInBox(_sessionID);
            Disconnect();
            //There is good data in the InnerXML
            //which can be parsed and used to processes specific exceptions
        } catch (SoapException ex) {
            XmlDocument doc = new XmlDocument();
            XmlNode node;
            doc.LoadXml(ex.Detail.OuterXml);
            node = doc.DocumentElement.SelectSingleNode("ErrorInfo");
            if (node != null) {
                HttpContext.Current.Response.Write(String.Format("SOAP Exception: ({0}) {1}", node.SelectSingleNode("ErrorCode").InnerText, node.SelectSingleNode("ErrorString").InnerText, "<br />\n"));
                HttpContext.Current.Response.Write(String.Format(" Error Item: {0}", node.SelectSingleNode("ErrorItem").InnerText, "<br />"));
                HttpContext.Current.Response.Write(String.Format(" Msg: {0}", node.SelectSingleNode("ErrorMessage").InnerText, "<br />"));
            } else {
                HttpContext.Current.Response.Write("ERROR: " + ex.Message.ToString() + "<br />" + ex.StackTrace + "<br />");
            } // end if/else
        } catch (Exception ex) {
            HttpContext.Current.Response.Write("Error:<br />");
            HttpContext.Current.Response.Write(ex.Message + "<br />");
            HttpContext.Current.Response.Write(ex.StackTrace + "<br />");
        } finally {
            Disconnect();
        } // end try/catch/finally
    } // end method GetInbox

    /// <summary>
    /// Retrieves the contents of the inbox for the mailbox.
    /// </summary>
    public ParcelIDInfo[] Inbox {
        get {
            if (_pInfo != null) {
                return _pInfo;
            } else {
                GetInbox();
                return _pInfo;
            } // end if/else
        }
    } // end property Inbox

    /// <summary>
    /// Gets the user information associated with the current login.
    /// </summary>
    public UserIDInfo UserInfo {
        get {
            if (_uInfo != null) {
                return _uInfo;
            } else {
                LoadUserInfo();
                return _uInfo;
            } // end if/else
        }
    } // end property UserInfo

} // end class ECGridConnect
In this way, you can see how in the sample output I provided, I wire-up the class property UserInfo so that I can access all of the variables, manage the connection and display information to the user much faster. I also have custom event handlers set up to detect successful logons and logoffs so that I am sure to close my connection to the ECGridOS system.

I am attempting to setup some kind of caching for the responses from ECGridOS so that it comes back even faster.

Similarly, I encapsulate the inbox so that on a separate "inbox" page, I can bind the ParcelIDInfo property Inbox, to a GridView or other .NET Databound Control.

Hope this helps to give you more information.
Reply With Quote