C# example

This is developer level content. The toolkit itself is available on the Downloads page.

See Toolkit - Install and Toolkit - Create for general information that will set the context for using this particular example.

The C# example is based on a solution named Custom Rate Engine.sln, which is used to create a simple web application able to handle Pierbridge Ship or Rate Requests and generate rates accordingly. This solution can be modified to match the requirements of each project. The solution consists of following files:

  • CustomRateEngine.aspx
  • CustomRateParameters.cs
  • LogFile.cs
  • Web.config

The CustomRateEngine.aspx.cs consists of the main logic of the Custom Rate Engine. This class contains the following methods, which can be called to demonstrate the creation of a Pierbridge Rate Response.

  • Example1() - This method will receive a request from the product, access some most commonly used parameters from the request, and create a Pierbridge Rate Response based around the rating logic as an http response to each request.
  • Example2() - This method will receive a request from the product, which includes the Pierbridge Rate Request and the Pierbridge Rate or Ship Response present as a child element. Using the request it will access some most commonly used parameters from the response and create a Pierbridge Rate Response based around the rating logic as a http response to each request.

Access request properties

Top

The Pierbridge Rate Request data will be stored in a number of CustomRateParameters which provide an easy access to each individual field through a property. If necessary, access to these parameters can be obtained by:

int packageIndex = 1;
CustomRateParameters parameters = new CustomRateParameters(xml);
parameters.InsuranceType(packageIndex);

It is worth noting that the parameters are pulled out from the request and stored in string format. Therefore, any type checks should be handled accordingly by the custom solution. This is due to the fact that different Custom Rate Engines are likely to deal differently with null and empty values. When expanding the sample code to suit the needs of the project, it may be beneficial to inspect whether the CustomRateParameters class would benefit from appropriate return types for different parameters.

public string Carrier
{
  get { return this._request.SelectSingleNode("/*/Carrier").InnerText; }
}

The example below shows how a type can be checked before casting and accessing an integer value, which originally has been stored as a string parameter.

if (!String.IsNullOrEmpty(parameters.Carrier))
{
  int carrierId = Convert.ToInt32(parameters.Carrier);
}

Access response properties

Top

The Pierbridge Rate or Ship Response is present as a child of the root element. This xml contains the data returned from the carrier, such as rates, zone etc., in the Pierbridge format.

The Pierbridge Ship or Rate Response data is also stored in a number of CustomRateParameters which provide an easy access to each individual field through a property. These properties are can be found in the Response Properties region in this class. If necessary, access to these parameters can be obtained by:

CustomRateParameters parameters = new CustomRateParameters(xml);
String myZone = parameters.Zone;

The rates returned by the carrier can be accessed via a list property named CarrierRates:

public XmlNodeList CarrierRates
{
  get ( return this._request.SelectSingleNode(ResponsePath + "/Rates").ChildNodes; ) 
}

Example methods have been written to demonstrate access to specific child elements of a given rate, for example:

public double RateWeight(XmlNode Rate)
{
  double ratedWeight = 0;
  if (Rate.SelectSingleNode("RateWeight") != null)
  {
  double.TryParse(Rate.SelectSingleNode("RateWeight").InnerText, out ratedWeight);
  }
  return ratedWeight;
}

These properties and methods can be used as follows:

XmlNodeList carrierRates = _parameters.CarrierRates;
double rateWeight = _parameters.RateWeight(carrierRates.itme(0));

Create Pierbridge Rate Response

Top

Any response file manipulation has been done by using a simple string builder to demonstrate the context and in order to keep the example code easy to read.

Based on the simple logic present for rating the response is generated:

StringBuilder builder = new StringBuilder();
string response = String.Empty;
builder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
builder.Append("<PierbridgeRateResponse xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
builder.Append("<TransactionIdentifier/>");
builder.Append("<Packages><Package>");
builder.Append("</Package></Packages></PierbridgeRateResponse>");
response = builder.ToString();

It is important to match the rate type node with the rate engine custom rate.

Rates are inserted appropriately within the response structure:

//chargegroups
builder.Append("<ChargeGroups>");
//chargegroup - accessorials
  builder.Append("<ChargeGroup>");
  builder.Append("<ChargeGroupType>1</ChargeGroupType>");
  builder.Append("<ChargeGroupDescription>Accessorial Charges</ChargeGroupDescription>");
  builder.Append("<ChargeGroupValue>" + accessorialCharge.ToString() + "</ChargeGroupValue>");
  builder.Append("<ChargeGroupCurrency>USD</ChargeGroupCurrency>");
//charges - cod
  builder.Append("<Charges><Charge>");
  builder.Append("<ChargeType>3</ChargeType>");
  builder.Append("<ChargeDescription>COD Charge</ChargeDescription>");
  builder.Append("<ChargeValue>" + codCharge.ToString() + "</ChargeValue>");
  builder.Append("<ChargeGroupCurrency>USD</ChargeGroupCurrency>");
  builder.Append("</Charge>");
//charges - total accessorials
  builder.Append("<Charge>");
  builder.Append("<ChargeType>15</ChargeType>");
  builder.Append("<ChargeDescription>Total Accesorial Charge</ChargeDescription>");
  builder.Append("<ChargeValue>" + accessorialCharge.ToString() + "</ChargeValue>");
  builder.Append("<ChargeGroupCurrency>USD</ChargeGroupCurrency>");
  builder.Append("</Charge></Charges>");
  builder.Append("</ChargeGroup>");
  builder.Append("</ChargeGroups>");

See Charge Types and Charge Base Types for listings from the Transtream database.

Error logging

Top

The rate engine uses a log file for error logging in case any error occurs. If logging is enabled, the error messages are written in a log file. The error log path is configured in the Web.config file and should be modified to match the installation.

<appSettings><add key="LogFilePath" value="C:\Temp\Custom Rate Engine" /><add key="Logging" value="true" /></appSettings>

Error messages are customizable and can be changed:

LogFile.WriteLogToFile("Custom Error Message”);

In addition, the rate engine outputs the request and response xml to the files; Request (CRE).xml and Response (CRE).xml respectively. These files are placed in the same folder as above.

Article last edited 20 July 2017