Book Image

Microsoft Silverlight 4 Data and Services Cookbook

Book Image

Microsoft Silverlight 4 Data and Services Cookbook

Overview of this book

Microsoft Silverlight is a programmable web browser plugin that enables features including animation, vector graphics, and audio-video playback – features that characterize Rich Internet Applications. However, Silverlight is a great (and growing) Line-Of-Business platform and is increasingly being used to build data-driven business applications. Silverlight Data Services enable efficient access to your data, allowing you to draw on multiple sources of data and solve particular data problems. There is very little existing material that demonstrates how to build data-driven solutions with the platform. Silverlight 3 made a big step into Line-Of-Business data services and Silverlight 4 builds further upon this. This book will enable .NET developers to get their finger on the pulse of data-driven business applications in Silverlight.This book is not a general Silverlight 3/4 overview book; it is uniquely aimed at developers who want to build data-driven applications. It focuses on showing .NET developers how to interact with, and handle multiple sources of data in Silverlight business applications, and how to solve particular data problems following a practical hands-on approach, using real-world recipes. It is a practical cookbook that teaches you how to build data-rich business applications with Silverlight that draw on multiple sources of data. Most of the covered features work both in Silverlight 3 and 4. However, we cover some that are specific for Silverlight 4, which will therefore not work with Silverlight 3. Where this is the case, it is clearly indicated.Packed with reusable, real-world recipes, the book begins by introducing you to general principles when programming Silverlight. It then dives deep into the world of data services, covering all the options available to access data and communicate with services to get the most out of data in your Silverlight business applications, whilst at the same time providing a rich user experience. Chapters cover data binding, data controls, the concepts of talking to services, communicating with WCF, ASMX and REST services, and much more.By following the practical recipes in this book, which are of varying difficulty levels, you will learn the concepts needed to create data-rich business applications—from the creation of a Silverlight application, to displaying data in the Silverlight application and upgrading your existing applications to use Silverlight. Each recipe covers a data services topic, going from the description of the problem, through a conceptual solution to a solution containing sample code.
Table of Contents (16 chapters)
Silverlight 4 Data and Services Cookbook
Credits
About the authors
About the reviewers
Preface

Creating a REST service from WCF


Using WCF Services, it is possible to expose a REST endpoint. This way, we can easily create a WCF services project in which some services have a SOAP endpoint while others have a REST endpoint.

In .NET 4.0, there are several ways to create REST services. In this example, we'll use a manual approach based on the WebHttpBinding. This process is quite simple: we need to make a configuration change to the configuration code of the service and apply an attribute on the service methods.

We'll create a small sample service project and expose the service as a REST endpoint. To do so, create a new empty ASP.NET web application in Visual Studio 2010 and name it OfficeSupplies. Within the created web application, add a new WCF service called TonerService. This triggers the creation of both the service interface, ITonerService.cs, and the service implementation, TonerService.cs.

To allow our service to communicate using REST, we'll first create a REST endpoint. In the web.config file, we'll start by adding the webHttp behavior:

<system.serviceModel>
<behaviors>
...
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>

Next, we can add a new endpoint that uses this behavior:

<services>
<service name="OfficeSupplies.TonerService">
<endpoint
address=""
behaviorConfiguration="webBehavior"
binding="webHttpBinding"
bindingConfiguration=""
contract="OfficeSupplies.ITonerService"/>
</service>
</services>
</system.serviceModel>

Finally, we have to apply an attribute, WebGetAttribute, on the service methods we want to expose over REST. Using the UriTemplate on this attribute, we can use the same endpoint for several service methods. In the following sample code snippet, we can see that IsTonerAvailable has its UriTemplate set to Toner:

[OperationContract]
[WebGet(UriTemplate = "toner",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Xml)]
bool IsTonerAvailable();

This service method can thus be accessed using http://localhost:123/TonerService.svc/Toner.

If we want to pass in a parameter, we can do so using a query expression. If we want to check the toner for a specific color, we can pass the color in using the following method:

[OperationContract]
[WebGet(UriTemplate = "toner/{color}",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Xml)]
bool IsColorAvailable(string color);

To invoke this service, we can perform a call to http://localhost:123/TonerService.svc/Toner/Red.