C#WebService接口编写与调用

参看资料来源:http://www.cnblogs.com/nianyuwen/archive/2012/05/15/2500932.html

释义:

Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术。是:通过SOAP在Web上提供的软件服务,使用WSDL文件进行说明,并通过UDDI进行注册。简单的理解就是:webservice就是放在服务器上的函数,所有人都可以调用,然后返回信息。

1.新建一个项目,右击项目-->添加新项-->WEB服务(asmx)

双击打开asmx文件,编写如下代码:

2.运行以上页面,运行结果如下:

3.新建项目或者网站,为项目或者网站添加WEB服务应用,(注:这时需要运行服务,不然找不到服务)如图:

 

 

4:新建页面调用服务,如图:

编写后台代码如下:

 protected void Button1_Click(object sender, EventArgs e)
        {

            localhost.WebService1 a = new localhost.WebService1();
            Label1.Text = Convert.ToString(a.GetSum(Convert.ToInt32(TextBox1.Text.Trim()), Convert.ToInt32(TextBox2.Text.Trim())));
        }

运行结果:

注:WebService1必须运行着,若是关闭了服务(不运行),在调用接口时运行会报错

 



  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# WebService 接口开发教程 1. 创建 WebService 应用程序 在 Visual Studio 中,选择“新建项目”,选择“ASP.NET Web 服务应用程序”,输入项目名称,点击“确定”按钮。 2. 添加 WebService 接口 在 Solution Explorer 中,右键单击项目名称,选择“添加新项”,选择“Web 服务”,输入 WebService 名称,点击“添加”按钮。 3. 编写 WebService 接口代码 在 WebService 文件中,编写 WebService 接口代码。例如: ``` [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] public class MyWebService : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } } ``` 4. 调试 WebService 接口 在 Visual Studio 中,点击“调试”菜单,选择“启动调试”,浏览器将打开 WebService 的测试页面。 5. 调用 WebService 接口 在客户端应用程序中,添加 WebService 引用。例如: ``` using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using MyWebService; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { MyWebService.MyWebService ws = new MyWebService.MyWebService(); string result = ws.HelloWorld(); Response.Write(result); } } ``` 以上就是 C# WebService 接口开发的基本流程。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值