使用C#创建webservice及调用方式

 

使用C#创建webservice及调用方式

 

微软.NET战略的一个比较重要的部分就是webservice,利用webservice我们可以创建真正有效的分布式应用程序。
   
下面,我们对webservice做一些说明。
   
假设A是客户端,Bwebservice服务端,用户通过http协议向服务器发送soap请求,webservice返回客户端XML格式的数据。
   
现在我们看一看创建一个webservice的大致过程:
服务端的webservice是必须要建的。中间的soap,xml我们不用去关心,在客户端这边,比较重要的是如何从webservice取得对象?答案是用的是proxy对象。客户端由代理对象(proxy)负责与webservice的通信。所以在客户端使用webservice,完全和使用一个本地对象是一样的。

一、我们现在以一个简单的实例来说明如何使用C#创建webservice
1)打开vs.net,新建工程(asp.net  web服务),在位置中键入http://localhost/webserver,其中webserver就是工程的名字。确定后,出现一个Service1.asmx.cx,双击,出现代码窗口

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.Web;           //后加的

using System.Web.Services;  //后加的

 

namespace WebService

{

     /// <summary>

     /// Service1 的摘要说明。

     /// </summary>

     (1)

     public class Service1 : System.Web.Services.WebService

     {

         public Service1()

         {

              //CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的

              InitializeComponent();

         }

 

         #region 组件设计器生成的代码

        

         //Web 服务设计器所必需的

         private IContainer components = null;

                  

         /// <summary>

         /// 设计器支持所需的方法 - 不要使用代码编辑器修改

         /// 此方法的内容。

         /// </summary>

         private void InitializeComponent()

         {

         }

 

         /// <summary>

         /// 清理所有正在使用的资源。

         /// </summary>

         protected override void Dispose( bool disposing )

         {

              if(disposing && components != null)

              {

                   components.Dispose();

              }

              base.Dispose(disposing);        

         }

        

         #endregion

 

         // WEB 服务示例

         // HelloWorld() 示例服务返回字符串 Hello World

         // 若要生成,请取消注释下列行,然后保存并生成项目

         // 若要测试此 Web 服务,请按 F5

 

         [WebMethod]

         public string HelloWorld()

         {

              return "Hello World";

         }

 

         [WebMethod]

         public  string  show(string  yourname)

         {

              return  "http://localhost/webserver"+"欢迎"+yourname;

          }

 

     }

}


2下面在(1)处加入
[WebService(Namespace="http://localhost/webserver/")]
这是因为soap是基于http协议上的,客户端无法知道webservice位于那个服务器上。在实际应用中,比如http://www.ourfly.com上放置这个webservice,则Namespace改为http://www.ourfly.com/webserver.
3)下面我们给这个webservice添加一个方法。
// [WebMethod]
// public  string  HelloWorld()
// {
// return  "Hello  World";
// }
微软帮我们写好了一个,接着添加一个方法。方法名称叫show.
[WebMethod]

public  string  show(string  yourname)

{

     return  "http://localhost/webserver"+"欢迎"+yourname;

}  
4)现在,就可以运行了,按F5点击“show”,输入你的名字,比如”zhangmin”,然后点击“调用”

<?xml  version="1.0"  encoding="utf-8"  ?>  
    <string  xmlns="http://tempuri.org/">http://localhost/webserver欢迎zhangmin</string>


成功了。打开bin目录,Vs.net已经将proxy做好了webserver.dll

二、现在我们在不同的环境下测试:
1
打开vs.net,新建”windows应用程序工程,命名为Client,增加按钮,文本框。
1)现在要用到代理了,右键单击右边的reference(引用),选择添加引用”,选择浏览,找到webserver目录下的bin目录下的webserver.dll ,再加入一个system.web.webservices的引用,在列表中有。


2)在form1.cs里,加入
using  System.Web.Services;
using  webserver;

3)然后在
private  System.Windows.Forms.Button  button1;
private  System.Windows.Forms.TextBox  textBox1;
后面,插入
private  webserver.service1  Client
以建立一个service1的实例。


4)双击按钮,代码如下:


private void button1_Click(object sender, System.EventArgs e)

{

     Client  =new  Service1();

     string  name;

     name=Client.show("龙卷风.NET");

     textBox1.Text=name;

}


5)按F5,运行工程,点击按钮,文本框中显示
http://localhost/webserver欢迎龙卷风.NET
2
Asp.NET  web窗口的测试
方法与上面的一模一样,添加引用,建立service1的实例
在此不在细说。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值