•编写一个普通的ASP.NET WebService
•为WebService类添加自定义属性标记
–ScriptServiceAttribute
•释放WebService方法
–访问级别为public
–使用WebMethodAttribute进行标记
•为页面中的ScriptManager(Proxy)引入asmx文件
客户端访问WebService
•[Namespaces.]ClassName.MethodName
•依次传入参数
•传入一个方法做为成功后的回调函数
•即使没有返回值也会调用回调函数
客户端访问WebService基础
aspx
<asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Debug">
<Services>
<asp:ServiceReference Path="WebServiceFoundation.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)
{
Sample.WebServiceFoundation.GetRandom(getRandomSucceeded);
}
else
{
Sample.WebServiceFoundation.GetRangeRandom(minValue, maxValue, getRandomSucceeded);
}
}
function getRandomSucceeded(result)
{
alert(result);
}
</script>
</form>
cs
{
}
WebServiceFoundation.asmx
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
namespace Sample
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WebServiceFoundation : System.Web.Services.WebService
{
[WebMethod]
public int GetRandom()
{
return new Random(DateTime.Now.Millisecond).Next();
}
[WebMethod]
public int GetRangeRandom(int minValue, int maxValue)
{
return new Random(DateTime.Now.Millisecond).Next(minValue, maxValue);
}
}
}