【问题标题】:How to trace ScriptService WebService requests?如何跟踪 ScriptService WebService 请求?
【发布时间】:2010-11-04 10:30:37
【问题描述】:

我有一个用于记录所有 SOAP 请求和响应的 SoapExtension。它适用于使用 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();

    /// <summary>
    /// Fetches the role items.
    /// </summary>
    /// <returns></returns>
    [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 服务(数据类型:“json”)请求 Json 结果而不是 XML。所以 ScriptService 属性被激活了,但是你没有发送 SOAP 消息。

这回答了为什么 SoapExtension 不起作用。对使用 ScriptService 进行跟踪有什么建议吗?唯一想到的是 ScriptService 基类,它提供了一种记录请求的方法。但是我必须在每个 ScriptService WebService 中的每个 WebMethod 中调用该方法(我有很多)。如果可能的话,我想使用像 SoapExtension 属性一样干净简单的东西。

【问题讨论】:

    标签: asp.net jquery web-services soap-extension


    【解决方案1】:

    我找到了解决方案。通过使用 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);
                }
            }
        }
    }
    

    我向Capturing HTML generated from ASP.NET借了很多钱。

    【讨论】:

    【解决方案2】:

    这可能是因为您通过 Web 服务(数据类型:“json”)请求 Json 结果而不是 XML。所以 ScriptService 属性被激活了,但是你没有发送 SOAP 消息。

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

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

    另外,日志记录的另一个选项是 Log4Net。它可以为您提供更多功能。

    【讨论】:

    • 我请求 json 是因为我不想解析 XML。如果 ScriptService 不使用 SOAP,那么这是有道理的。谢谢!
    【解决方案3】:

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

    【讨论】:

    • 这对测试很有帮助,但我需要完整的请求日志。
    猜你喜欢
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 2023-03-29
    • 2011-05-21
    • 2015-11-19
    相关资源
    最近更新 更多