다음을 통해 공유


방법: 서비스 작업 정의(WCF Data Services)

WCF Data Services 는 서버에서 서비스 작업으로 정의된 메서드를 노출합니다. 데이터 서비스는 서비스 작업을 사용하여 URI를 통해 서버에 정의된 메서드에 액세스할 수 있도록 합니다. 서비스 작업을 정의하려면 [WebGet] 또는 [WebInvoke] 특성을 메서드에 적용합니다. 쿼리 연산자를 지원하려면 서비스 작업에서 IQueryable 인스턴스를 반환해야 합니다. 서비스 작업은 DataServiceCurrentDataSource 속성을 통해 기본 데이터 소스에 액세스할 수 있습니다. 자세한 내용은 서비스 작업(WCF Data Services)을 참조하십시오.

이 항목의 예제에서는 Orders의 필터링된 IQueryable 인스턴스 및 관련 Order_Details 개체를 반환하는 GetOrdersByCity라는 서비스 작업을 정의합니다. 이 예제에서는 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 서비스 작업을 쿼리하려면

  • 웹 브라우저에서 다음 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

예제

다음 예제에서는 Northwind 데이터 서비스에 GetOrderByCity라는 서비스 작업을 구현합니다. 이 작업은 ADO.NET Entity Framework를 사용하여 제공된 도시 이름을 기준으로 Orders 집합 및 관련 Order_Details 개체를 IQueryable 인스턴스로 반환합니다.

Dd744841.note(ko-kr,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 정의