ASP.NET AJAX 客户端 访问 Web Service(5)

本文详细介绍了如何通过Web服务技术改变客户端访问方法名及使用HTTPGET访问方法,包括使用WebServiceAttribute指定方法名、改变方法名、使用ScriptMethodAttribute标记方法并设置UseHttpGet属性,以及实现方法之间的区分与性能优化。

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

 

改变客户端访问时的方法名

  • 客户端无法重载方法(可以通过判断arguments数量来模拟)
  • 如果服务器端出现了方法重载?

                使用WebServiceAttribute指定客户端方法名

                使用和真正的WebService相同的做法

                [WebMethod(MessageName="…")]

  • 并非出现重载才能改变方法名称

        一个改变客户端访问时的方法名的示例

首先创建一个名为MethodOverloadService.asmx的WebService

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

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

    [WebMethod]
    public int GetRandom()
    {
        return new Random(DateTime.Now.Millisecond).Next();
    }

    [WebMethod]
    public int GetRandom(int minValue, int maxValue)
    {
        return new Random(DateTime.Now.Millisecond).Next(minValue, maxValue);
    }
}

     然后我们创建一个页面,引用这个WebService,并设置ScriptManager的InlineScript="true",这样生成的代理就直接写到页面上了,我们可以看到,页面中只注册下面的一个GetRandom方法,因为第一个方法已经被覆盖

      如果我们要避免这种客户端对同名方法的覆盖,我们就要改变客户端访问这个方法时的名字,只需要在任意一个这样的方法下面加上如下代码就可以实现了

[WebMethod(MessageName = "GetRangeRandom")]

这时我们就可以在页面中找到它注册了两个方法 ,方法名分别是GetRandom和GetRangeRandom,好了,成功啦

 

使用HTTP GET访问WebService方法

  • 使用ScriptMethodAttribute进行标记(UseHttpGet属性设置为true),出于安全性考虑,默认只使用POST
  • 客户端使用代理的方法没有任何变化
  • 参数将使用Query String进行传递
  • 性能较HTTP POST方法略有提高
  • 一些特性略有改变(缓存的基础等,HTTP GET是没有缓存的)

一个使用HTTP GET访问WebService方法的示例

首先创建一个名为UserHttpGetService的WebService

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

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

    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public int GetRangeRandom(int minValue, int maxValue)
    {
        return new Random(DateTime.Now.Millisecond).Next(minValue, maxValue);
    }
}

然后创建页面,代码如下

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Services>
                <asp:ServiceReference Path="~/Demo03/UseHttpGetService.asmx" InlineScript="true" />
            </Services>
        </asp:ScriptManager>
        
        <input type="button" value="Get Random" onclick="getRandom()" />
        <input type="button" value="Get Range Random" onclick="getRandom(50,100)" />
        
        <script language="javascript" type="text/javascript">
            function getRandom(minValue, maxValue) {
                if (arguments.length != 2) {
                    UseHttpGetService.GetRandom(onSucceeded);
                }
                else {
                    UseHttpGetService.GetRangeRandom(minValue, maxValue, onSucceeded);
                }
            }

            function onSucceeded(result) {
                alert(result);
            }
        </script>
    </form>
</body>
</html>

这样我们就完成了这个示例,我们会发现页面代码和以前访问WebService是一摸一样的,但是我们使用HttpWatch查看就可以发现,他们的访问方式是不一样的,而且我们打开页面源代码也可以发现以下代码

this._invoke(this._get_path(), 'GetRandom',false,{},succeededCallback,failedCallback,userContext); 

this._invoke(this._get_path(), 'GetRangeRandom',true,{minValue:minValue,maxValue:maxValue},succeededCallback,failedCallback,userContext); }}

这里的false和true就表示是不是使用HTTP GET

内容概要:该论文探讨了一种基于粒子群优化(PSO)的STAR-RIS辅助NOMA无线通信网络优化方法。STAR-RIS作为一种新型可重构智能表面,能同时反射和传输信号,与传统仅能反射的RIS不同。结合NOMA技术,STAR-RIS可以提升覆盖范围、用户容量和频谱效率。针对STAR-RIS元素众多导致获取完整信道状态信息(CSI)开销大的问题,作者提出一种在不依赖完整CSI的情况下,联合优化功率分配、基站波束成形以及STAR-RIS的传输和反射波束成形向量的方法,以最大化总可实现速率并确保每个用户的最低速率要求。仿真结果显示,该方案优于STAR-RIS辅助的OMA系统。 适合人群:具备一定无线通信理论基础、对智能反射面技术和非正交多址接入技术感兴趣的科研人员和工程师。 使用场景及目标:①适用于希望深入了解STAR-RIS与NOMA结合的研究者;②为解决无线通信中频谱资源紧张、提高系统性能提供新的思路和技术手段;③帮助理解PSO算法在无线通信优化问题中的应用。 其他说明:文中提供了详细的Python代码实现,涵盖系统参数设置、信道建模、速率计算、目标函数定义、约束条件设定、主优化函数设计及结果可视化等环节,便于读者理解和复现实验结果。此外,文章还对比了PSO与其他优化算法(如DDPG)的区别,强调了PSO在不需要显式CSI估计方面的优势。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值