gpt4 book ai didi

asp.net - 如何跟踪 ScriptService WebService 请求?

转载 作者:行者123 更新时间:2023-12-03 22:41:42 25 4
gpt4 key购买 nike

我有一个 SoapExtension,用于记录所有 SOAP 请求和响应。对于来自使用 MS Soap Toolkit(OnBase 工作流)的应用程序的调用,它工作得很好。但它不适用于 html 页面上的 $.ajax() 调用。这是一个例子:

$.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() 请求上执行的内容?

更新

@克里斯·布兰德斯玛

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 基类,它提供了记录请求的方法。但随后我必须在每个 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借了很多钱.

关于asp.net - 如何跟踪 ScriptService WebService 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1020045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com