zoukankan      html  css  js  c++  java
  • 用记事本开始写自己的第一个WebService

    打开记事本键入如下代码将其保存为addService.asmx
    <%@ WebService Language="c#" Class="AddNumbers"%>
    using System;
    using System.Web.Services;
    public class AddNumbers : WebService
    {
    [WebMethod]
    public int Add(int a, int b){
    int sum;
    sum = a + b;
    return sum;
    }
    }


    第一行的<%@ WebService Language="c#" Class="AddNumbers"%> 指令表示使用C#
    创建一个WebService ,类名为 AddNumbers。
    System.Web.Services 命名空间中的类提供了对WebService 的支持。
    使用[WebMethod]属性声明的ADD方法使得它可以被远程的客户端使用。


    把放到网站目录下使用IIS浏览文件。既可以对addService进行测试,会显示WebService 的名称和可以使用的方法列表,单击方法链接打开的页面中有一个测试窗体。输入参数值单击调用按钮结果将以XML格式返回。

    可能遇到的问题:
    显示该页无法显示
    解决方法是打开 IIS-->web服务扩展。允许第4项(asp.net...)

    客户端调用方法
    Vs2008中新建网站-》右击项目名称选择添加web引用你的addService.asmx文件的URL复制过去指定命名空间假如是invokeAddService

    在需要调用的CS文件中键入引入刚刚添加“添加web引用”指定的命名空间后就可以想使用本地代码一样使用这个webservice中的类
    本例代码如下
    using System;
    using invokeAddService;
    protected void Page_Load(object sender, EventArgs e)
    {
    AddNumbers sum = new AddNumbers();
    int a = 10;
    int b = 5;
    int total = sum.Add(a,b);//total 等于15
    }

  • 相关阅读:
    Redis的数据结构
    Centos6.5安装Redis3.0备忘记录
    Centos6.5安装MySQL5.6备忘记录
    QueryRunner使用之可变条件的处理
    List集合遍历整理
    js获取select标签选中的值
    linuxC/C++面试问题总结整理
    live555学习(一)通读Makefile编译live555
    入园博客开篇
    springboot 整合 MongoDB 实现登录注册,html 页面获取后台参数的方法
  • 原文地址:https://www.cnblogs.com/rentj1/p/1421935.html
Copyright © 2011-2022 走看看