次の方法で共有


方法: サービス操作を定義する (WCF Data Services)

WCF Data Services は、サーバー上でサービス操作として定義されたメソッドを公開します。 サービス操作では、データ サービスを使用して、サーバー上で定義されているメソッドに URI を介してアクセスできます。 サービス操作を定義するには、[WebGet] 属性または [WebInvoke] 属性をメソッドに適用します。 クエリ演算子をサポートするには、サービス操作は、IQueryable インスタンスを返す必要があります。 サービス操作は、DataServiceCurrentDataSource プロパティを介して、基になるデータ ソースにアクセスできます。 詳細については、「サービス操作 (WCF Data Services)」を参照してください。

このトピックの例では、GetOrdersByCity という名前のサービス操作を定義します。このサービス操作は、Orders オブジェクトおよび関連する Order_Details オブジェクトのフィルターされた IQueryable インスタンスを返します。 この例は、Northwind サンプル データ サービスのデータ ソースである ObjectContext インスタンスにアクセスします。 このサービスは、WCF Data Services クイック スタートを完了したときに作成されます。

Northwind データ サービスのサービス操作を定義するには

  1. Northwind データ サービス プロジェクトで Northwind.svc ファイルを開きます。

  2. Northwind クラスで、次に示すように GetOrdersByCity というサービス操作メソッドを定義します。

    <WebGet()> _
    Public Function GetOrdersByCity(ByVal city As String) As IQueryable(Of Order)
    
    [WebGet]
    public IQueryable<Order> GetOrdersByCity(string city)
    
  3. Northwind クラスの InitializeService メソッドで次のコードを追加して、サービス操作へのアクセスを有効にします。

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

GetOrdersByCity サービス操作をクエリするには

  • Web ブラウザーで次のいずれかの URI を入力して、次の例で定義されているサービス操作を呼び出します。

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

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

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

次の例は、GetOrderByCity という名前のサービス操作を Northwind データ サービスに実装します。 この操作は、ADO.NET Entity Framework を使用して、Orders オブジェクトのセットおよび関連する Order_Details オブジェクトを、指定した都市名に基づく IQueryable インスタンスとして返します。

Dd744841.note(ja-jp,VS.100).gif注 :
メソッドは IQueryable インスタンスを返すので、クエリ操作は、このサービス操作エンドポイントでサポートされています。

<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
[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 occurred: {0}", ex);
    }
}

参照

その他のリソース

WCF Data Services の定義