Share via


지연된 콘텐츠 로드(WCF Data Services)

WCF Data Services 는 기본적으로 쿼리에서 반환되는 데이터 양을 제한합니다. 그러나 필요한 경우 데이터 서비스에서 관련 엔터티, 페이징 응답 데이터 및 이진 데이터 스트림을 포함한 추가 데이터를 명시적으로 로드할 수 있습니다. 이 항목에서는 이러한 지연된 콘텐츠를 응용 프로그램에 로드하는 방법을 설명합니다.

관련 엔터티

쿼리를 실행하면 주소가 지정된 엔터티 집합의 엔터티만 반환됩니다. 예를 들어, Northwind 데이터 서비스에 대한 쿼리가 Customers 엔터티를 반환하는 경우 CustomersOrders 간에 관계가 있어도 기본적으로 관련 Orders 엔터티가 반환되지 않습니다. 또한 데이터 서비스에서 페이징을 사용하도록 설정한 경우 서비스에서 이후 데이터 페이지를 명시적으로 로드해야 합니다. 관련 엔터티를 로드하는 방법에는 다음 두 가지가 있습니다.

  • 즉시 로드: $expand 쿼리 옵션을 사용하여 연결에 의해 쿼리가 요청한 엔터티 집합에 관련된 엔터티를 쿼리에서 반환하도록 요청할 수 있습니다. 데이터 서비스에 전송되는 쿼리에 $expand 옵션을 추가하려면 DataServiceQueryExpand 메서드를 사용합니다. 다음 예제와 같이 관련 엔터티 집합을 쉼표로 구분하여 여러 개 요청할 수 있습니다. 쿼리에서 요청한 모든 엔터티가 하나의 응답으로 반환됩니다. 다음 예제에서는 Orders 엔터티 집합과 함께 Order_DetailsCustomers를 반환합니다.

    ' Define a query for orders that also returns items and customers.
    Dim query As DataServiceQuery(Of Order) = _
    context.Orders.Expand("Order_Details,Customer")
    
    // Define a query for orders that also returns items and customers.
    DataServiceQuery<Order> query =
        context.Orders.Expand("Order_Details,Customer");
    

    WCF Data Services 에서는 $expand 쿼리 옵션을 사용하여 단일 쿼리에 포함할 수 있는 엔터티 집합의 수를 12개로 제한합니다.

  • 명시적 로드: DataServiceContext 인스턴스의 LoadProperty 메서드를 호출하여 관련 엔터티를 명시적으로 로드할 수 있습니다. LoadProperty 메서드를 호출할 때마다 데이터 서비스에 대해 별도의 요청이 만들어집니다. 다음 예제에서는 Orders 엔터티에 대한 Order_Details를 명시적으로 로드합니다.

    ' Explicitly load the order details for each order.
    context.LoadProperty(order, "Order_Details")
    
    // Explicitly load the order details for each order.
    context.LoadProperty(order, "Order_Details");
    

사용할 옵션을 고려할 때는 데이터 서비스에 대한 요청 수와 하나의 응답으로 반환되는 데이터 양이 서로 상쇄되는 관계임을 염두에 두어야 합니다. 응용 프로그램에 연결된 개체가 필요하지만 이러한 개체의 명시적 검색 요청에서 추가적인 지연이 발생하는 것을 피하려는 경우 즉시 로드를 사용합니다. 그러나 응용 프로그램에 특정 관련 엔터티 인스턴스에 대한 데이터만 필요한 경우 LoadProperty 메서드를 호출하여 이러한 엔터티를 명시적으로 로드하는 것을 고려해야 합니다. 자세한 내용은 방법: 관련 엔터티 로드(WCF Data Services)를 참조하십시오.

페이징 콘텐츠

데이터 서비스에서 페이징을 사용하도록 설정한 경우 데이터 서비스가 반환하는 피드의 항목 수는 데이터 서비스의 구성에 의해 제한됩니다. 페이지 제한은 각 엔터티 집합에 대해 별도로 설정할 수 있습니다. 자세한 내용은 데이터 서비스 구성(WCF Data Services)을 참조하십시오. 페이징을 사용하도록 설정하면 피드의 최종 항목에 다음 데이터 페이지에 대한 링크가 포함됩니다. 이 링크는 DataServiceQueryContinuation 개체에 포함됩니다. DataServiceQuery를 실행할 때 반환되는 QueryOperationResponseGetContinuation 메서드를 호출하여 다음 데이터 페이지의 URI를 가져옵니다. 반환된 DataServiceQueryContinuation 개체는 다음 결과 페이지를 로드하는 데 사용됩니다. GetContinuation 메서드를 호출하기 전에 쿼리 결과를 열거해야 합니다. do…while 루프를 사용하여 쿼리 결과를 먼저 열거한 후 다음 non-null 링크 값을 확인하는 것이 좋습니다. GetContinuation 메서드에서 null(Visual Basic에서는 Nothing)을 반환하면 원래 쿼리에 대한 추가 결과 페이지가 없습니다. 다음 예제에서는 Northwind 샘플 데이터 서비스에서 페이징 고객 데이터를 로드하는 do…while 루프를 보여 줍니다.

