如何跟踪ScriptService WebService请求?

我有一个SoapExtension,旨在记录所有SOAP请求和响应。 它适用于使用MS Soap Toolkit(OnBase Workflow)的应用程序调用。 但它不适用于$ .ajax()在html页面上进行的调用。 这是一个例子:

$.ajax({ type: "POST", url: url, data: data, contentType: "application/json; charset=utf-8", dataType: "json" }); 

它调用标记有WebService和ScriptService属性的ASP.NET 3.5 WebService:

 [WebService(Namespace = XmlSerializationService.DefaultNamespace)] [ScriptService] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class DepartmentAssigneeService : WebService { private readonly DepartmentAssigneeController _controller = new DepartmentAssigneeController(); ///  /// Fetches the role items. ///  ///  [WebMethod] [SoapLog] public ListItem[] FetchDepartmentItems() { return CreateListItems(_controller.FetchDepartments()); } } 

以下是SoapExtension和SoapExtensionAttribute的基础知识:

 public class LoggingSoapExtension : SoapExtension, IDisposable { /*...*/ } [AttributeUsage(AttributeTargets.Method)] public sealed class SoapLogAttribute : SoapExtensionAttribute { /*...*/ } 

我错过了允许LoggingSoapExtension在$ .ajax()请求上执行的内容吗?

更新

@Chris Brandsma

这可能是因为您通过Web服务(dataType:“json”)请求Json结果而不是XML。 因此,正在激活ScriptService属性,但您不发送SOAP消息。

这就解释了SoapExtension无法正常工作的原因。 有关使用ScriptService进行跟踪的任何建议吗? 我们唯一想到的是ScriptService基类,它提供了一个记录请求的方法。 但是我必须在每个ScriptService WebService中的每个WebMethod中调用该方法(我有很多)。 如果可能的话,我想使用像SoapExtension属性一样干净简单的东西。

我找到了解决方案。 通过使用IHttpModule,我可以记录来自任何东西的请求(SOAP,JSON,表单等)。 在下面的实现中,我选择记录所有.asmx和.ashx请求。 这将从问题中替换LoggingSoapExtension。

 public class ServiceLogModule : IHttpModule { private HttpApplication _application; private bool _isWebService; private int _requestId; private string _actionUrl; #region IHttpModule Members public void Dispose() { } public void Init(HttpApplication context) { _application = context; _application.BeginRequest += ContextBeginRequest; _application.PreRequestHandlerExecute += ContextPreRequestHandlerExecute; _application.PreSendRequestContent += ContextPreSendRequestContent; } #endregion private void ContextPreRequestHandlerExecute(object sender, EventArgs e) { _application.Response.Filter = new CapturedStream(_application.Response.Filter, _application.Response.ContentEncoding); } private void ContextBeginRequest(object sender, EventArgs e) { string ext = VirtualPathUtility.GetExtension(_application.Request.FilePath).ToLower(); _isWebService = ext == ".asmx" || ext == ".ashx"; if (_isWebService) { ITraceLog traceLog = TraceLogFactory.Create(); _actionUrl = _application.Request.Url.PathAndQuery; StreamReader reader = new StreamReader(_application.Request.InputStream); string message = reader.ReadToEnd(); _application.Request.InputStream.Position = 0; _requestId = traceLog.LogRequest(_actionUrl, message); } } private void ContextPreSendRequestContent(object sender, EventArgs e) { if (_isWebService) { CapturedStream stream = _application.Response.Filter as CapturedStream; if (stream != null) { ITraceLog traceLog = TraceLogFactory.Create(); traceLog.LogResponse(_actionUrl, stream.StreamContent, _requestId); } } } } 

我从捕获从ASP.NET生成的HTML中大量借用。

这可能是因为您通过Web服务(dataType:“json”)请求Json结果而不是XML。 因此,正在激活ScriptService属性,但您不发送SOAP消息。

您可以将dataType更改为xml,看看是否有效。

http://docs.jquery.com/Ajax/jQuery.ajax#options

另外,日志记录的另一个选项是Log4Net。 它对你来说可能更加通用。

Fiddler(主要用于IE,但现在用于firefox)或Firebug(用于firefox)是用于监视客户端请求和响应的宝贵工具。