C# WebService 测试(2)

76 篇文章 1 订阅

在测试(1)的代码 中增加其它的自定义Web方法,然后演示如何调用该方法。

1. 增加一个简单加法
[WebMethod\]

public int Sum(int a, int b)

{ 
	 return a + b;

}

运行Web Service ,该功能可以正常调用。

调用测试:

输出值正常。

2. 创建一个返回值为DataTable对象数据

`

DataTable dt = new DataTable();

[WebMethod\]

public string Get\_Products()

 {

     dt.Columns.Add("ProductName");

      dt.Columns.Add("Cost");

      dt.Rows.Add("Rluer", 200);

      dt.Rows.Add("Pen", 120);

      dt.Rows.Add("PenBox", 300);

      dt.Rows.Add("ColorPen", 210);

       return JsonConvert.SerializeObject(dt);

    }

`

需要将DataTable对象序列化为Json字符串。需要添加引用 Microsoft.AspNet.WebApi.client组件

添加引用后,后台代码:

using Newtonsoft.Json;
在这里插入图片描述

测试运行:
在这里插入图片描述

3. 创建一个App添加WebService的引用

添加服务引用
在这里插入图片描述

输入服务器地址,转到。可重新命名命名空间,也可使用默认名称。
在这里插入图片描述

添加Web引用

可默认名称,也可改变名称:
在这里插入图片描述

在这里插入图片描述

后台代码添加引用:

using Newtonsoft.Json;

using System.Data;

创建服务的实例:

localhost.MyWebService myService = new localhost.MyWebService();

在窗口加载事件中:

private void Window\_Loaded(object sender, RoutedEventArgs e)

    {

        var results = myService.Get\_Products();

        DataTable dt = JsonConvert.DeserializeObject<DataTable>(results);

        dg.ItemsSource = dt.DefaultView;

    }

效果如下:
在这里插入图片描述

使用Web Service 功能OK。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以为您提供关于 C# WebService 服务端的相关信息。C# WebService 服务端是一种基于 SOAP 协议的 Web 服务,它可以通过 HTTP 或 HTTPS 协议进行通信,实现跨平台的数据传输。下面是一个简单的 C# WebService 服务端的实现示例: 1. 创建一个 C# 类,并在类上添加 [WebService] 和 [WebServiceBinding] 属性,示例代码如下: ``` using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class MyWebService : System.Web.Services.WebService { // web service methods here } ``` 2. 在类中添加一个或多个 Web 方法,并使用 [WebMethod] 属性标识它们。示例代码如下: ``` [WebMethod] public string HelloWorld(string name) { return "Hello, " + name + "!"; } ``` 3. 在 Web 服务描述语言 (WSDL) 文件中定义 Web 服务的接口和方法。可以使用 Visual Studio 自带的 WSDL 生成工具或手动创建。示例代码如下: ``` <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tempuri.org/" targetNamespace="http://tempuri.org/"> <wsdl:message name="HelloWorldSoapIn"> <wsdl:part name="name" type="xsd:string"/> </wsdl:message> <wsdl:message name="HelloWorldSoapOut"> <wsdl:part name="return" type="xsd:string"/> </wsdl:message> <wsdl:portType name="MyWebServiceSoap"> <wsdl:operation name="HelloWorld"> <wsdl:input message="tns:HelloWorldSoapIn"/> <wsdl:output message="tns:HelloWorldSoapOut"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="MyWebServiceSoap" type="tns:MyWebServiceSoap"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="HelloWorld"> <soap:operation soapAction="http://tempuri.org/HelloWorld"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="MyWebService"> <wsdl:port name="MyWebServiceSoap" binding="tns:MyWebServiceSoap"> <soap:address location="http://localhost/MyWebService.asmx"/> </wsdl:port> </wsdl:service> </wsdl:definitions> ``` 4. 将该类部署到 IIS 或自承载的 ASP.NET 运行时中。在浏览器中访问 Web 服务的 URL(如 http://localhost/MyWebService.asmx),即可看到 Web 服务的描述信息和测试界面。 希望这些信息能够对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flysh05

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值