共用方式為


Using a Data Service in a Client Application (ADO.NET Data Services)

You can access ADO.NET Data Services resources by supplying a URI to a Web browser. The URI provides the address of a resource, and request messages are sent to these addresses to access or change the underlying data that the resource represents. The browser issues an HTTP GET command and returns the requested resource as an AtomPub feed. For more information, see Accessing the Service from a Web Browser (ADO.NET Data Services Quickstart).

Although a Web browser may be useful for testing that a data service resource returns the expected data, production data services that can also create, update, and delete data are generally accessed by application code or scripting languages in a Web page. This topic provides an overview of how data services are accessed in an interoperable manner from an HTTP client.

Accessing and Changing Data Using REST Semantics

ADO.NET Data Services uses standard Internet protocols to ensure interoperability with any applications that can generate HTTP requests and consume HTTP responses. Applications access and change data in a data service by sending request messages of a specific HTTP action and with a URI that addresses an entity resource against which the action should be performed. When entity data must be supplied, it is supplied as a properly encoded payload in the body of the message.

HTTP Actions

ADO.NET Data Services supports the following HTTP actions to perform create, read, update, and delete operations on the entity data that the addressed resource represents:

  • HTTP GET - This is the default action when a resource is accessed from a browser. No payload is supplied in the request message, and a response method with a payload that contains the requested data is returned.

  • HTTP POST - Inserts new entity data into the supplied resource. Data to be inserted is supplied in the payload of the request message. The payload of the response message contains the data for the newly created entity, including any autogenerated key values. The header also contains the URI that addresses the new entity resource.

  • HTTP DELETE - Deletes the entity data that the specified resource represents. A payload is not present in the request or response messages.

  • HTTP PUT - Replaces existing entity data at the requested resource by first deleting the entity and then replacing it with new data that is supplied in the payload of the request message.

  • HTTP MERGE - Because of inefficiencies in executing a delete followed by an insert in the data source just to change entity data, ADO.NET Data Services introduces a new HTTP MERGE action. The payload of the request message contains the properties that must be changed on the addressed entity resource. Because HTTP MERGE is not defined in the HTTP specification, it may require additional processing to route a HTTP MERGE request through non-ADO.NET Data Services aware servers

For more information, see Making Changes to Data (ADO.NET Data Services).

Payload Formats

For an HTTP PUT, HTTP POST, or HTTP MERGE request, the payload of a request message contains the entity data that you send to the data service. The contents of the payload depend on the data format of the message. The HTTP responses to all actions except DELETE also contain such a payload. ADO.NET Data Services supports the following payload formats for accessing and changing data with the service:

  • AtomPub - An XML-based message encoding that is defined in the Atom Publishing Protocol (Atom) to enable data exchange over HTTP for Web feeds, podcasts, wikis, and XML-based Internet functionality. ADO.NET Data Services defines a fixed mapping between entities in the data model and the Atom elements. For more information, see Atom Format (ADO.NET Data Services).

  • JSON - JavaScript Object Notation (JSON) is a lightweight data interchange format that is based on a subset of the JavaScript Programming Language. For more information, see JSON Format (ADO.NET Data Services).

The message format of the payload is requested in the header of the HTTP request message. For more information, see Payload Formats (ADO.NET Data Services).

Accessing and Changing Data Using Client Libraries

ADO.NET Data Services includes client libraries that enable you to more easily access data services from .NET Framework and Silverlight-based client applications. These libraries simplify sending and receiving HTTP messages. They also translate the message payload into CLR objects that represent entity data. The client libraries feature the two core classes DataServiceContext and DataServiceQuery. These classes, which provide functionality that is similar to the ObjectContext and ObjectQuery<T> classes in the ADO.NET Entity Framework, enable you to query a data service and then work with the returned entity data as CLR objects. For more information, see Using a Data Service in a .NET Framework Application (ADO.NET Data Services).

You can use the Add Service Reference dialog in Visual Studio to add a reference to a data service. This tool requests the service metadata from a referenced data service and generates the DataServiceContext that represents a data service, as well as generates the client data service classes that represent entities. For more information, see Generating the Data Service Client Library (ADO.NET Data Services).

See Also

Other Resources

Accessing Data Service Resources (ADO.NET Data Services)

Quickstart (ADO.NET Data Services)