不当之处,欢迎指正。
我先把例子中的核心代码列出,方便大家阅读。
HelloWorldService服务中:
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/5871dade63cd4b90955351619ab48fbb.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/b163e05ea8e34dffade77b366b358acd.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/b163e05ea8e34dffade77b366b358acd.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/b163e05ea8e34dffade77b366b358acd.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/b163e05ea8e34dffade77b366b358acd.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/b163e05ea8e34dffade77b366b358acd.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/2e973ee9fadc49d0a07f0d37c90256f1.gif)
HelloWorld.aspx文件:
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/5871dade63cd4b90955351619ab48fbb.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/b163e05ea8e34dffade77b366b358acd.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/b163e05ea8e34dffade77b366b358acd.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/b163e05ea8e34dffade77b366b358acd.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/b163e05ea8e34dffade77b366b358acd.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/2e973ee9fadc49d0a07f0d37c90256f1.gif)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/5871dade63cd4b90955351619ab48fbb.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/b163e05ea8e34dffade77b366b358acd.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/b163e05ea8e34dffade77b366b358acd.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/2e973ee9fadc49d0a07f0d37c90256f1.gif)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
后台调用机制分析:(都是Microsoft.Web.Extensions.dll反编译后看到的)
1、在AssemblyInfo.cs中声明了如下一些javascript脚本:
[assembly: System.Web.UI.WebResource("Microsoft.Web.Resources.Timer.bmp", "image/bmp")]
[assembly: System.Web.UI.WebResource("Microsoft.Web.Resources.Background.gif", "image/gif")]
[assembly: System.Web.UI.WebResource("Microsoft.Web.Resources.ScriptLibrary.MicrosoftAjaxWebForms.debug.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("Microsoft.Web.Resources.ScriptLibrary.MicrosoftAjaxRuntime.debug.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("Microsoft.Web.Resources.ScriptLibrary.MicrosoftAjax.debug.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("Microsoft.Web.Resources.ScriptLibrary.MicrosoftAjaxWebForms.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("Microsoft.Web.Resources.ScriptLibrary.MicrosoftAjaxRuntime.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("Microsoft.Web.Resources.ScriptLibrary.MicrosoftAjax.js", "application/x-javascript")]
这些javascript脚本也都是作为资源包含在Microsoft.Web.Extensions.dll中的
2、在ScriptManager的OnPreRender中进行如下操作,进行js代码的注册(有的方法是调用其它类里面的):
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/5871dade63cd4b90955351619ab48fbb.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/b163e05ea8e34dffade77b366b358acd.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/0b464375f4434b59846b7f068c01e1ea.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/b163e05ea8e34dffade77b366b358acd.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/73d1f43a907044479cffecf16f7b5728.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/b163e05ea8e34dffade77b366b358acd.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/2e973ee9fadc49d0a07f0d37c90256f1.gif)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/5871dade63cd4b90955351619ab48fbb.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/b163e05ea8e34dffade77b366b358acd.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/2e973ee9fadc49d0a07f0d37c90256f1.gif)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/5871dade63cd4b90955351619ab48fbb.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/b163e05ea8e34dffade77b366b358acd.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/2e973ee9fadc49d0a07f0d37c90256f1.gif)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
(通过查看运行时的aspx文件的html代码)
<script src="/Web/WebResource.axd?d=7lZSau3voGpyPgiEEMwJxQ2&t=632965017419375000" type="text/javascript"></script>
......
<script src="/Web/WebResource.axd?d=VbevhO3ptb2WYvwQWrgTHKAWH4jfDIInCsIvYHSSJgRDZKhi9uLV9U_0kyOUMuNB32yLgf_UUzCml8vnGl7OI4r89qpFWhjBi2BmMZGBkApjwmG5MAGWzA_yjFYvRA8w2gdgDWR5RgcQF-F7MVigcyaKFVde2W9oIqW8P4zdL9g1&t=632985966674218750" type="text/javascript"></script>
<script src="HelloWorldService.asmx/jsdebug" type="text/javascript"></script>
3、通过IHttpHandlerFactory接口对服务进行处理:
在web.config中进行注册:
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="Microsoft.Web.Script.Services.ScriptHandlerFactory, Microsoft.Web.Extensions"/>
</httpHandlers>
然后在ScriptHandlerFactory中调用如下一段代码:
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/5871dade63cd4b90955351619ab48fbb.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/b163e05ea8e34dffade77b366b358acd.gif)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384589/20241017/2e973ee9fadc49d0a07f0d37c90256f1.gif)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/6cqkyzyy2svti/developer-article384659/20241017/78e4818cddfa4e738f124d9a07f5cb42.gif)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/notfound.png?x-oss-process=image/resize,w_1400/format,webp)
以上代码实际上就是根据页面上有的服务生成对应的javascript代码,比如我们针对上面的HelloWorldService.asmx服务,就会生成如下一段javascript
代码,注册到页面中去:
var HelloWorldService=function() {
this._timeout = 0;
this._userContext = null;
this._succeeded = null;
this._failed = null;
}
HelloWorldService.prototype={
HelloWorld:function(name,succeededCallback, failedCallback, userContext) {
/// <summary>Invoke the HelloWorld WebMethod</summary>
/// <param name=\"name\">WebMethod parameter: name(type: String)</param>
/// <param name=\"succeededCallback\" type=\"function\" optional=\"true\">Callback on successful completion of request</param>
/// <param name=\"failedCallback\" type=\"function\" optional=\"true\">Callback on failure of request</param>
/// <param name=\"userContext\" optional=\"true\">User context data (any JavaScript type)</param>
return Sys.Net._WebMethod._invoke.apply(null, [ this, 'HelloWorld','HelloWorldService.HelloWorld',false,{name:name},succeededCallback, failedCallback, userContext]); },_get_path: function() { return HelloWorldService.get_path(); },
set_timeout: function(value) {
/// <summary>Sets the timeout for this service.</summary>
/// <param name=\"value\" type=\"Number\">The timeout in milliseconds.</param>
var e = Function._validateParams(arguments, [{name: 'timeout', type: Number}]);
if (e) throw e;
if (value < 0) {
throw Error.argumentOutOfRange('value', value, Sys.Res.invalidTimeout);
}
this._timeout = value;
},
get_timeout: function() {
/// <summary>Returns the timeout in milliseconds for this service.</summary>
/// <returns type=\"Number\">The timeout in milliseconds for the service.</returns>
return this._timeout;
},
set_defaultUserContext: function(value) {
/// <summary>Sets the default userContext for this service.</summary>
/// <param name=\"value\">The default userContext for this service.</param>
this._userContext = value;
},
get_defaultUserContext: function() {
/// <summary>Returns the default userContext for this service.</summary>
/// <returns>Returns the default userContext for this service.</returns>
return this._userContext;
},
set_defaultSucceededCallback: function(value) {
/// <summary>Sets the default succeededCallback for this service.</summary>
/// <param name=\"value\" type=\"Function\">The default succeededCallback for this service.</param>
var e = Function._validateParams(arguments, [{name: 'defaultSucceededCallback', type: Function}]);
if (e) throw e;
this._succeeded = value;
},
get_defaultSucceededCallback: function() {
/// <summary>Returns the default succeededCallback for this service.</summary>
/// <returns type=\"Function\">Returns the default succeededCallback for this service.</returns>
return this._succeeded;
},
set_defaultFailedCallback: function(value) {
/// <summary>Sets the default FailedCallback for this service.</summary>
/// <param name=\"value\" type=\"Function\">The default FailedCallback for this service.</param>
var e = Function._validateParams(arguments, [{name: 'set_defaultFailedCallback', type: Function}]);
if (e) throw e;
this._failed = value;
},
get_defaultFailedCallback: function() {
/// <summary>Returns the default failedCallback for this service.</summary>
/// <returns type=\"Function\">Returns the default failedCallback for this service.</returns>
return this._failed;
}
}
HelloWorldService._staticInstance = new HelloWorldService();
HelloWorldService.set_path = function(value) {
/// <summary>Sets the service url.</summary>
/// <param name=\"path\" type=\"String\">The service url.
var e = Function._validateParams(arguments, [{name: 'path', type: String}]); if (e) throw e; HelloWorldService._staticInstance._path = value; }
HelloWorldService.get_path = function() {
/// <summary>Returns the service url.</summary>
/// <returns type=\"String\">The service url.</returns>
return HelloWorldService._staticInstance._path; }
HelloWorldService.set_timeout = function(value) {
/// <summary>Sets the service timeout.</summary>
/// <param name=\"value\" type=\"Number\">The service timeout.
var e = Function._validateParams(arguments, [{name: 'timeout', type: Number}]); if (e) throw e; if (value < 0) { throw Error.argumentOutOfRange('value', value, Sys.Res.invalidTimeout); }
HelloWorldService._staticInstance._timeout = value; }
HelloWorldService.get_timeout = function() {
/// <summary>Returns the service timeout.</summary>
/// <returns type=\"Number\">The service timeout.</returns>
return HelloWorldService._staticInstance._timeout; }
HelloWorldService.set_defaultUserContext = function(value) {
/// <summary>Sets the service default userContext.</summary>
/// <param name=\"value\">The service default user context.
HelloWorldService._staticInstance._userContext = value; }
HelloWorldService.get_defaultUserContext = function() {
/// <summary>Returns the service default user context.</summary>
/// <returns>The service default user context.</returns>
return HelloWorldService._staticInstance._userContext; }
HelloWorldService.set_defaultSucceededCallback = function(value) {
/// <summary>Sets the service default succeeded callback.</summary>
/// <param name=\"value\" type=\"Function\">The service default succeed callback function.
var e = Function._validateParams(arguments, [{name: 'defaultSucceededCallback', type: Function}]); if (e) throw e; HelloWorldService._staticInstance._succeeded = value; }
HelloWorldService.get_defaultSucceededCallback = function() {
/// <summary>Returns the service default succeeded callback.</summary>
/// <returns type=\"Function\">The service default succeeded callback.</returns>
return HelloWorldService._staticInstance._succeeded; }
HelloWorldService.set_defaultFailedCallback = function(value) {
/// <summary>Sets the service default failed callback function.</summary>
/// <param name=\"value\" type=\"Function\">The service default failed callback function.
var e = Function._validateParams(arguments, [{name: 'defaultFailedCallback', type: Function}]); if (e) throw e; HelloWorldService._staticInstance._failed = value; }
HelloWorldService.get_defaultFailedCallback = function() {
/// <summary>Returns the service default failed callback function.</summary>
/// <returns type=\"Function\">The service default failed callback function.</returns>
return HelloWorldService._staticInstance._failed; }
HelloWorldService.set_path(\"/Web/HelloWorldService.asmx\");
HelloWorldService.HelloWorld= function(name,onSuccess,onFailed,userContext) { /// <summary>Invoke the HelloWorld WebMethod</summary>
/// <param name=\"name\">WebMethod parameter: name(type: String)</param>
/// <param name=\"succeededCallback\" type=\"function\" optional=\"true\">Callback on successful completion of request</param>
/// <param name=\"failedCallback\" type=\"function\" optional=\"true\">Callback on failure of request</param>
/// <param name=\"userContext\" optional=\"true\">User context data (any JavaScript type)</param>
HelloWorldService._staticInstance.HelloWorld(name,onSuccess,onFailed,userContext); }
4、实际调用
这样服务注册好之后,我们在页面上写的
var fs = HelloWorldService;
fs.set_defaultSucceededCallback(OnShow);
fs.HelloWorld(document.getElementById("name").value);
实际上就是调用注册好的javascript脚本,然后这些脚本再通过通用的AJAX连接XML的异步方式调用后台代码就可以了
本文转自永春博客园博客,原文链接:http://www.cnblogs.com/firstyi/archive/2006/11/08/554374.html,如需转载请自行联系原作者