It corresponds to queries that can be directly used on the client, and has many enhanced functions on the server.
Service Operations
Release some business logic processing by yourself
Service operations enable you to expose business logic in a data service, such as to implement validation logic, to apply role-based security, or to expose specialized querying capabilities.
For specific requirements such as return values and parameters, refer to msdn to define these operations as long as they comply with this requirement.
Example
Implement custom condition Query
· Http: // localhost: 12345/northwind. svc/getordersbycity? City = 'London'
· Http: // localhost: 12345/northwind. svc/getordersbycity? City = 'London '& $ Top = 2
[Webget]
Public iqueryable <order> getordersbycity (string City)
{
If (string. isnullorempty (city ))
{
Throw new argumentnullexception ("city ",
"You must provide a value for the parameter 'city '.");
}
// Get the objectcontext that is the data source for the service.
Northwindentities context = This. currentdatasource;
Try
{
VaR selectedorders = from order in context. Orders. Include ("order_details ")
Where order. Customer. City = City
Select order;
Return selectedorders;
}
Catch (exception ex)
{
Throw new applicationexception ("an error occured: {0}", ex );
}
}
Interceptors
Intercept the request, so that you can add your own business processing logic. Currently, the system has two predefined values:
Queryinterceptor: restricted use of the queried object data range
Changeinterceptor: used to modify an object
WCF data services enables an application to intercept request messages so that you can add custom logic to an operation. you can use this custom logic to validate data in incoming messages. you can also use it to further restrict the scope of a query request, such as to insert a custom Authorization Policy on a per request basis.
Interception is specified med by specially attributed methods in the data service. these methods are called by WCF data services at the appropriate point in message processing. interceptors are defined on a per-entity set basis, and interceptor methods cannot accept parameters from the request like service operations can.
Query interceptorMethods, which are called when processing an http get request, must return a Lambda expression that determines whether an instance of the interceptor's entity set shoshould be returned by the query results. this expression is used by the data service to further refine the requested operation.
Example
[Queryinterceptor ("orders")]
Public Expression <func <order, bool> onqueryorders ()
{
// Filter the returned orders to only orders // that belong to a customer that is the current user. Return o => O. Customer. contactname =
Httpcontext. Current. User. Identity. Name;
}
[Changeinterceptor ("Products")]
Public void onchangeproducts (product, updateoperations operations)
{
If (Operations = updateoperations. Add |
Operations = updateoperations. Change)
{
// Reject changes to discontinued products. If (product. discontinued)
{
Throw new dataserviceexception (400,
"A discontinued product cannot be modified ");
}
}
Else if (Operations = updateoperations. delete)
{
// Block the delete and instead set the discontinued flag.
Throw new dataserviceexception (400,
"Products cannot be deleted; instead set the discontinued flag to 'true '");
}
}
Data Service Provider
In applications, we generally use entity framework provider [edmx]. You can generate a model from a database or create a database from a model.
Because of the powerful functions of the provider model, we can also implement a provider by ourselves. The following example shows an example of class implementation:
Namespace dataservices
{
[Dataservicekey ("name")]
Public class inductee
{
Public string name {Get; set ;}
Public bool Group {Get; set ;}
Public int yearinducted {Get; set ;}
Public list <song> songs {Get; set ;}
Public static list <inductee> makeinducteelist ()
{
Return (new list <inductee> ()
{
New inductee ()
{
Name = "Rolling Stones ",
Group = false,
Yearinducted = 1990,
Songs = new list <song> ()
},
New inductee ()
{
Name = "Beatles ",
Group = false,
Yearinducted = 1986,
Songs = new list <song> ()
}
});
}
}
[Dataservicekey ("songtitle")]
Public class song
{
Public String songtitle {Get; set ;}
Public static list <song> makesonglist ()
{
Return (new list <song> ()
{
New Song () {songtitle = "Satisfaction "},
New Song () {songtitle = "All you need is love "},
});
}
}
Public class assigninducteestosongs
{
Public static void assign (list <inductee> inductee, list <song> songs)
{
Inductee [0]. Songs. Add (songs [0]);
Inductee [1]. Songs. Add (songs [1]);
}
}
/// <Summary>
/// Summary description for mydatamodel
/// </Summary>
Public class mydatamodel
{
Static list <inductee> inductees;
Static list <song> songs;
Static mydatamodel ()
{
Inductees = inductee. makeinducteelist ();
Songs = song. makesonglist ();
Assigninducteestosongs. Assign (inductees, songs );
}
Public iqueryable <inductee> inductees
{
Get
{
Return inductees. asqueryable ();
}
}
Public iqueryable <song> songs
{
Get
{
Return songs. asqueryable ();
}
}
}
// Service
[System. servicemodel. servicebehavior (includeexceptiondetailinfaults = true)]
Public class demoob: dataservice <mydatamodel>
{
Public static void initializeservice (dataserviceconfiguration config)
{
Config. setentitysetaccessrule ("*", entitysetrights. allread );
Config. setserviceoperationaccessrule ("*", serviceoperationrights. All );
Config. dataservicebehavior. maxprotocolversion = dataserviceprotocolversion. V2;
}
}
After the service is released, you can use the "WCF Data Services Query" method and client query.
This example can be downloaded in http://dskit.codeplex.com
Hosting
Generally, the service is hosted in IIS, And the httphandler with the SVC extension IIS can be processed.
However, you may need to host the service on your own. In this case, you can use the WCF technology.
Using system. servicemodel. Web;
Webservicehost host = new webservicehost (typeof (sampledataservice ));
Host. open ();
You only need to set the other ABC related to WCF in APP. config.