如何跟踪ScriptService WebService请求?

jru*_*ell 7 asp.net jquery web-services soap-extension

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

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

它调用标记有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());
    }
}
Run Code Online (Sandbox Code Playgroud)

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

public class LoggingSoapExtension : SoapExtension, IDisposable { /*...*/ }

[AttributeUsage(AttributeTargets.Method)]
public sealed class SoapLogAttribute : SoapExtensionAttribute { /*...*/ }
Run Code Online (Sandbox Code Playgroud)

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

更新

@Chris Brandsma

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

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

jru*_*ell 3

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

我大量借鉴了Capturing HTML generated from ASP.NET