Http://www.cnblogs.com/2018/archive/2010/10/17/1853384.html previous
Describes the query syntax and examples.ProgramHow to use these published services? InCodeTo summarize the methods used to access these services.
Client Access
Query |
These queries can be used in combination with the preceding query syntax. Ø browser address: Enter the address and make the GET request directly. Ø javascipt Library: JS libraries that support JSON processing, such as extjs, dojo, and Ms Ajax Service reference Reference In common forms, IDE directly adds references and uses proxy objects and context processing. // Generic { Dataservicecontext CTX = new dataservicecontext (U ); VaR q = CTX. Execute <order> (New uri ("/orders (10402)", urikind. Relative ));
Foreach (VAR t in Q) { Console. writeline (T. orderdate ); }
} // Client proxy { Northwindentities CTX = new northwindentities (U ); VaR q = from C in CTX. Suppliers select C;
Foreach (VAR t in Q) { Console. writeline (T. City ); }
Support for HTTP Httpwebrequest Req = (httpwebrequest) webrequest. Create (URL + "/orders (10402 )"); Req. method = "get "; Req. Accept = "application/JSON "; Using (httpwebresponse resp = (httpwebresponse) Req. getresponse ())
{ Using (streamreader sr = new streamreader (resp. getresponsestream ())) { Console. writeline (Sr. readtoend ());
} } Req = (httpwebrequest) webrequest. Create (URL + "/orders (10402 )"); Req. method = "get ";
Req. Accept = "application/atom + XML "; Using (httpwebresponse resp = (httpwebresponse) Req. getresponse ()) { Using (streamreader sr = new streamreader (resp. getresponsestream ()))
{ Xmldocument xml = new xmldocument (); XML. Load (SR );
Console. writeline (XML. innerxml ); } }
|
Add, delete, and modify |
Ø HTTP format You can transfer parameters and request forms according to the msdn regulations, which may cause some trouble. Service reference Reference Common formats. For details, refer to the "modify data" section. To handle concurrency conflicts: Dataservicerequestexception · Entity Framework provider-in the data model,ConcurrencymodeAttribute of a property that is part of the concurrency token for an entity type is setFixed. · Reflection provider-The etagattribute is applied to the data class that is an entity type. This attribute declares the concurrency Token Based on the supplied property names. |
Modify data
Insert |
HTTP POST Content-Type: Set correctly |
Update |
Http put/merger |
Delete |
HTTP Delete |
Example
You need to modify the data object. Note that the server must set the correct attributes, for example:
Config. setentitysetaccessrule ("*", entitysetrights. All );
Config. setserviceoperationaccessrule ("*", serviceoperationrights. All );
Config. dataservicebehavior. maxprotocolversion = dataserviceprotocolversion. V2;
Client Access through proxy:
// Client proxy
{
Northwindentities CTX = new northwindentities (U );
VaR q = from C in CTX. Suppliers select C;
Foreach (VAR t in Q)
{
Console. writeline (T. City );
}
/// Add
VaR Cust = new customer ()
{
Customerid = "test1 ",
Address = "Beijing ",
City = "Peking ",
CompanyName = "Demo ",
Contactname = "test ",
Contacttitle = "mr .",
Country = "China ",
Fax = "123 ",
Phone = "111 ",
Postalcode = "456 ",
Region = "HD"
};
CTX. addtocustomers (Cust );
CTX. savechanges ();
/// Update
VaR upcust = (from C in CTX. Customers where c. customerid = Cust. customerid select c). firstordefault ();
Upcust. contactname + = "-UPD ";
CTX. updateobject (upcust );
CTX. savechanges ();
/// Delete
Upcust = (from C in CTX. Customers where c. customerid = Cust. customerid select c). firstordefault ();
CTX. deleteobject (upcust );
CTX. savechanges ();
It can be seen that the query can be performed using the LINQ syntax on the client.
In addition to clients, the server also has strong support.