Logo
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
EPL Standings
 
 
Windows Azure

Web Services and Azure : Our WCF web services

8/28/2012 6:41:23 PM
Our WCF web service is going to expose two functions to return data to the client and one routine to add a message to the queue when an order status is updated on the client.

ERP service interface — IERPService.vb

These following functions and routines will be exposed when the service is called from the client. This is our code in our IERPService.vb file, in the WCFWebService1 role:

Imports System.ServiceModel
IERPService.vb file, WCF web serviceabout' NOTE: If you change the class name "IERPService" here, you must also update the reference to "IERPService" in Web.config.
<ServiceContract()> _
Public Interface IERPService
<OperationContract()> _
Function LoadStartupData() As DataSet
<OperationContract()> _
Function GetOrderStatusForOrder(ByVal iOrderHeaderID As Integer) As String
<OperationContract()> _
Sub AddOrderStatusUpdateToQueue(ByVal iOrderHeaderID As Integer, ByVal iOrderStatusID As Integer)
<DataContract()> _
Class OrderStatus
Private statusName_value As String
<DataMember()> _
Public Property StatusName() As String
Get
Return statusName_value
End Get
Set(ByVal value As String)
statusName_value = value
End Set
End Property
End Class
End Interface


					  

Now that we've created our interface, we can see it looks very similar to a traditional interface, but the class and methods are decorated with contract attributes.

Service Contract

As we mentioned above, a Service Contract is a class-level attribute. The Service Contract is the top level of the service definition, and encapsulates both the operations and data. Just as methods and properties are children of a class, Operation Contracts and Data Contracts are children of a Service Contract.

Operation Contract

The Operation Contract specifies the methods and the method signatures that the web service client can call. Not all methods in the Service Contract need to be labelled as Operation Contracts. Web services may use any number of helper methods to support the publicly accessible ones.

Data Contract

The Data Contract describes how the returned data will be serialized or deserialized. A Data Contract is a separate class in our interface; the data elements are properties with the<DataMember()> attribute.

If we are returning a simple data structure say a list of names we do not necessarily need to establish a Data Contract. However, using a Data Contract is recommended as a best practice.

For more advanced data types, even something as basic as a list of name-value pairs, we need to include a Data Contract. We'll use a Data Contract to return the status of an order.

Using ADO.NET datasets

As ADO.NET datasets are serializable objects, it is possible to skip using a Data Contract, and allow WCF to serialize a dataset. The one caveat is that we need to make sure our client is .NET based so that it can deserialize the dataset properly. An advantage ADO.NET datasets have is returning multiple recordsets in the same method call. This can speed up performance by reducing the number of server requests. We'll use this technique to load our client application's startup data.

ERP service implementation — ERPService.svc.vb

Here are the actual functions and routines that will be executed when called from the client. We can keep both the functions and routines directly linked to the service calls (these are marked with Implements IERPService.[function or routine name]) and also other functions and routines that can be called (just like any other class):

Imports System.Data.SqlClient
ERPService.svc.vb, WCF web serviceaboutImports Microsoft.WindowsAzure
Imports Microsoft.WindowsAzure.StorageClient
Imports Microsoft.WindowsAzure.ServiceRuntime
' NOTE: If you change the class name "ERPService" here, you must also update the reference to "ERPService" in Web.config and in the associated .svc file.
Public Class ERPService
Implements IERPService


					  

LoadStartupData service function

This function returns a dataset to the client with two different DataTables one is the list of orders not yet complete and the other is a list of order statuses.

Private Function LoadStartupData() As DataSet Implements IERPService.LoadStartupData
service functionLoadStartUpDataDim _dataSet As New DataSet
_dataSet = GetOrdersNotComplete(_dataSet)
_dataSet = GetOrderStatuses(_dataSet)
Return _dataSet
End Function


					  

GetOrderStatusForOrder service function

This function will accept an Order Header ID and return the status for that particular order. This will be used to show the order status for a selected order, in a list box, in our client ERP application.

