지연된 콘텐츠 로드(WCF Data Services)
http://msdn.microsoft.com/ko-kr/library/ee358709.aspx
실버라이트에서의 사용법 : http://msdn.microsoft.com/ko-kr/blogvisualizer/ee681614
여기서는 WCF가 기본적으로 한번에 받을 수 있는 양을 제한 하기 때문에 페이징을 이용해서 데이터를 가져옵니다.
(예를 들어 한번에 1만건의 데이터를 가지고 올수없으니 100개씩 쪼개서 로컬에서 합칩니다)
프로젝트 구성
edmx 는 NorthwindEntities를 구성하였습니다.
Model1.edmx
WcfDataService1.svc
SetEntitySetPageSize : http://msdn.microsoft.com/ko-kr/library/system.data.services.dataserviceconfiguration.setentitysetpagesize.aspx
namespace WcfService1
{
public class WcfDataService1 : DataService<NorthwindEntities>
{
// 이 메서드는 서비스 전반적인 정책을 초기화하는 데 한 번만 호출됩니다.
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: 규칙을 설정하여 어떤 엔터티 집합과 서비스 작업을 표시할 수 있고 업데이트할 수 있는지 등을 표시합니다.
// 예제:
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.SetEntitySetPageSize("Customers", 8); //Customers 데이터를 8개씩 끊어서 가져옵니다.
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
}
}
페이징으로 불러오기
MainWindow.xaml.cs
namespace WebServicePager
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ServiceReference1.NorthwindEntities context = new ServiceReference1.NorthwindEntities(new Uri("http://localhost:50764/WcfDataService1.svc"));
//일반적으로 가져오는 방식
//var query = from c in ne.Customers
// select c;
//this.dataGrid1.ItemsSource = query.ToList();
//여기서부터는 페이징을 통해서 가져오는방법
List<ServiceReference1.Customers> pagerCustomer = new List<ServiceReference1.Customers>();
DataServiceQueryContinuation<ServiceReference1.Customers> token = null;
int pageCount = 0;
try
{
QueryOperationResponse<ServiceReference1.Customers> response =
context.Customers.Execute() as QueryOperationResponse<ServiceReference1.Customers>;
do
{
Console.WriteLine("Page {0}:", pageCount++);
if (token != null)
{
response = context.Execute<ServiceReference1.Customers>(token)
as QueryOperationResponse<ServiceReference1.Customers>;
}
pagerCustomer.AddRange(response.ToList());
}
while ((token = response.GetContinuation()) != null);
this.dataGrid1.ItemsSource = pagerCustomer;
}
catch (DataServiceQueryException ex)
{
throw new ApplicationException(
"An error occurred during query execution.", ex);
}
}
}
}좀더 쉽게 사용하기 제가 만든거기때문에 최적화 안됨( 발빼기 ㅋ)
//여기서부터는 페이징을 통해서 가져오는방법 PagerService.NorthwindEntities __ContextPager = new PagerService.NorthwindEntities(new Uri("http://localhost:56323/PagerWcfDataService.svc")); DataServicePager<PagerService.Categories> pager = new DataServicePager<PagerService.Categories>(this.__ContextPager); List<PagerService.Categories> list = pager.List(); this.dataGrid1.ItemsSource = list; this.label1.Content = list.Count.ToString();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Services.Client;
namespace 테스트용
{
///
/// WCF 서비스에서 config.SetEntitySetPageSize("Customers", 10);
/// 설정되있을때 페이징으로 가지고 오는 메소드 입니다.
///
/// WCF DataSerive애 속해있는 테이블 명
public class DataServicePager where T : class
{
private string __Table { set; get; }
private Type __Type;
DataServiceQueryContinuation token = null;
int pageCount = 0;
private DataServiceContext __DataServiceContext;
public DataServicePager(DataServiceContext dataServiceContext)
{
this.__Type = typeof(T);
this.__DataServiceContext = dataServiceContext;
}
public List List()
{
List __PagerObjectList = new List();
try
{
Uri uri = new Uri(string.Format("{0}/{1}", __DataServiceContext.BaseUri.ToString(), this.__Type.Name));
QueryOperationResponse response = __DataServiceContext.Execute(uri) as QueryOperationResponse;
do
{
Console.WriteLine("Page {0}:", pageCount++);
if (token != null)
{
response = __DataServiceContext.Execute(token)
as QueryOperationResponse;
}
__PagerObjectList.AddRange(response.ToList());
}
while ((token = response.GetContinuation()) != null);
}
catch (Exception ex)
{
throw new Exception("Ge tData ServiceList Error :" + ex.Message);
}
return __PagerObjectList.ToList();
}
}
}
'C#.NET 웹서비스' 카테고리의 다른 글
| WCF 대용량 파일업로드 (0) | 2013.12.09 |
|---|---|
| [웹서비스 및 웹사이트 디버깅 , system.diagnostics] (0) | 2012.03.12 |
| [ AspNetCompatibilityRequirements ] 서비스가 ASP.NET 호환성을 지원하지 않으므로 서비스를 활성화할 수 없습니다. (0) | 2011.02.23 |
| [DataService UploadFileCompleted Result ] 비동기 업로드 xml 결과값 받기 (0) | 2011.01.26 |
| WCF DataService IIS 실행 (세팅) 안될때 확인 해볼 사항 (0) | 2011.01.25 |
| WCF Data Services 퀵 스타트 // IIS Data Services 세팅 (0) | 2011.01.25 |
| [MSDN]How to: Add, Modify, and Delete Entities (WCF Data Services) (0) | 2010.10.25 |
| MSDN [WCF Data Services/DataSvcUtil.exe ]수동으로 클라이언트 데이터 서비스 클래스 생성 (0) | 2010.10.20 |
| 메시지를 역직렬화하는 동안 포맷터에서 예외가 발생했습니다. (0) | 2010.07.22 |
| [ServiceThrottlingBehavior] 서비스 성능을 조정할 수 있는 런타임 처리량 설정을 구성 (0) | 2010.05.20 |