Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [460]

By Root 5520 0
the JSON calls that are typical of ASP.NET AJAX clients.

The following code snippet shows how to use the new WebGet attribute in the definition of a service contract:

[ServiceContract]

public interface ICalculator {

[OperationContract]

[WebGet]

long Add(long x, long y);

[OperationContract]

[WebGet(UriTemplate="Sub?p1={x}&p2={y}")]

long Subtract(long x, long y);

}

The WebGet attribute qualifies a method as a retrieval operation and enables callers to use the HTTP GET verb to invoke it. The WebGet attribute also features the UriTemplate property. You use this property to specify which URL format is accepted to invoke the method. If not otherwise specified via an explicit UriTemplate property, the URI template for a WebGet method like the aforementioned Add is the following:

theService.svc/Add?x=1&y=2

The service name is followed by the method name, and formal parameters follow in order, each with its own actual value. You can change this standard URI template by changing the method name and formal parameter names.

The WebInvoke attribute indicates that a given method has to be considered as a logical invoke operation that can be invoked using any HTTP verb, but typically the POST verb is called upon:

[ServiceContract]

public interface ICalculator {

[OperationContract]

[WebInvoke(Method="Post",

RequestFormat=WebMessageFormat.Xml,

ResponseFormat=WebMessageFormat.Json)]

long Add(long x, long y);

}

Through the WebInvoke attribute, you can set the URI template, the method to be used to invoke the method, as well as the format for the request and response text.

Note

If you choose to add to your Visual Studio project a new item known as an AJAX-enabled service, the wizard gets you a skeleton of code that’s ready to help you build an AJAX-enabled WCF service.

To be invoked from an AJAX client, a WCF service can be configured with a specific binding model—the webHttpBinding model. The webHttpBinding model is a basic HTTP binding except that it doesn’t use SOAP. The webHttpBinding binding model is specifically created for REST scenarios. Here’s an excerpt from a sample configuration script for an AJAX-enabled WCF service:

...

When webHttpBinding is turned on, you can also use the optional enableWebScript element, which enables the run time to generate the JavaScript proxy for the service. The proxy is a JavaScript class that makes it particularly easy to invoke the service endpoints. To invoke a service, a proxy is not strictly required, as you’ll see in the next chapter about jQuery. In addition, you might also want to publish service metadata for retrieval using an HTTP GET request.

The services hosted by the Web application must be specially configured to use the Web HTTP-specific binding model, as shown here:

...

behaviorConfiguration="metadataBehavior">

binding="webHttpBinding"

behaviorConfiguration="ajaxBehavior" />

The configuration of a WCF service specifies key pieces of information—the binding model (mandatory), contract, and behavior. The binding indicates how the call is going to happen—essentially whether it will use a REST approach, a SOAP approach, or both. The contract attribute indicates which contract the endpoint is exposing. If the service class implements a single contract type, the contract attribute can be omitted in the endpoint section. Finally, the behaviorConfiguration attribute contains the name of the behavior to be used in the endpoint.

Note

In some particular scenarios, you can also resort to a simplified configuration scheme for AJAX-enabled WCF services. In the service endpoint file

Return Main Page Previous Page Next Page

®Online Book Reader