Silverlight和服务器端通信

 

与服务器端通信,首先我们用wcf,新建一个wcf的页面。

添加一个简单的求和的方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace SLDome.Web
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“ISLS”。
    [ServiceContract]
    public interface ISLS
    {
        [OperationContract]
        int Add(int number1, int number2);
    }
}

 

=============================================================================

然后我们再实现这个接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace SLDome.Web
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“SLS”。
    // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 SLS.svc 或 SLS.svc.cs,然后开始调试。
    public class SLS : ISLS
    {

        public int Add(int number1, int number2)
        {
            return number1 + number2;
        }
    }
}

 

=================================================================================

现在我们去设置下前台页面:

<UserControl x:Class="SLDome.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" Background="White">
        <TextBox HorizontalAlignment="Left" Height="23" Margin="118,58,0,0" TextWrapping="Wrap" Name="txtName" VerticalAlignment="Top" Width="120"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="118,113,0,0" TextWrapping="Wrap" Name="txtPwd" VerticalAlignment="Top" Width="120"/>
        <Button Content="WCF"  Name="btnWcf" HorizontalAlignment="Left" Margin="46,216,0,0" VerticalAlignment="Top" Width="75" Click="btnWcf_Click"/>
        <Button Content="一般处理程序" Name="btnHandler" HorizontalAlignment="Left" Margin="211,216,0,0" VerticalAlignment="Top" Width="110" Click="btnHandler_Click"/>

    </Grid>
</UserControl>

 

这样我们的wcf就写好了,下面是我们的一般处理程序的代码:

我们新建一个一般处理程序的页面,用来处理用户的登陆:

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

namespace SLDome.Web
{
    /// <summary>
    /// LoginHandler 的摘要说明
    /// </summary>
    public class LoginHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string name = context.Request["txtName"].ToString();
            string password = context.Request["txtPwd"].ToString();
            if (name=="admin")
            {
                if (password=="admin123")
                {
                    context.Response.Write("登陆成功!");

                }
                else
                {
                    context.Response.Write("密码错误!");
                }
            }
            else
            {
                context.Response.Write("用户名不存在!");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

================================================================================

然后我们处理下这个一般处理程序:

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;

namespace SLDome
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void btnWcf_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference1.SLSClient sc = new ServiceReference1.SLSClient();
            sc.AddAsync(10, 20);
            sc.AddCompleted += sc_AddCompleted;
        }

        void sc_AddCompleted(object sender, ServiceReference1.AddCompletedEventArgs e)
        {
            MessageBox.Show(e.Result.ToString());
        }

        private void btnHandler_Click(object sender, RoutedEventArgs e)
        {
            string name = txtName.Text.Trim();
            string password = txtPwd.Text.Trim();
            WebClient wc = new WebClient();

            wc.DownloadStringAsync(new Uri("http://localhost:7090/LoginHandler.ashx?txtName=" + name + "&txtPwd=" + password, UriKind.RelativeOrAbsolute));

            wc.DownloadStringCompleted += wc_DownloadStringCompleted;
           
        }

       

        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            MessageBox.Show(e.Result);
        }
    }
}

 

 


原文链接: http://blog.csdn.net/mypc2010/article/details/8073536

转载于:https://my.oschina.net/changpinghu/blog/92501

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值