我有一个SoapExtension,旨在记录所有SOAP请求和响应。对于使用MS Soap Toolkit(OnBase工作流)的应用程序进行的调用,它工作得很好。但是它不适用于$ .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)



这就回答了为什么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);
            }
        }
    }
}

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

10-07 19:31
查看更多