[英]How to trace ScriptService WebService requests?
我有一個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服務(dataType:“json”)請求Json結果而不是XML。 因此,正在激活ScriptService屬性,但您不發送SOAP消息。
這就解釋了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從ASP.NET生成的HTML中大量借用。
這可能是因為您通過Web服務(dataType:“json”)請求Json結果而不是XML。 因此,正在激活ScriptService屬性,但您不發送SOAP消息。
您可以將dataType更改為xml,看看是否有效。
http://docs.jquery.com/Ajax/jQuery.ajax#options
另外,日志記錄的另一個選項是Log4Net。 它對你來說可能更加通用。
Fiddler(主要用於IE,但現在用於firefox)或Firebug(用於firefox)是用於監視客戶端請求和響應的寶貴工具。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.