一步一步学Silverlight 2系列(15):数据与通信之ASMX

概述

Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython,对JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步学Silverlight 2系列》文章将从Silverlight 2基础知识、数据与通信、自定义控件、动画、图形图像等几个方面带您快速进入Silverlight 2开发。

本文将简单介绍在Silverlight 2中如何与ASMX进行通信。

简单示例

本文的示例非常简单,其过程也跟我们在一步一步学Silverlight 2系列(14):数据与通信之WCF中差不多,我们仍然显示一个最新随笔的列表,最终完成后效果如下所示:

TerryLee_Silverlight2_0065

定义一个业务实体Post。

public class Post
{
    public int Id { get; set; }

    public string Title { get; set; }

    public string Author { get; set; }
}

在Web项目中添加一个Web Service文件,命名为BlogService.asmx

TerryLee_Silverlight2_0068

实现该服务,定义一个GetPosts方法:

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

        return posts.ToArray();
    }
}

同样设置Web Development Server的端口号为一个固定值,这里设为8081,然后在浏览器中测试服务是否正确:

TerryLee_Silverlight2_0069

点击调用后测试服务正确

TerryLee_Silverlight2_0070

在Silverlight项目中,添加对服务引用,

TerryLee_Silverlight2_0071

使用对象浏览器查看一下生成客户端代理类中的对象:

TerryLee_Silverlight2_0072

编写展示界面,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>

实现调用ASMX,并进行数据的绑定。仍然采用异步模式,所使用的方法如上图中红色框中的部分。过程与WCF通信差不多,只不过不再需要指定Bingding等信息:

public partial class Page : UserControl
{
    public Page()
    {
        InitializeComponent();
    }

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        BlogServiceSoapClient client = new BlogServiceSoapClient();
        client.GetPostsCompleted += new EventHandler<GetPostsCompletedEventArgs>(client_GetPostsCompleted);
        client.GetPostsAsync();
    }

    void client_GetPostsCompleted(object sender, GetPostsCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            Posts.ItemsSource = e.Result;
        }
    }
}

一个完整的Silverlight 2中调用ASMX的示例就完成了,运行后效果如下:

TerryLee_Silverlight2_0065

结束语

本文简单介绍了在Silverlight 2中如何调用ASMX,你可以从这里下载示例代码。

下一篇:一步一步学Silverlight 2系列(16):数据与通信之JSON

Tag标签: Silverlight

posted on 2008-03-10 18:24 TerryLee 阅读(3898) 评论(16)  编辑 收藏 所属分类: Silverlight

评论

#1楼  2008-03-11 07:38 生鱼片      

有了wcf,webservice用的就应该不多了吧   回复  引用  查看    

#2楼 [楼主] 2008-03-11 08:36 TerryLee      

@生鱼片
视使用情况而定吧,肯定还会有人使用的:)   回复  引用  查看    

#3楼  2008-03-15 19:30 ptymsx1 [未注册用户]

请问webservices哪些返回类型可以在sliverlight中使用?我发现返回的dataset无法使用   回复  引用  查看    

#4楼 [楼主] 2008-03-15 22:29 TerryLee      

@ptymsx1
Silverlight是客户端技术,不能直接访问数据,DataSet也不会支持,但支持一些泛型的集合   回复  引用  查看    

#5楼  2008-03-24 13:36 luojing [未注册用户]

TerryLee你好,我在调用远程webservice的时候为什么得不到返回的数据,或许在silverlight2.0中不能访问远程的webservice,下面是我的部分代码:
private UserService.UserServiceSoapClient client;
private void Button_Click(object sender, RoutedEventArgs e)
{

BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://10.9.225.65/ASNSService/UserService.asmx");
client = new UserServiceSoapClient(binding,address);
client.UserLogonCompleted += new EventHandler<UserLogonCompletedEventArgs>(client_UserLogonCompleted);
client.UserLogonAsync(passId.Text,password.Text);

}

void client_UserLogonCompleted(object sender, UserLogonCompletedEventArgs e)
{

if (e.Error == null)
{
string[] mystr = e.Result.ToArray();
System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("http://www.google.com"));
}
}
谢谢!   回复  引用  查看    

#6楼 [楼主] 2008-03-24 21:24 TerryLee      

@luojing
调用远程Web Service没有任何问题,可以参考后面一篇关于Live Search的实例   回复  引用  查看    

#7楼  2008-03-26 19:14 ptymsx1 [未注册用户]

TerryLee你好,我在练手测试下面这个asmx时,遇到了一个很奇怪的问题,代码跟你的基本一样。也可以执行,但是在Async时报错
http://www.wopos.com/webservice/chengyu.asmx?op=ChengYuDianGu
错误消息


[Async_ExceptionOccurred]
Arguments:
Debugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=2.0.30226.2&File=System.dll&Key=Async_ExceptionOccurred

这是我在网上查到别人的同样经历
http://topic.csdn.net/u/20080317/20/08ff4fa0-f0c1-4e61-b0ed-b06242bda663.html

请问怎么解决这个问题,它的原因是什么?   回复  引用  查看    


标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-03-10 18:30 编辑过
 
历史上的今天:

另存  打印
最新IT新闻:
· 阿里巴巴确立未来十年战略规划 修改自身定位
· 微软高管:Wii用户最终会成为Xbox 360用户
· 遵守YouTube案裁定 谷歌将陷入隐私指控深渊
· iPhone入华在即 中国手机产业生存面临考验
· 阿里巴巴集团再向淘宝注资20亿元
 


导航

公告

  • 网名:TerryLee
  • 本名:李会军
  • 位置:中国北京 Ethos
  • 联系方式:
  • 个人主页

 MVP配置

 版权声明

  • 本站采用创作共用许可 署名,非商业

绿色通道

IT新闻

统计

与我联系

留言簿(311)

我的标签

随笔分类

随笔档案

个人站点

关注项目

好的网站

我的好友

友情博客

搜索

积分与排名

阅读排行榜

评论排行榜