Private Function GetOrderStatusForOrder(ByVal iOrderHeaderID As Integer) As String Implements IERPService.GetOrderStatusForOrder
Dim _connStr As String = ConfigurationManager.ConnectionStrings ("portal").ConnectionString
Dim _SQLcon As New SqlConnection(_connStr)
Dim _SQLcmd As New SqlCommand()
_SQLcon.Open()
With _SQLcmd
ERPService.svc.vb, WCF web serviceGetOrderStatusForOrder service function.CommandText = "GetOrderStatusForOrderHeaderID"
.CommandType = CommandType.StoredProcedure
.Connection = _SQLcon
.Parameters.AddWithValue ("@orderHeaderID", iOrderHeaderID)
End With
Return _SQLcmd.ExecuteScalar().ToString
End Function


					  

AddOrderStatusUpdateToQueue service function

The following function will take the Order Header ID for a selected order, the Order Status ID for a selected status, and add a message to our queue for our worker role to pick up and update the order .

Private Sub AddOrderStatusUpdateToQueue(ByVal iOrderHeaderID As Integer, ByVal iOrderStatusID As Integer) Implements IERPService.AddOrderStatusUpdateToQueue
Dim _account = CloudStorageAccount.DevelopmentStorageAccount()
Dim _client = _account.CreateCloudQueueClient()
Dim _queue As CloudQueue = _client.GetQueueReference("orderupdatequeue")
_queue.CreateIfNotExist()
Dim _msg As New CloudQueueMessage (iOrderHeaderID & "," & iOrderStatusID)
_queue.AddMessage(_msg)
End Sub


					  

GetOrdersNotComplete, GetOrderStatuses, and CreateDataSetFromDataReader class functions

The following functions will do the work of retrieving data and packaging the DataTables into the DataSet to return to the client. These functions will be called by the service functions we looked at in the previous section. They do not implement a service function.

