不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
使用C#创建一个简单的Web服务
编辑:dnawo 日期:2008-11-04
Web服务提供了一种快捷调用远程服务器上函数并进行数据传输的方法。下边我们来创建一个简单的Web服务并在ASP.NET Web应用程序中进行调用。
步骤1:打开Microsoft Visual Studio 2008,新建一个Web服务;

步骤2:打开Service1.asmx.cs代码页,输入以下代码;
注意:凡是要可以远程调用的函数必须在前面加"[WebMethod]"约束;
步骤3:按Ctrl+F5运行代码,可看到如下页面:

上边列出了Web服务所有可供远程调用的函数,任意点击其中一个函数名即可进行测试。
步骤4:ASP.NET Web应用程序中调用Web服务;
在ASP.NET Web应用程序项目中点击右键,选择"添加 Web 引用...":

在弹出的对话框中输入Web服务访问地址,如下图示:

任意输入一个Web引用名后点击"添加引用"按钮即完成了对Web服务的引用,下边代码说明如何在程序中使用Web服务:
步骤1:打开Microsoft Visual Studio 2008,新建一个Web服务;

步骤2:打开Service1.asmx.cs代码页,输入以下代码;
复制内容到剪贴板
程序代码

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
namespace MzwuCom
{
/// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://www.mzwu.com/")]
[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 a, int b)
{
return a + b;
}
[WebMethod]
public DataSet GetCustomers()
{
using (SqlConnection conn = new SqlConnection("uid=sa;password=sa;database=Northwind;server=(local)"))
{
SqlDataAdapter da = new SqlDataAdapter("Select top 20 CompanyName,City,Address From Customers", conn);
DataSet ds = new DataSet();
da.Fill(ds, "Customers");
return ds;
}
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
namespace MzwuCom
{
/// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://www.mzwu.com/")]
[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 a, int b)
{
return a + b;
}
[WebMethod]
public DataSet GetCustomers()
{
using (SqlConnection conn = new SqlConnection("uid=sa;password=sa;database=Northwind;server=(local)"))
{
SqlDataAdapter da = new SqlDataAdapter("Select top 20 CompanyName,City,Address From Customers", conn);
DataSet ds = new DataSet();
da.Fill(ds, "Customers");
return ds;
}
}
}
}
注意:凡是要可以远程调用的函数必须在前面加"[WebMethod]"约束;
步骤3:按Ctrl+F5运行代码,可看到如下页面:

上边列出了Web服务所有可供远程调用的函数,任意点击其中一个函数名即可进行测试。
步骤4:ASP.NET Web应用程序中调用Web服务;
在ASP.NET Web应用程序项目中点击右键,选择"添加 Web 引用...":

在弹出的对话框中输入Web服务访问地址,如下图示:

任意输入一个Web引用名后点击"添加引用"按钮即完成了对Web服务的引用,下边代码说明如何在程序中使用Web服务:
复制内容到剪贴板
程序代码

//创建Web服务对象实例
MzwuCom.Service1 service = new MzwuCom.Service1();
//调用 Add() 方法
TextBox3.Text = service.Add(Convert.ToInt16(TextBox1.Text), Convert.ToInt16(TextBox2.Text)).ToString();
//调用 GetCustomers() 方法
GridView1.DataSource = service.GetCustomers().Tables["Customers"].DefaultView;
GridView1.DataBind();
MzwuCom.Service1 service = new MzwuCom.Service1();
//调用 Add() 方法
TextBox3.Text = service.Add(Convert.ToInt16(TextBox1.Text), Convert.ToInt16(TextBox2.Text)).ToString();
//调用 GetCustomers() 方法
GridView1.DataSource = service.GetCustomers().Tables["Customers"].DefaultView;
GridView1.DataBind();
评论: 0 | 引用: 0 | 查看次数: 6468
发表评论
请登录后再发表评论!