WCF Web Services & iOS - Part 2
By Mike Gledhill
So, we now have a very basic WCF Web Service, which returns a string in JSON format.
In this section, we're going to link in some SQL Server data, and walk through some of the problems you might encounter.
To do this, you will need the following:
Linking our web service to a SQL Server database
Adding the GetAllCustomers WCF Web Service, which returns JSON data
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
namespace JSONWebService
{
[DataContract]
public class wsCustomer
{
[DataMember]
public string CustomerID { get; set; }
[DataMember]
public string CompanyName { get; set; }
[DataMember]
public string City { get; set; }
}
Now, let's create a web service which can return a collection (List) of these records.
Let's start with the IService1.cs file. Add the following lines, at the top of the class:
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getAllCustomers")]
List<wsCustomer> GetAllCustomers();
...
public class Service1 : IService1
{
public List<wsCustomer> GetAllCustomers()
{
NorthwindDataContext dc = new NorthwindDataContext();
List<wsCustomer> results = new List<wsCustomer>();
foreach (Customer cust in dc.Customers)
{
results.Add(new wsCustomer() {
CustomerID = cust.CustomerID,
CompanyName = cust.CompanyName,
City = cust.City
});
}
return results;
}
...
http://localhost:15021/Service1.svc/getAllCustomers
JSON format
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "getAllCustomers")]
List<wsCustomer> GetAllCustomers();
A quick word about JSON
Adding the GetOrdersForCustomer WCF Web Service, which returns JSON data
http://localhost:15021/Service1.svc/GetOrdersForCustomer/ANATR
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
namespace JSONWebService
{
[DataContract]
public class wsOrder
{
[DataMember]
public int OrderID { get; set; }
[DataMember]
public string OrderDate { get; set; }
[DataMember]
public string ShippedDate { get; set; }
[DataMember]
public string ShipName { get; set; }
[DataMember]
public string ShipAddress { get; set; }
[DataMember]
public string ShipCity { get; set; }
[DataMember]
public string ShipPostcode { get; set; }
}
}
Now, let's create the web service which will return the list of [Order]s for a particular [Customer].
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getAllCustomers")]
List<wsCustomer> GetAllCustomers();
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getOrdersForCustomer/{customerID}")]
List<wsOrder> GetOrdersForCustomer(string customerID);
...
public List<wsOrder> GetOrdersForCustomer(string customerID)
{
NorthwindDataContext dc = new NorthwindDataContext();
List<wsOrder> results = new List<wsOrder>();
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.GetCultureInfo("en-US");
foreach (Order order in dc.Orders.Where(s => s.CustomerID == customerID))
{
results.Add(new wsOrder()
{
OrderID = order.OrderID,
OrderDate = (order.OrderDate == null) ? "" : order.OrderDate.Value.ToString("d", ci),
ShipAddress = order.ShipAddress,
ShipCity = order.ShipCity,
ShipName = order.ShipName,
ShipPostcode = order.ShipPostalCode,
ShippedDate = (order.ShippedDate == null) ? "" : order.ShippedDate.Value.ToString("d", ci)
});
}
return results;
}
http://localhost:15021/Service1.svc/getOrdersForCustomer/ALFKI
http://localhost:15021/Service1.svc/getOrdersForCustomer/ANATR
Passing parameters to WCF Web Service in a URL
public List<wsOrder> GetOrdersForCustomer(string customerID)
{
NorthwindDataContext dc = new NorthwindDataContext();
...
[ServiceContract]
...
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getOrderDetails/{orderID}")]
List<wsOrder> GetOrderDetails(int orderID);
...
[ServiceContract]
...
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getOrderDetails/{orderID}")]
List<wsOrder> GetOrderDetails(string orderID);
...
public List<wsOrder> GetOrderDetails(string orderID)
{
NorthwindDataContext dc = new NorthwindDataContext();
int orderIDnumber = int.Parse(orderID)
var orders = dc.Orders.Where(s => s.OrderID == orderIDnumber);
...
Calling a SQL Server Stored Procedure
USE [Northwind]
GO
EXEC [dbo].[CustOrderHist] 'ANTON'
http://localhost:15021/Service1.svc/getCustomerOrderHistory/ANTON
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
namespace JSONWebService
{
[DataContract]
[Serializable]
public class CustomerOrderHistory
{
[DataMember]
public string ProductName { get; set; }
[DataMember]
public int Total { get; set; }
}
}
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "getCustomerOrderHistory/{customerID}")]
List<CustomerOrderHistory> GetCustomerOrderHistory(string customerID);
public List<CustomerOrderHistory> GetCustomerOrderHistory(string customerID)
{
List<CustomerOrderHistory> results = new List<CustomerOrderHistory>();
NorthwindDataContext dc = new NorthwindDataContext();
foreach (CustOrderHistResult oneOrder in dc.CustOrderHist(customerID))
{
results.Add(new CustomerOrderHistory()
{
ProductName = oneOrder.ProductName,
Total = oneOrder.Total ?? 0
});
}
return results;
}
Summary
Comments