The Web Programming Model provides the basic framework elements required to build Web-style services with Windows Communication Foundation (WCF). Web-style services are designed to be accessed by the widest range of possible clients (including Web browsers with no additional client framework) and have the following unique requirements:
-
URIs and URI Processing. URIs play a central role in the design of Web-style services. The WCF Web Programming model uses the UriTemplate and UriTemplateTable classes to provide URI processing capabilities.
-
Support for GET and POST operations. Web-style services make use of the GET verb for data retrieval, in addition to various invoke verbs for data modification and remote invocation. The Web Programming Model uses the WebGetAttribute and WebInvokeAttribute to associate service operations with both GET and POST verbs.
-
Multiple data formats. Web-style services process many kinds of data in addition to SOAP messages. The Web Programming Model uses the WebHttpBinding and WebHttpBehavior to support many different data formats including XML documents, JSON data object, and streams of binary content such as images, video files, or plain text.
The Web Programming Model extends the reach of WCF to cover Web-style scenarios that include REST services, AJAX and JSON services, and Syndication (ATOM/RSS) feeds. For more information about AJAX and JSON services, see AJAX Integration and JSON Support. For more information about Syndication, see WCF Syndication Overview.
URI Processing with UriTemplate and UriTemplateTable
URI templates provide an efficient syntax for expressing large sets of structurally similar URIs. For example, the following template expresses the set of all three-segment URIs that begin with "a" and end with "c" without regard to the value of the intermediate segment: a/{segment}/c
This template describes URIs like the following:
-
a/x/c
-
a/y/c
-
a/z/c
-
and so on.
In this template, the curly brace notation ("{segment}") indicates a variable segment instead of a literal value.
.NET Framework 3.5 provides a new API for working with URI templates called UriTemplate. UriTemplates allow you to do the following:
-
You can call one of the Bind methods with a set of parameters to produce a fully-closed URI that matches the template. This means all variables within the URI template are replaced with actual values.
-
You can call Match() with a candidate URI, which uses a template to break up a candidate URI into its constituent parts and returns a dictionary that contains the different parts of the URI labeled according to the variables in the template.
- Bind() and Match() are inverses so that you can call Match( Bind( x ) ) and come back with the same environment you started with.
There are many times (especially on the server, where dispatching a request to a service operation based on the URI is necessary) that you want to keep track of a set of UriTemplate objects in a data structure that can independently address each of the contained templates. UriTemplateTable represents a set of URI templates and selects the best match given a set of templates and a candidate URI. This is not affiliated with any particular networking stack (WCF included) so you can use it wherever necessary.
The WCF Service Model makes use of UriTemplate and UriTemplateTable to associate service operations with a set of URIs described by a UriTemplate. A service operation is associated with a UriTemplate, using either the WebGetAttribute or the WebInvokeAttribute.
Service Operation Parameters and URLs
Web-style services can be called from a Web browser by typing a URL that is associated with a service operation. These service operations may take parameters which must be specified in a string form within the URL. The following table shows the types that can be passed within a URL and the format used.
|
Type
|
Format
|
| Byte | 0 - 255 |
| SByte | -128 - 127 |
| Int16 | -32768 - 32767 |
| Int32 | -2,147,483,648 - 2,147,483,647 |
| Int64 | -9,223,372,036,854,775,808 - 9,223,372,036,854,775,807 |
| UInt16 | 0 - 65535 |
| UInt32 | 0 - 4,294,967,295 |
| UInt64 | 0 - 18,446,744,073,709,551,615 |
| Single | -3.402823e38 - 3.402823e38 |
| Double | -1.79769313486232e308 - 1.79769313486232e308 |
| Char | Any single character |
| Decimal | Any decimal in standard notation (no exponent) |
| Boolean | True or False (case insensitive) |
| String | Any string |
| DateTime | MM/DD/YYYY MM/DD/YYYY HH:MM:SS [AM|PM] Month Day Year Month Day Year HH:MM:SS [AM|PM] |
| TimeSpan | DD.HH:MM:SS Where DD = Days, HH = Hours, MM = minutes, SS = Seconds |
| Guid | A GUID, for example: 936DA01F-9ABD-4d9d-80C7-02AF85C822A8 |
| DateTimeOffset | MM/DD/YYYY HH:MM:SS MM:SS Where DD = Days, HH = Hours, MM = minutes, SS = Seconds |
| Enums | The enumeration value for example, defining the enumeration as shown in the following code. public enum Days{ Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; Any of the individual enumeration values (or their corresponding integer values) may be specified in the query string. |
| Types that have a TypeConverterAttribute that can convert the type to and from a string representation. | Depends on the Type Converter. |
WebGet and WebInvoke
Web-style services make use of retrieval verbs (for example HTTP GET) in addition to various invoke verbs (for example HTTP POST, the verb used by SOAP services). The Web Programming Model allows service developers to control the both the URI template and verb associated with their service operations with the WebGetAttribute and WebInvokeAttribute. The WebGetAttribute and the WebInvokeAttribute allow you to control how individual operations get bound to URIs and the HTTP methods associated with those URIs. For example, adding WebGetAttribute and WebInvokeAttribute in the following code.
[ServiceContract]
interface ICustomer
{
//"View It"
[OperationContract]
[WebGet]
Customer GetCustomer():
//"Do It"
[OperationContract]
[WebInvoke]
Customer UpdateCustomerName( string id,
string newName );
} The preceding code allows you to make the following HTTP requests.
GET /GetCustomer
POST /UpdateCustomerName
WebInvokeAttribute defaults to POST but you can use it for other verbs too.
[ServiceContract]
interface ICustomer
{
//"View It“ -> HTTP GET
[OperationContract]
[WebGet( UriTemplate=“customers/{id}” )]
Customer GetCustomer( string id ):
//"Do It“ -> HTTP PUT
[OperationContract]
[WebInvoke( UriTemplate=“customers/{id}”, Method=“PUT” )]
Customer UpdateCustomer( string id, Customer newCustomer );
} Formats and the Web Programming Model
The Web Programming Model has new features to work with many different data formats. At the binding layer, the WebHttpBinding can read and write the following different kinds of data:
-
XML
-
JSON
-
Opaque binary streams
This means the Web Programming Model can handle any type of data but, you may be programming against Stream.
.NET Framework 3.5 provides support for JSON data (AJAX) as well as Syndication feeds (including ATOM and RSS). For more information about these features, see WCF Syndication Overview and AJAX Integration and JSON Support.
See Also
© 2007 Microsoft Corporation. All rights reserved.