Silverlight调用Web Service的方法


先展示一下我的Silverlight项目

新建Web Service,写两个方法,一个带输入参数。

ExpandedBlockStart.gif 大气象
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Web.Services;

namespace  HCLoad.Web
{
    
///   <summary>
    
///  WebService1 的摘要说明
    
///   </summary>
    [WebService(Namespace  =   " http://tempuri.org/ " )]
    [WebServiceBinding(ConformsTo 
=  WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(
false )]
    
//  若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    
//  [System.Web.Script.Services.ScriptService]
     public   class  WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        
public   string  HelloWorld()
        {
            
return   " Hello World " ;
        }
        
// 带输入参数
        [WebMethod]
        
public   string  HelloWorld1( string  str)
        {
            
return   " Hello World "   +  str;
        }
    }
}

新建一个Silverlight用户控件,调用Web Service方法。

ExpandedBlockStart.gif 大气象
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  HCLoad
{
    
public   partial   class  uc_WebService : UserControl
    {
        
// 创建Web Service对象
        ServiceReference1.WebService1SoapClient ws  =   new  HCLoad.ServiceReference1.WebService1SoapClient();
        
public  uc_WebService()
        {
            InitializeComponent();
            
this .Loaded  +=   new  RoutedEventHandler(Page_Loaded);
        }
        
void  Page_Loaded( object  sender, RoutedEventArgs e)
        {
            AddEvent();
            Bind();
        }
        
// 注册事件,有点类似Ajax的回调
         private   void  AddEvent()
        {
            ws.HelloWorldCompleted 
+=   new  EventHandler < ServiceReference1.HelloWorldCompletedEventArgs > (ws_HelloWorldCompleted);
            ws.HelloWorld1Completed 
+=   new  EventHandler < ServiceReference1.HelloWorld1CompletedEventArgs > (ws_HelloWorld1Completed);
        }
        
// 回调函数
         void  ws_HelloWorldCompleted( object  sender, ServiceReference1.HelloWorldCompletedEventArgs e)
        {
            tbWeb.Text 
+=  e.Result  +   "  hi " ;
        }
        
void  ws_HelloWorld1Completed( object  sender, ServiceReference1.HelloWorld1CompletedEventArgs e)
        {
            tbWeb.Text 
+=  e.Result  +   "  hi " ;
        }
        
private   void  Bind()
        {
            ws.HelloWorldAsync();
            ws.HelloWorld1Async(
" 输入 " ); // 调用带输入参数的Web Service方法
        }
    }
}

 

一些小技巧:
1.右击Silverlight项目,添加服务引用,点发现,可以找到你创建的Web Service
2.当你修改了Web Service要重新生成一下,并且右击Silverlight的引用更新服务。
3.修改web serivce端口与你web项目端口一致,否则会提示跨域操作。
在本机上调试asp.net程序时,设置端口。
右击项目->属性->Web->使用 Visual Studio 开发服务器(D) 把自动分配端口,修改为特定端口,输入指定的端口。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值