Private Function GetOrdersNotComplete (ByVal dsLoadData As DataSet) As DataSet
Dim _connStr As String = ConfigurationManager.ConnectionStrings ("portal").ConnectionString
Dim _SQLcon As New SqlConnection(_connStr)
Dim _SQLcmd As New SqlCommand()
_SQLcon.Open()
class functionsGetOrderStatusesWith _SQLcmd
.CommandText = "GetOrdersNotComplete"
.CommandType = CommandType.StoredProcedure
.Connection = _SQLcon
End With
dsLoadData = CreateDataSetFromDataReader (_SQLcmd.ExecuteReader(), dsLoadData, "OrdersNotComplete")
Return dsLoadData
End Function
Private Function GetOrderStatuses (ByVal dsLoadData As DataSet) As DataSet
Dim _connStr As String = ConfigurationManager.ConnectionStrings ("portal").ConnectionString
Dim _SQLcon As New SqlConnection(_connStr)
Dim _SQLcmd As New SqlCommand()
_SQLcon.Open()
With _SQLcmd
.CommandText = "GetOrderStatuses"
.CommandType = CommandType.StoredProcedure
.Connection = _SQLcon
End With
dsLoadData = CreateDataSetFromDataReader (_SQLcmd.ExecuteReader(), dsLoadData, "OrderStatuses")
Return dsLoadData
End Function
Private Function CreateDataSetFromDataReader (ByVal drReader As SqlDataReader, ByVal dsDataSet As DataSet, ByVal sTableName As String) As DataSet
ERPService.svc.vb, WCF web serviceGetOrderStatuses functionDo
Dim _schemaTable As DataTable = drReader.GetSchemaTable()
Dim _dataTable As New DataTable()
If _schemaTable IsNot Nothing Then
'The SqlDataReader returned results
'Set the DataTable Name to reference from DataSet
_dataTable.TableName = sTableName
For i As Integer = 0 To _schemaTable.Rows.Count - 1
Dim _dataRow As DataRow = _schemaTable.Rows(i)
'Create the column names
Dim _columnName As String = _dataRow("ColumnName").ToString
'Set the column type
Dim _column As New DataColumn (_columnName, DirectCast (_dataRow("DataType"), Type))
_dataTable.Columns.Add(_column)
Next
'Add DataTable to DataSet
dsDataSet.Tables.Add(_dataTable)
'Fill DataTable with results from SqlDataReader
While drReader.Read()
Dim _dataRow As DataRow = _dataTable.NewRow()
For i As Integer = 0 To drReader.FieldCount - 1
_dataRow(i) = drReader.GetValue(i)
Next
_dataTable.Rows.Add(_dataRow)
End While
End If
Loop While drReader.NextResult()
Return dsDataSet
End Function
End Class			  
Other -----------------
- Web Services and Azure : Creating a new WCF service web role
- Azure Blob Storage : Windows Azure Content Delivery Network, Blob Storage Data Model
- Azure Blob Storage : Blobs in the Azure ecosystem, Creating Blob Storage
- The Nickel Tour of Azure : How are Azure costs calculated?
- The Nickel Tour of Azure : Explaining Azure to the managers
- Application Life Cycle Management
- Sharing Digital Photographs : Exploring Photo-Sharing Communities
- Sharing Digital Photographs : Exploring Online Photo-Editing Applications
- Surfacing SQL Azure Data in Bing Maps by Using the Client Object Model
- Storing and Sharing Files and Other Online Content : Exploring Online Bookmarking Services
- Storing and Sharing Files and Other Online Content : Understanding Cloud Storage & Evaluating Online File-Storage and -Sharing Services
- Integrating the SharePoint Server Object Model and the Entity Data Model (part 2) - Create a Meeting Scheduler Visual Web Part
- Integrating the SharePoint Server Object Model and the Entity Data Model (part 1) - Create a Console Application to Write Data to a SharePoint List
- Collaborating on Presentations : Evaluating Web-Based Presentation Applications (part 2)
- Collaborating on Presentations : Evaluating Web-Based Presentation Applications (part 1)
- Securing the Connection to SQL Azure (part 3) - Surface SQL Azure Data in a Visual Web Part
- Securing the Connection to SQL Azure (part 2) - Set Permissions for an External Content Type
- Securing the Connection to SQL Azure (part 1) - Create an Application ID & Create an External Content Type
- Consuming SQL Azure Data : Integrating SQL Azure with BCS by Using SharePoint Designer 2010
- Collaborating on Databases : Exploring Web-Based Databases
 
 
Most view of day
- Microsoft Dynamics GP 2010 : Improving performance by adjusting AutoComplete settings, Cleaning up Accounts Receivable with Paid Transaction Removal
- Exchange Server 2007 : Migrating from Windows 2000 Server to Windows Server 2003 (part 3) - Upgrading the AD Schema Using adprep
- Mix and Match with Old Windows and Macs : Internetworking with Macintosh
- Windows 7 Mobility Features : Using Windows 7 with a Netbook
- Windows Server 2008 : Promoting and Demoting a Domain Controller - Promoting a DC to an RODC with an Existing Account
- SQL Server 2008 R2 : Configuring Resource Governor (part 2) - Defining Workload Groups, Creating Workload Groups in T-SQL
- SharePoint 2010 : Packaging and Deployment Model - Working with Packages
- Windows Server 2003 : Protecting Hosts with Windows Host Firewalls - Routing and Remote Access Basic Firewall
- Managing Client Protection : User Account Control (part 3) - UAC Virtualization, UAC and Startup Programs, Compatibility Problems with UAC
- System Center Configuration Manager 2007 : Operating System Deployment - Tips and Techniques
Top 10
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 3) - Creating IPv4 DHCP Scopes
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 2) - Installing DHCP Server and Server Tools
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 1)
- Windows Server 2012 : DHCP,IPv6 and IPAM - Understanding the Components of an Enterprise Network
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 3) - Translating Text with the Mini Translator
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 2) - Translating a Word or Phrase with the Research Pane
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 1) - Setting Options for the Research Task Pane, Searching with the Research Task Pane
- Microsoft OneNote 2010 : Doing Research with Linked Notes (part 2) - Ending a Linked Notes Session, Viewing Linked Notes
- Microsoft OneNote 2010 : Doing Research with Linked Notes (part 1) - Beginning a Linked Notes Session
- Microsoft OneNote 2010 : Doing Research with Side Notes (part 3) - Moving Side Notes to Your Existing Notes
 
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro