一个创建访问WebService的简单示例子

首先,创建Webservice服务应用

1.打开Visual 2005,新建项目

 

系统会自动生成代码,Web服务的类继承自System.Web.services.WebService类.此外,Web服务对外提供的方法均需有[WebMethod]属性标记,对于供内部调用,则不需要使用.在系统自动生成的代码中,已经提供一个返回字符串(Hello world)的方法,这里再添加了一个计算两个整数的和的方法.代码如下:

 

using  System;
using  System.Data;
using  System.Web;
using  System.Collections;
using  System.Web.Services;
using  System.Web.Services.Protocols;
using  System.ComponentModel;

namespace  WebService
... {
    
/**//// <summary>
    
/// Service1 的摘要说明
    
/// </summary>

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(
false)]
    
public class Service1 : System.Web.Services.WebService
    
...{

        [WebMethod]
        
public string HelloWorld()
        
...{
            
return "Hello World";
        }


        [WebMethod]
        
public int Add(int x,int y)
        
...{
            
return x + y;
        }

    }

}

保存并生成项目.这样,一个Web服务就创建完成.

2.发布Web服务

 创建好Web服务项目后,首先生成Web服务

输出Web服务

最后发布Web服务

注:.NET平台下的Web服务是创建在Web服务器IIS上的,因此,如果要在本机上创建和运行Web服务,必须事先安装IIS服务器,可以通过"添加/删除Windows组件"来安装IIS.

 3.调用Web服务

新建一个Windows应用程序,设计界面

添加要引用的Web服务

编写代码

 

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;
using  WinDemo.localhost;

namespace  WinDemo
... {
    
public partial class Form1 : Form
    
...{
        
public Form1()
        
...{
            InitializeComponent();
        }


        
private void Form1_Load(object sender, EventArgs e)
        
...{
            Service1 ser 
= new Service1();
            label1.Text 
= ser.HelloWorld();
            ser.Dispose();
        }


        
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        
...{
            
if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar))
            
...{
                e.Handled 
= true;
            }

        }


        
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        
...{
            
if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar))
            
...{
                e.Handled 
= true;
            }

        }


        
private void button1_Click(object sender, EventArgs e)
        
...{
            Service1 ser 
= new Service1();
            label3.Text 
= Convert.ToString(ser.Add(Convert.ToInt32(textBox1.Text.Trim()), Convert.ToInt32(textBox2.Text.Trim())));
            ser.Dispose();
        }


        
private void button2_Click(object sender, EventArgs e)
        
...{
            Application.Exit();
        }

    }

}

 

占华

http://www.cardprinterworld.com

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值