Accessing the Inventory Api with .Net

Customers using an ASP.NET Framework Client will need to do the following steps in order to call the .Net Inventory API’s services (its functions).
 
General Information
 
  • The Inventory API uses SOAP to communicate.
 
Set Up Steps to Use the API Services (the functions)
 
  1. Add a “service reference” to the client application that will be calling the API functions

          Namespace is: InventoryServiceLibrary

  1. Add entries to the to the client application’s web.config file.

          See the "Client application’s web.config file" section below.

  1. Reference the following: svcutil.exe http://api.reliablesite.net/inventory.svc?wsdl to review the function calls of the API service.

 
Demonstration on How to Code a Call to an API Function
 
Calling the “Inventory Management” service’s “list servers” functionality.
 
Function to be called along with the required parameters:
     ServersList()
 
The function results and server’s object (the server details) are passed back to the calling client.
 
1) Using the .Net language - VB.NET, creating the subroutine that can be called. 
Sub ServersList()  
  Dim strMessage As String = “”
  Dim strSLApiServersList As String = “”
  Dim serverListWithDetailsInfoResult As New ServerListWithDetailsInfoResult
  Dim serverDetailsList As New Server_Details

   ‘ Step 1: Create an instance of the WCF proxy.
   Dim proxyClient As New InventoryService.InventoryServiceClient

   Try
       ‘ Step 2: Call the API function.
       serverListWithDetailsInfoResult = proxyClient.ServersList()

       ‘Show the results.
       Console.WriteLine("")
       Console.WriteLine("Message:       " & serverListWithDetailsInfoResult.Message)
       Console.WriteLine("Call Result:   " & serverListWithDetailsInfoResult.Result.ToString())
       Console.WriteLine("Result Code:  " & serverListWithDetailsInfoResult.Result_Code.
                                        ToString())
       Console.WriteLine("")

    For Each serverDetails In serverListWithDetailsInfoResult.ServerDetailsList
        strSLApiServersList = strSLApiServersList & _
        " Product Id: " & serverDetails.Product_Id & "  Description: " &
        serverDetails.Description & _
        " Data Center: " & serverDetails.Data_Center & "  Detail: " &
        serverDetails.Detail & _
        " Stock: " & serverDetails.Stock & "  Setup 1 Hour: " &
        serverDetails.Setup_1_Hour & _
        " Setup 1 Month: " & serverDetails.Setup_1_Month & "  Setup 3 Month: " &
        serverDetails.Setup_3_Month & _
        " Setup 6 Month: " & serverDetails.Setup_6_Month & "  Setup 12 Month: " &
        serverDetails.Setup_12_Month & _
        " Setup 24 Month: " & serverDetails.Setup_24_Month & "  Setup 36 Month: " &
        serverDetails.Setup_36_Month & _
        " Recurring 1 Hour: " & serverDetails.Recurring_1_Hour & "  Recurring 1 Month: "
        & serverDetails.Recurring_1_Month & _
        " Recurring 3 Month: " & serverDetails.Recurring_3_Month & "  Recurring 6 Month:
        " & serverDetails.Recurring_6_Month & _
        " Recurring 12 Month: " & serverDetails.Recurring_12_Month & "  Recurring 24
        Month: " & serverDetails.Recurring_24_Month & _
        "  Recurring 36 Month: " & serverDetails.Recurring_36_Month & vbCrLf & vbCrLf
       Console.WriteLine("")
       Console.WriteLine("List: " & strSLApiServersList)
    Next

      ‘Step 3: Closing the client gracefully closes the connection and cleans up resources.
      proxyClient.Close()                   
   Catch timeout As TimeoutException
        Console.WriteLine("Critical Error - from API web service(ServersList) - at 
        timeout. Contact ReliableSite, do not continue. " & timeout.ToString())
        strMessage = "Critical Error - from API web service(ServersList) - at timeout”
   Catch ex As Exception
       Console.WriteLine("Critical Error - from API web service(ServersList). Contact 
       ReliableSite, do not continue. " & ex.Message)
       StrMessage = "Critical Error - from API web service(ServersList)”
   Finally
       If InStr(strMessage, "Critical Error") > 0 Then
           proxyClient.Abort()
       End If
   End Try
End Sub
 
2) Using the .Net language - C#, creating the subroutine that can be called.
public void ServersList()
{
  string strMessage = "";
  string strSLApiServersList = "";
  ServerListWithDetailsInfoResult serverListWithDetailsInfoResult = new 
  ServerListWithDetailsInfoResult();
  Server_Details serverDetailsList = new Server_Details();

  // Step 1: Create an instance of the WCF proxy.
  InventoryService.InventoryServiceClient proxyClient = new
  InventoryService.InventoryServiceClient();

  Try
 {
  // Step 2: Call the API function.
  serverListWithDetailsInfoResult = proxyClient.ServersList();

  //Show the results.
  Console.WriteLine("");
  Console.WriteLine("Message:      " + serverListWithDetailsInfoResult.Message);
  Console.WriteLine("Call Result:  " + serverListWithDetailsInfoResult.Result.ToString());
  Console.WriteLine("Result Code:  " +
  serverListWithDetailsInfoResult.Result_Code.ToString());
  Console.WriteLine("");

  foreach (void serverDetails_loopVariable in
  serverListWithDetailsInfoResult.ServerDetailsList)
 {
  serverDetails = serverDetails_loopVariable;
  strSLApiServersList = strSLApiServersList + " Product Id: " + serverDetails.Product_Id + "    Description: " + serverDetails.Description + " Data Center: " + serverDetails.Data_Center + "  Detail: " + serverDetails.Detail + " Stock: " + serverDetails.Stock + "  Setup 1 Hour: " + serverDetails.Setup_1_Hour + " Setup 1 Month: " + serverDetails.Setup_1_Month + "  Setup 3 Month: " + serverDetails.Setup_3_Month + " Setup 6 Month: " + serverDetails.Setup_6_Month + "  Setup 12 Month: " + serverDetails.Setup_12_Month + " Setup 24 Month: " + serverDetails.Setup_24_Month + "  Setup 36 Month: " + serverDetails.Setup_36_Month + " Recurring 1 Hour: " + serverDetails.Recurring_1_Hour + "  Recurring 1 Month: " + serverDetails.Recurring_1_Month + " Recurring 3 Month: " + serverDetails.Recurring_3_Month + "  Recurring 6 Month: " + serverDetails.Recurring_6_Month + " Recurring 12 Month: " + serverDetails.Recurring_12_Month + "  Recurring 24 Month: " + serverDetails.Recurring_24_Month + " Recurring 36 Month: " + serverDetails.Recurring_36_Month + Constants.vbCrLf + Constants.vbCrLf;
  Console.WriteLine("");
  Console.WriteLine("List: " + strSLApiServersList);
  }

  //Step 3: Closing the client gracefully closes the connection and cleans up resources.
  proxyClient.Close();
 } 
  catch (TimeoutException timeout) {
  Console.WriteLine("Critical Error - from API web service(ServersList) - at timeout. Contact
  ReliableSite, do not continue. " + timeout.ToString());
  strMessage = "Critical Error - from API web service(ServersList) - at timeout";
}
 catch (Exception ex) {
  Console.WriteLine("Critical Error - from API web service(ServersList). Contact ReliableSite,
  do not continue. " + ex.Message);
  strMessage = "Critical Error - from API web service(ServersList)";
}
 finally {
  if (Strings.InStr(strMessage, "Critical Error") > 0) {
    proxyClient.Abort();
    }
  }
}
 
Client application’s web.config file.
 
<configuration>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IInventoryService" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://api.reliablesite.net/Inventory.svc/Inventory"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IInventoryService"
        contract="InventoryService.IInventoryService" name="BasicHttpBinding_IInventoryService" />
    </client>
    <diagnostics wmiProviderEnabled="true">
      <messageLogging logEntireMessage="false" logMalformedMessages="false" logMessagesAtTransportLevel="false" logMessagesAtServiceLevel="false"/>
    </diagnostics>
  </system.serviceModel>
</configuration>

Add Feedback