共用方式為


How to: Define a Service Operation (WCF Data Services)

Important

WCF Data Services has been deprecated and will no longer be available for download from the Microsoft Download Center. WCF Data Services supported earlier versions of the Microsoft OData (V1-V3) protocol only and has not been under active development. OData V1-V3 has been superseded by OData V4, which is an industry standard published by OASIS and ratified by ISO. OData V4 is supported through the OData V4 compliant core libraries available at Microsoft.OData.Core. Support documentation is available at OData.Net, and the OData V4 service libraries are available at Microsoft.AspNetCore.OData.

RESTier is the successor to WCF Data Services. RESTier helps you bootstrap a standardized, queryable, HTTP-based REST interface in minutes. Like WCF Data Services before it, Restier provides simple and straightforward ways to shape queries and intercept submissions before and after they hit the database. And like Web API + OData, you still have the flexibility to add your own custom queries and actions with techniques you're already familiar with.

WCF Data Services expose methods that are defined on the server as service operations. Service operations allow a data service to provide access through a URI to a method that is defined on the server. To define a service operation, apply the [WebGet] or [WebInvoke] attribute to the method. To support query operators, the service operation must return an IQueryable<T> instance. Service operations may access the underlying data source through the CurrentDataSource property on the DataService<T>. For more information, see Service Operations.

The example in this topic defines a service operation named GetOrdersByCity that returns a filtered IQueryable<T> instance of Orders and related Order_Details objects. The example accesses the ObjectContext instance that is the data source for the Northwind sample data service. This service is created when you complete the WCF Data Services quickstart.

To define a service operation in the Northwind data service

  1. In the Northwind data service project, open the Northwind.svc file.

  2. In the Northwind class, define a service operation method named GetOrdersByCity as follows:

    [WebGet]
    public IQueryable<Order> GetOrdersByCity(string city)
    
    <WebGet()> _
    Public Function GetOrdersByCity(ByVal city As String) As IQueryable(Of Order)
    
  3. In the InitializeService method of the Northwind class, add the following code to enable access to the service operation:

    config.SetServiceOperationAccessRule(
        "GetOrdersByCity", ServiceOperationRights.AllRead);
    
    config.SetServiceOperationAccessRule( _
        "GetOrdersByCity", ServiceOperationRights.AllRead)
    

To query the GetOrdersByCity service operation

  • In a Web browser, enter one of the following URIs to invoke the service operation that is defined in the following example:

    • http://localhost:12345/Northwind.svc/GetOrdersByCity?city='London'

    • http://localhost:12345/Northwind.svc/GetOrdersByCity?city='London'&$top=2

    • http://localhost:12345/Northwind.svc/GetOrdersByCity?city='London'&$expand=Order_Details&$orderby=RequiredDate desc

Example

The following example implements a service operation named GetOrderByCity on the Northwind data service. This operation uses the ADO.NET Entity Framework to return a set of Orders and related Order_Details objects as an IQueryable<T> instance based on the provided city name.

Note

Query operators are supported on this service operation endpoint because the method returns an IQueryable<T> instance.

[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(string.Format(
            "An error occurred: {0}", ex.Message));
    }
}
<WebGet()> _
Public Function GetOrdersByCity(ByVal city As String) As IQueryable(Of Order)
    If String.IsNullOrEmpty(city) Then
        Throw New ArgumentNullException("city", _
            "You must provide a value for the parameter'city'.")
    End If

    ' Get the ObjectContext that is the data source for the service.
    Dim context As NorthwindEntities = Me.CurrentDataSource

    Try
        Dim selectedOrders = From order In context.Orders.Include("Order_Details") _
                             Where order.Customer.City = city _
                             Select order
        Return selectedOrders
    Catch ex As Exception
        Throw New ApplicationException("An error occurred: {0}", ex)
    End Try
End Function

See also