如何跟踪 ScriptService WebService 请求?

发布于 2024-07-25 05:14:46 字数 1715 浏览 6 评论 0原文

我有一个 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();

    /// <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 结果而不是 XML(数据类型:“json”)。 因此 ScriptService 属性已被激活,但您并未发送 SOAP 消息。

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

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

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

And here are the basics for the SoapExtension and SoapExtensionAttribute:

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

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

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

Update

@Chris Brandsma

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

八巷 2024-08-01 05:14:46

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

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

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

梨涡少年 2024-08-01 05:14:46

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

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

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

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

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.

You could change the dataType to xml and see if that works.

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

Also, another option for logging would be Log4Net. It could be a lot more versatile for you.

假装不在乎 2024-08-01 05:14:46

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

Fiddler (for IE primarily, but now for firefox) or Firebug (for firefox) are invaluable tools for watching your client-side requests and responses.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文