You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在.NET控制台应用中调用本地ASMX Web服务

问题描述

我正在学习Web服务,从基础ASMX模板入手。已创建.NET控制台应用并添加Web引用,配置看似正常,但不清楚如何在控制台中调用该服务并使用其方法。

服务端代码(SoapDemo.asmx.cs)

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

namespace soapdemo
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.None)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

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

        [WebMethod]
        public int SumOfNums(int FirstNumber, int SecondNumber)
        {
            return FirstNumber + SecondNumber;
        }


        [WebMethod(MessageName = "SumOfFloats")]
        public float SumOfNums(float FirstNumber, float SecondNumber)
        {
            return FirstNumber + SecondNumber;
        }
    }
}

客户端代码(Program.cs,当前状态)

using SoapDemo;
namespace ConsoleClient3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            WebService1SoapClient client = new WebService1SoapClient();
            
            //var add = client.HelloWorldAsync();
            //Console.WriteLine(add);
            //Console.ReadLine();

        }
    }
}

已配置的连接服务

  • 控制台应用已成功添加SoapDemo服务引用,配置界面显示服务连接状态正常
  • 服务引用详细设置界面确认服务地址、绑定参数配置无误
解决方案

可以通过同步或异步两种方式调用ASMX服务的方法,以下是具体实现:

1. 同步调用方法

直接调用同步版本方法,等待结果返回后输出:

using SoapDemo;
namespace ConsoleClient3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using (WebService1SoapClient client = new WebService1SoapClient())
            {
                // 调用HelloWorld方法
                string helloResult = client.HelloWorld();
                Console.WriteLine("HelloWorld返回结果:" + helloResult);

                // 调用整数求和方法
                int sumIntResult = client.SumOfNums(10, 20);
                Console.WriteLine("整数求和结果:" + sumIntResult);

                // 调用浮点数求和方法(对应服务端MessageName设置,方法名为SumOfFloats)
                float sumFloatResult = client.SumOfFloats(1.5f, 2.3f);
                Console.WriteLine("浮点数求和结果:" + sumFloatResult);

                Console.ReadLine();
            }
        }
    }
}

2. 异步调用方法

若需异步执行,需使用await关键字,同时将Main方法改为异步:

using SoapDemo;
namespace ConsoleClient3
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            using (WebService1SoapClient client = new WebService1SoapClient())
            {
                // 异步调用HelloWorld方法
                string helloResult = await client.HelloWorldAsync();
                Console.WriteLine("HelloWorld返回结果:" + helloResult);

                // 异步调用整数求和方法
                int sumIntResult = await client.SumOfNumsAsync(10, 20);
                Console.WriteLine("整数求和结果:" + sumIntResult);

                // 异步调用浮点数求和方法
                float sumFloatResult = await client.SumOfFloatsAsync(1.5f, 2.3f);
                Console.WriteLine("浮点数求和结果:" + sumFloatResult);

                Console.ReadLine();
            }
        }
    }
}

注意事项

  • 用using语句包裹客户端实例,确保服务连接被正确释放
  • 浮点数求和方法调用名称为SumOfFloats,对应服务端MessageName设置,避免重载冲突
  • 异步调用时,Main方法必须标记为async Task,并通过await等待异步操作完成

内容的提问来源于stack exchange,提问作者Ali Kocak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 03:32:48