' With a paged response from the service, use a do...while loop 
' to enumerate the results before getting the next link.
Do
    ' Write the page number.
    Console.WriteLine("Page {0}:", pageCount + 1)

    ' If nextLink is not null, then there is a new page to load.
    If token IsNot Nothing Then
        ' Load the new page from the next link URI.
        response = CType(context.Execute(Of Customer)(token),  _
        QueryOperationResponse(Of Customer))
    End If

    ' Enumerate the customers in the response.
    For Each customer As Customer In response
        Console.WriteLine("\tCustomer Name: {0}", customer.CompanyName)
    Next

    ' Get the next link, and continue while there is a next link.
    token = response.GetContinuation()
Loop While token IsNot Nothing
// With a paged response from the service, use a do...while loop 
// to enumerate the results before getting the next link.
do
{
    // Write the page number.
    Console.WriteLine("Page {0}:", pageCount++);

    // If nextLink is not null, then there is a new page to load.
    if (token != null)
    {
        // Load the new page from the next link URI.
        response = context.Execute<Customer>(token)
            as QueryOperationResponse<Customer>;
    }

    // Enumerate the customers in the response.
    foreach (Customer customer in response)
    {
        Console.WriteLine("\tCustomer Name: {0}", customer.CompanyName);
    }
}

// Get the next link, and continue while there is a next link.
while ((token = response.GetContinuation()) != null);

쿼리에서 관련 엔터티가 요청된 엔터티 집합과 함께 단일 응답으로 반환되도록 요청하는 경우 페이징 제한이 응답에 인라인으로 포함된 중첩된 피드에 영향을 줄 수 있습니다. 예를 들어, Northwind 샘플 데이터 서비스에 Customers 엔터티 집합에 대한 페이징 제한이 설정되어 있는 경우 Northwind 샘플 데이터 서비스를 정의하는 Northwind.svc.cs 파일에서 다음 예제와 같이 관련 Orders 엔터티 집합에 대해 독립적인 페이징 제한을 설정할 수도 있습니다.

' Set page size defaults for the data service.
config.SetEntitySetPageSize("Orders", 20)
config.SetEntitySetPageSize("Order_Details", 50)
config.SetEntitySetPageSize("Products", 50)

' Paging requires v2 of the OData protocol.
config.DataServiceBehavior.MaxProtocolVersion = _
    System.Data.Services.Common.DataServiceProtocolVersion.V2
// Set page size defaults for the data service.
config.SetEntitySetPageSize("Orders", 20);
config.SetEntitySetPageSize("Order_Details", 50);
config.SetEntitySetPageSize("Products", 50);

// Paging requires v2 of the OData protocol.
config.DataServiceBehavior.MaxProtocolVersion =
    System.Data.Services.Common.DataServiceProtocolVersion.V2;

이 경우 최상위 Customers와 중첩된 Orders 엔터티 피드에 대해 모두 페이징을 구현해야 합니다. 다음 예제에서는 선택한 Customers 엔터티와 관련된 Orders 엔터티의 페이지를 로드하는 데 사용되는 while 루프를 보여 줍니다.

While nextOrdersLink IsNot Nothing
    For Each o As Order In c.Orders
        ' Print out the orders.
        Console.WriteLine("\t\tOrderID: {0} - Freight: ${1}", _
                o.OrderID, o.Freight)
    Next
    ' Load the next page of Orders.
    Dim ordersResponse = _
    context.LoadProperty(c, "Orders", nextOrdersLink)
    nextOrdersLink = ordersResponse.GetContinuation()
End While
while (nextOrdersLink != null)
{
    foreach (Order o in c.Orders)
    {
        // Print out the orders.
        Console.WriteLine("\t\tOrderID: {0} - Freight: ${1}",
            o.OrderID, o.Freight);
    }

    // Load the next page of Orders.
    var ordersResponse = context.LoadProperty(c, "Orders", nextOrdersLink);
    nextOrdersLink = ordersResponse.GetContinuation();
}

자세한 내용은 방법: 페이징 결과 로드(WCF Data Services)를 참조하십시오.

이진 데이터 스트림

WCF Data Services 를 사용하면 BLOB(Binary Large Object) 데이터를 데이터 스트림으로 액세스할 수 있습니다. 스트리밍을 사용하면 필요할 때까지 이진 데이터 로드가 지연되며, 클라이언트가 보다 효율적으로 이 데이터를 처리할 수 있습니다. 이 기능을 사용하려면 데이터 서비스에서 IDataServiceStreamProvider 공급자를 구현해야 합니다. 자세한 내용은 스트리밍 공급자(WCF Data Services)를 참조하십시오. 스트리밍을 사용하도록 설정하면 엔터티 형식이 관련 이진 데이터 없이 반환됩니다. 이 경우 DataServiceContext 클래스의 GetReadStream 메서드를 사용하여 서비스에서 이진 데이터의 데이터 스트림에 액세스해야 합니다. 이와 유사하게, SetSaveStream 메서드를 사용하여 엔터티의 이진 데이터를 스트림으로 추가하거나 변경할 수 있습니다. 자세한 내용은 이진 데이터로 작업(WCF Data Services)을 참조하십시오.

참고 항목

개념

데이터 서비스 쿼리(WCF Data Services)

기타 리소스

WCF Data Services 클라이언트 라이브러리