如何跟踪ScriptService WebService的请求? [英] How to trace ScriptService WebService requests?

查看:215
本文介绍了如何跟踪ScriptService WebService的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旨在记录所有的SOAP请求和响应的SoapExtension。它工作得很好,从使用MS SOAP工具包(工作流程的OnBase)的应用程序调用。不过,这并不由$。阿贾克斯()的HTML页面上呼叫工作。这里有一个例子:

I have a SoapExtension that is intended to log all SOAP requests and responses. It works just fine for calls from an application using the MS Soap Toolkit (OnBase Workflow). But it doesn't work for calls made by $.ajax() on an html page. Here's an example:

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

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

It's calling an ASP.NET 3.5 WebService marked with WebService and ScriptService attributes:

[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的基本知识:

And here are the basics for the SoapExtension and SoapExtensionAttribute:

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

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

我缺少的东西,使LoggingSoapExtension在$阿贾克斯的execute()请求?

Am I missing something that would allow LoggingSoapExtension to execute on $.ajax() requests?

更新

@克里斯Brandsma

@Chris Brandsma

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

It might be because you are requesting Json results instead of XML via your web service (dataType: "json"). So the ScriptService attribute is being activated, but you are not sending SOAP messages.

这答案为什么的SoapExtension不工作。与ScriptService跟踪有什么建议?我想到的唯一的事情是一个ScriptService基类,它提供了一个登录请求方法。但后来我不得不打电话给在每一个的WebMethod在每一个ScriptService WebService的那个方法(我有好几个)。我想使用的东西一样干净和简单的作为的SoapExtension属性,如果可能的话。

That answers why the SoapExtension isn't working. Any suggestions for tracing with ScriptService? The only thing that comes to mind is a ScriptService base class that provides a method to log a request. But then I'd have to call that method in every WebMethod in every ScriptService WebService (I have quite a few). I'd like to use something as clean and simple as a SoapExtension attribute, if possible.

推荐答案

我找到了解决办法。通过使用IHttpModule的,我可以从日志什么(SOAP,JSON,表格等)的请求。在下面的实施中,我选择记录所有的.asmx和.ashx的请求。从这个问题的替代LoggingSoapExtension。

I found a solution. By using an IHttpModule I can log requests from anything (SOAP, JSON, forms, etc). In the implementation below, I've chosen to log all .asmx and .ashx requests. This replaces LoggingSoapExtension from the question.

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);
            }
        }
    }
}

我从href=\"http://stackoverflow.com/questions/386487/capturing-html-generated-from-asp-net\">从ASP.NET 生成HTML捕捉

I borrowed heavily from Capturing HTML generated from ASP.NET.

这篇关于如何跟踪ScriptService WebService的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