Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Use WPF and WCF to Build Next-generation Applications : Page 2

Microsoft's new Expression tool allows designers to pass their output directly to developers in order to create rich XAML applications with an integrated toolset. Find out how to build a service using the Windows Communication Foundation and create a test client for it using "Cider," the plug-in for Visual Studio.NET that allows you to build XAML interfaces.


advertisement
Building the Service Layer
After following the installation procedures, you now have a development environment that allows you to build next-generation Windows and Web applications—congratulations! So, let's get started.

To build a new WinFX service, use the File->New Web Site command in Visual Studio.NET. If you have the new environment set up correctly, your dialog will look that in Figure 1.

Figure 1. Create a new WinFX service.
You'll see that there is a new template called 'WinFX' service. Selecting this template creates a solution containing everything needed for a Windows Communication Foundation service. You can find the finished service in the download.



This service wraps the underlying database in a WCF layer. Achieving this is very simple.

First, add a new DataSet to the project, and then add two simple queries. The first query selects the full details for a specific record, including details from both the Product table and the ProductPhotos table. The SQL to get this data looks like this:

SELECT Production.Product.ProductID, Production.Product.Name, Production.Product.ProductNumber, Production.Product.ListPrice, Production.Product.Color, Production.Product.Size, Production.Product.Style, Production.ProductPhoto.ThumbNailPhoto, Production.ProductPhoto.LargePhoto FROM Production.Product INNER JOIN Production.ProductProductPhoto ON Production.Product.ProductID = Production.ProductProductPhoto.ProductID INNER JOIN Production.ProductPhoto ON Production.ProductProductPhoto.ProductPhotoID = Production.ProductPhoto.ProductPhotoID WHERE (Production.Product.ProductID = @MyID)

The query takes one parameter, @MyID, which the service passes to it.

The second query returns a subset of data for all the records in the database. To keep it simple, I've restricted the query so it includes only records where the price exceeds a certain amount, as the database is very large.

SELECT ProductID, Name FROM Production.Product WHERE (ListPrice > 100)

The service that exposes this information provides the following operation contracts, as defined by WCF:

[OperationContract] string GetProductDescription(int nID); [OperationContract] recordDetails[] GetAllRecordDetails(); [OperationContract] fullRecord GetFullRecordDetails(int nID);

GetProductDescription returns the name of the product with the specified ID.

GetAllRecordDetails returns an array (of type recordDetails) containing the product ID and the name for each product in the database.

GetFullRecordDetails returns an object of type fullRecord for a product specified by its ID.

Figure 2. View the WCF service.
These operation contracts require some data contracts to define the data types being passed across the wire. These are called recordDetails and fullRecord. You can see the DataContracts that define them here.

[DataContract()] public class recordDetails { [DataMember] public int nID; [DataMember] public string strName; } [DataContract()] public class fullRecord { [DataMember] public int nID; [DataMember] public string strName; [DataMember] public string strProductNumber; [DataMember] public Decimal nListPrice; [DataMember] public string strColor; [DataMember] public string strSize; [DataMember] public string strStyle; [DataMember] public byte[] bThumbNailPhoto; [DataMember] public byte[] bLargePhoto; }

After setting these contracts up, it's pretty straightforward to write C# code that queries the database based on the input parameters and returns the appropriate values. You can see the implementation of each operation contract in the download. As an example, here's how the GetAllRecordDetails operation is implemented.

public recordDetails[] GetAllRecordDetails() { AWProductTableAdapters.ProductTableAdapter da = new AWProductTableAdapters.ProductTableAdapter(); AWProduct.ProductDataTable dt = da.GetData(); recordDetails[] recs = new recordDetails[dt.Rows.Count]; int n = 0; foreach (AWProduct.ProductRow row in dt.Rows) { recordDetails rd = new recordDetails(); rd.nID = row.ProductID; rd.strName = row.Name; recs[n] = rd; n++; } return recs; }

If the service is implemented correctly, when you compile and run it you'll see a result like that in Figure 2, by browsing to Service.svc on the implementation Web.

Author's Note: Copy the WSDL URI at the top of the Web service test screen. You'll need it in the next step.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap