Silverlight中调用Web Service

Silverlight应用程序中有一些基本功能适用于所有HTTP和HTTPS通信,如下:

1、始终允许同域调用。

2、如果正确配置了承载Web服务的Web服务器,则支持跨域调用。

3、所有通信都是异步的。

4、仅支持GET和POST调用。

5、支持大多数标准请求标头和所有的自定义请求标头,跨域请求的跨域策略文件中必须允许有标头。

6、只有"00确定"和"404未找到"状态码可以用。


废话少说,下面看一个实例:

1、新建一个Silverlight应用程序,选择.NET3.5



2、新建Book.cs业务类


Book.cs内容如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace 调用自定义的WebService示例.Web
{
    public class Book
    {
        public string Name { get; set; }
        public string Author { get; set; }
    }
}

3、右键 调用自定义的WebService示例.Web→添加→新建项→数据→SQL Server数据库:



数据表结构如下:



4、右键 调用自定义的WebService示例.Web→添加→新建项→Web→Web服务(BookServcie.asmx):



注释HelloWorld(),BookServcie.asmx代码如下:

[WebMethod]
 public List<Book> GetBooks()
        {
            return GetBook();
        }
        private List<Book> GetBook()
        {
            List<Book> books = new List<Book>();
            using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=你自己的项目路径\App_Data\Book.mdf;Integrated Security=True;User Instance=True"))
            {
                conn.Open();
                string sql="select * from BookList";
                using (SqlCommand cmd = new SqlCommand(sql,conn))
                {
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Book b = new Book();
                            b.Name=reader["Name"].ToString();
                            b.Author = reader["Author"].ToString();
                            books.Add(b);
                        }
                    }
                }
            }
            return books;
        }

5、右键 调用自定义的WebService示例.Web→属性→Web→选择特定端口:



6、右键 BookServcie.asmx→在浏览器中查看:



若能在浏览器中看到以上界面,则说明WebService可以成功访问,点击GetBooks



点击调用



可以访问到数据中的数据,以XML的形式返回。

7、接下来在Silverlight应用程序调用,右键 调用自定义的WebService示例→添加服务引用



第一步输入的地址是刚才访问BookService.asmx的地址


8、打开MainPage.xmal,代码如下:

<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="调用自定义的WebService示例.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded" Background="White">
        <sdk:DataGrid x:Name="myBooks" />
    </Grid>
</UserControl>
后台代码如下:

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 调用自定义的WebService示例.ServiceReference1;


namespace 调用自定义的WebService示例
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            ServiceReference1.BookServiceSoapClient client = new ServiceReference1.BookServiceSoapClient();
            //注册成功事件
            client.GetBooksCompleted += new EventHandler<ServiceReference1.GetBooksCompletedEventArgs>(client_GetBooksCompleted);
            client.GetBooksAsync();
        }

        void client_GetBooksCompleted(object sender, ServiceReference1.GetBooksCompletedEventArgs e)
        {
            //检测是否调用成功
            if (e.Error != null)
            {
                return;
            }
            myBooks.ItemsSource = e.Result;
        }
    }
}

9、ok,F5运行程序即可看到运行结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值