我有一个SoapExtension,用于记录所有SOAP请求和响应。对于使用(OnBase工作流)的应用程序的调用,它工作得很好。但是它不适用于html页面上的$.ajax()调用。下面是一个例子:
$.ajax({
type: "POST",
url: url,
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json"
});它正在调用一个标记为ASP.NET和ScriptService属性的WebService 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中的每个WebService中调用该方法(我有相当多的)。如果可能的话,我想使用像SoapExtension属性这样干净和简单的东西。
发布于 2009-06-23 19:19:30
我找到了解决办法。通过使用IHttpModule,我可以记录来自任何地方(SOAP、JSON、forms等)的请求。在下面的实现中,我选择记录所有.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那里借了很多钱。
发布于 2009-06-19 21:54:24
这可能是因为您通过web服务(dataType:" Json ")请求json结果而不是XML。因此,ScriptService属性正在被激活,但您没有发送SOAP消息。
您可以将dataType更改为xml,并查看这是否有效。
http://docs.jquery.com/Ajax/jQuery.ajax#options
另外,日志记录的另一个选项是Log4Net。对你来说可能更多用途。
发布于 2009-06-22 13:35:03
Fiddler (主要用于IE,现在用于firefox)或Firebug (用于firefox)是监视客户端请求和响应的宝贵工具。
https://stackoverflow.com/questions/1020045
复制相似问题