VS2010中C#创建webservice

本文介绍了在VS2010中如何创建C# Web Service的详细步骤,包括创建ASP.NET空Web应用程序,添加Web服务,编写服务方法,并展示了如何在另一个Web Form项目中调用该服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在VS2010中不能直接创建ASP.NET Web 服务应用程序

但是可以利用以下方法创建:

1.打开vs2010,文件--新建--项目---Visuio C#---web---ASP.NET 空Web应用程序,起名为TestWebService,确定

2.右击项目名称,---添加新项--Visual C#---web 服务--起名为MyService.asmx,点击确定

3.右击MyService.asmx---查看代码---添加两个方法,获取名字和年龄

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
///WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class MyService : System.Web.Services.WebService {

    public MyService () {

        //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();
    }

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

    [WebMethod]//必须要有的,为了说明,其下是一个方法,每一个方法前面都需要有。
    public string getName()
    {
        return "Hope";
    }

    [WebMethod]
    public string getAge()
    {
        return "25";
    }
    
}

3.右击MyService.asmx,选择“在浏览器中查看”,即可检查本项目是否有语法错误.可以点击 getAge,getName获取值。

接下来写一下如何调用这个service.

在VS2010中,新建一个项目--web Form 应用程序。名字为:WindowsFormsApplication1

右击项目名称---添加服务引用。


添加引用之后,项目列表中会显示。


在窗口中拖入控件:


代码如下:

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

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

        private void button1_Click(object sender, EventArgs e)
        {

            localhost.MyService obj = new localhost.MyService();
            textBox1.Text = obj.getAge();
        }
    }
}


运行winform项目:执行结果如下:


完整的服务端及客户端调用程序,在win7+ vs2015环境运行通过. 一、说明 1、创建winfrom应用程序;(或者是控制台项目) 2、在项目中添加一个WCF服务,并实现服务; 3、在需要启动WebService服务的地方启动该服务即可; 二、代码如下: 1、新建一个WCF服务——定义服务接口    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]     public interface ICalculator     {         [OperationContract]         double Add(double n1, double n2);     } 2、新建一个WCF服务——实现服务 public class CalculatorService : ICalculator     {         public double Add(double n1, double n2)         {             return n1 + n2;         }     } 3、添加完WcF服务后会在应用程序配置文件中有入下节点                             <!--TestServer.ICalculator服务定义的接口,根据自己定义进行修改-->                                                                   <baseAddresses> <!--这个是要发布的服务地址,可以进行修改-->                                   </baseAddresses>                   4、在要启动服务的地方启动服务监听   public frmMain() { InitializeComponent(); } private void frmMain_Load(object sender, EventArgs e) { try { //打开服务创建监听,并开始监听消息 ServiceHost serviceHost = new ServiceHost(typeof(Service1));//需要using System.ServiceModel; serviceHost.Open(); label1.Text = "服务启动正常"; } catch (Exception ex) { label1.Text = ex.Message; } } 5、下面可以在客户端通过上面的服务地址”http://xxx.xxx.xxx.xx:8733/test/Service1/“对服务进行调用 到这步就实现在控制台中实现webService的发布。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值