WCF 웹서비스를 이용한 데이터 바인딩
1 웹콘피그 설정
wsHttpBinding 를 basicHttpBinding 변경
<service behaviorConfiguration="TESTSliverlight.Web.Service1Behavior" name="TESTSliverlight.Web.Service1"> <endpoint address="" binding="basicHttpBinding" contract="TESTSliverlight.Web.IService1"> <!-- <endpoint address="" binding="wsHttpBinding" contract="TESTSliverlight.Web.IService1"> --> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> </system.serviceModel>
2. wcf 웹서비스로 전달할 클래스 만들기
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ServiceModel; using System.Runtime.Serialization; namespace TESTSliverlight.Web { [DataContract] public class WCFBook { public WCFBook(string BookTitle) { this.BookTitle = BookTitle; } [DataMember] public string BookTitle { get; set; } } }
3.웹서비스 만들기
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace TESTSliverlight.Web { // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config. public class Service1 : IService1 { public void DoWork() { } public List<WCFBook> GetBookData() { //하나의 값만 보내고 싶으면 return WCFBook 하면된다 //실버라이트에서 받을땐 e.result.BookTitle 이런씩으로 받으면 됩니다 List<WCFBook> li = new List<WCFBook>(); li.Add(new WCFBook("1111")); li.Add(new WCFBook("2222")); li.Add(new WCFBook("3333")); return li; } } }
실버라이트에서 처리하기
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.ServiceModel.Channels; using System.ServiceModel; using System.Collections.ObjectModel; namespace TESTSliverlight { public partial class DataList : UserControl { ServiceBook.Service1Client sclient; EndpointAddress defaultAddress; public DataList() { InitializeComponent(); Binding defaultBinding = new System.ServiceModel.BasicHttpBinding(); defaultAddress = new System.ServiceModel.EndpointAddress("http://localhost:62711/Service1.svc"); sclient = new TESTSliverlight.ServiceBook.Service1Client(defaultBinding, defaultAddress); sclient.GetBookDataCompleted += new EventHandler<TESTSliverlight.ServiceBook.GetBookDataCompletedEventArgs>(sclient_GetBookDataCompleted); sclient.GetBookDataAsync(); } void sclient_GetBookDataCompleted(object sender, TESTSliverlight.ServiceBook.GetBookDataCompletedEventArgs e) { this.ListWCF.ItemsSource = e.Result; //리스트 박스에 데이터 바인딩 } } }
-----------------------------------------------------------------------------------------------------------------------
<UserControl x:Class="TESTSliverlight.DataList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="440" Height="392" xmlns:TESTSliverlight="clr-namespace:TESTSliverlight"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:SB="clr-namespace:TESTSliverlight.ServiceBook">
<UserControl.Resources>
<SB:WCFBook x:Key="WCFBookDS" d:IsDataSource="True"/>
<DataTemplate x:Key="BookTitleTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=BookTitle}" />
<TextBlock Text="{Binding Path=BookTitle}" />
<TextBlock Text="{Binding Path=BookTitle}" />
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Height="48" HorizontalAlignment="Left" Margin="8,8,0,0" VerticalAlignment="Top" Width="192" Text="DATA LIST" TextWrapping="Wrap"/>
<ListBox Name="ListWCF" Margin="64,96,184,120" DataContext="{StaticResource WCFBookDS}"
ItemsSource="{Binding WCFBook}" ItemTemplate="{StaticResource BookTitleTemplate}"/>
</Grid>
</UserControl>