您的位置:知识库 » .NET技术

一步一步学Silverlight :数据与通信之WCF

作者: TerryLee  来源: 博客园  发布时间: 2008-10-09 11:45  阅读: 10886 次  推荐: 0   原文链接   [收藏]  

定义服务契约:

[ServiceContract]
public interface IBlog
{
    [OperationContract]
    Post[] GetPosts();
}

实现服务,这里可以是从数据库或者其他数据源读取,为了演示方便,我们直接初始化一个集合:

public class Blog : IBlog
{
    public Post[] GetPosts()
    {
        List<Post> posts = new List<Post>()
        {
            new Post(1,"一步一步学Silverlight 2系列(13):数据与通信之WebRequest","TerryLee"),
            new Post(2,"一步一步学Silverlight 2系列(12):数据与通信之WebClient","TerryLee"),
            new Post(3,"一步一步学Silverlight 2系列(11):数据绑定","TerryLee"),
            new Post(4,"一步一步学Silverlight 2系列(10):使用用户控件","TerryLee"),
            new Post(5,"一步一步学Silverlight 2系列(9):使用控件模板","TerryLee"),
            new Post(6,"一步一步学Silverlight 2系列(8):使用样式封装控件观感","TerryLee")
        };

        return posts.ToArray();
    }
}

修改Web.config中的服务配置,这里使用basicHttpBinding绑定,并且开启httpGetEnabled,以便后面我们可以在浏览器中查看服务:

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="TerryLee.SilverlightDemo27Web.BlogBehavior">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <services>
        <service behaviorConfiguration="TerryLee.SilverlightDemo27Web.BlogBehavior"
            name="TerryLee.SilverlightDemo27Web.Blog">
            <endpoint address="" binding="basicHttpBinding" contract="TerryLee.SilverlightDemo27Web.IBlog">
            </endpoint>
        </service>
    </services>
</system.serviceModel>

设置一下Web应用程序的端口号为固定端口52424,在浏览器中输入http://localhost:52424/Blog.svc,看看服务是否正常:

TerryLee_Silverlight2_0064

好了,现在服务端我们就实现完成了。现在编写界面展示部分,XAML如下:

<Grid Background="#46461F">
    <Grid.RowDefinitions>
        <RowDefinition Height="40"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Border Grid.Row="0" Grid.Column="0" CornerRadius="15"
            Width="240" Height="36" Background="Orange"
            Margin="20 0 0 0" HorizontalAlignment="Left">
        <TextBlock Text="最新随笔" Foreground="White"
                   HorizontalAlignment="Left" VerticalAlignment="Center"
                   Margin="20 0 0 0"></TextBlock>
    </Border>
    <ListBox x:Name="Posts" Grid.Row="1" Margin="40 10 10 10">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Id}" Height="40" Foreground="Red"></TextBlock>
                    <TextBlock Text="{Binding Title}" Height="40"></TextBlock>
                    <TextBlock Text="{Binding Author}" Height="40" Foreground="Orange"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

0
0
 

.NET技术热门文章

    .NET技术最新文章

      最新新闻

        热门新闻