Bạn có thể sử dụng mô-đun HTTP để nắm bắt thông báo ngoại lệ, theo dõi ngăn xếp và loại ngoại lệ được phương pháp dịch vụ web đưa ra.
Đầu tiên một số nền tảng ...
Nếu một phương pháp dịch vụ web ném một ngoại lệ đáp ứng HTTP có một mã trạng thái 500.
Nếu lỗi tùy chỉnh được tắt thì dịch vụ web sẽ trả về ngoại lệ thông báo và theo dõi ngăn xếp cho ứng dụng khách dưới dạng JSON.Ví dụ:
{"Message":"Exception message","StackTrace":" at WebApplication.HelloService.HelloWorld() in C:\Projects\Stackoverflow Examples\WebApplication\WebApplication\HelloService.asmx.cs:line 22","ExceptionType":"System.ApplicationException"}
Khi lỗi tùy chỉnh trên thì dịch vụ web trả về một thông điệp mặc định cho khách hàng và loại bỏ các ngăn xếp dấu vết và ngoại lệ loại:
{"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}
Vì vậy, những gì chúng ta cần làm là đặt lỗi tùy chỉnh cho dịch vụ web và cắm vào mô-đun HTTP:
- C hecks nếu yêu cầu là một phương pháp dịch vụ web
- Kiểm tra xem có ngoại lệ hay không - có nghĩa là mã trạng thái 500 đang được trả lại
- Nếu 1) và 2) là đúng thì nhận JSON ban đầu gửi cho khách hàng và thay thế bằng các JSON mặc định
Đoạn code dưới đây là một ví dụ của một mô-đun HTTP thực hiện điều này:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
public class ErrorHandlerModule : IHttpModule {
public void Init(HttpApplication context) {
context.PostRequestHandlerExecute += OnPostRequestHandlerExecute;
context.EndRequest += OnEndRequest;
}
static void OnPostRequestHandlerExecute(object sender, EventArgs e) {
HttpApplication context = (HttpApplication) sender;
// TODO: Update with the correct check for your application
if (context.Request.Path.StartsWith("/HelloService.asmx")
&& context.Response.StatusCode == 500) {
context.Response.Filter =
new ErrorHandlerFilter(context.Response.Filter);
context.EndRequest += OnEndRequest;
}
}
static void OnEndRequest(object sender, EventArgs e) {
HttpApplication context = (HttpApplication) sender;
ErrorHandlerFilter errorHandlerFilter =
context.Response.Filter as ErrorHandlerFilter;
if (errorHandlerFilter == null) {
return;
}
string originalContent =
Encoding.UTF8.GetString(
errorHandlerFilter.OriginalBytesWritten.ToArray());
// If customErrors are Off then originalContent will contain JSON with
// the original exception message, stack trace and exception type.
// TODO: log the exception
}
public void Dispose() { }
}
Module này sử dụng bộ lọc sau để ghi đè lên nội dung gửi đến khách hàng và lưu trữ các byte gốc (mà contai n thông báo ngoại lệ, theo dõi ngăn xếp và loại ngoại lệ):
public class ErrorHandlerFilter : Stream {
private readonly Stream _responseFilter;
public List OriginalBytesWritten { get; private set; }
private const string Content =
"{\"Message\":\"There was an error processing the request.\"" +
",\"StackTrace\":\"\",\"ExceptionType\":\"\"}";
public ErrorHandlerFilter(Stream responseFilter) {
_responseFilter = responseFilter;
OriginalBytesWritten = new List();
}
public override void Flush() {
byte[] bytes = Encoding.UTF8.GetBytes(Content);
_responseFilter.Write(bytes, 0, bytes.Length);
_responseFilter.Flush();
}
public override long Seek(long offset, SeekOrigin origin) {
return _responseFilter.Seek(offset, origin);
}
public override void SetLength(long value) {
_responseFilter.SetLength(value);
}
public override int Read(byte[] buffer, int offset, int count) {
return _responseFilter.Read(buffer, offset, count);
}
public override void Write(byte[] buffer, int offset, int count) {
for (int i = offset; i < offset + count; i++) {
OriginalBytesWritten.Add(buffer[i]);
}
}
public override bool CanRead {
get { return _responseFilter.CanRead; }
}
public override bool CanSeek {
get { return _responseFilter.CanSeek; }
}
public override bool CanWrite {
get { return _responseFilter.CanWrite; }
}
public override long Length {
get { return _responseFilter.Length; }
}
public override long Position {
get { return _responseFilter.Position; }
set { _responseFilter.Position = value; }
}
}
Phương pháp này yêu cầu phải tắt các lỗi tùy chỉnh cho dịch vụ web. Bạn có thể muốn giữ các lỗi tùy chỉnh cho phần còn lại của ứng dụng để các dịch vụ web phải được đặt trong một thư mục con. Lỗi tùy chỉnh có thể được tắt trong thư mục đó chỉ bằng cách sử dụng web.config ghi đè cài đặt gốc.
Nguồn
2009-02-15 20:28:15
Mặc dù tôi không tuân theo điều này trong dự án hiện tại của tôi, đây có lẽ là cách duy nhất để nắm bắt tất cả các lỗi này một cách đáng tin cậy. – Clyde
Tôi đã cố gắng này, nhưng bằng cách nào đó bối cảnh của tôi.Response.Filter.length ném một ngoại lệ của loại 'System.NotSupportedException'. Nó không hoạt động. Tôi nghĩ nó rất gần, đầu ra của tôi chính xác như bạn đã nói. và tôi nhận được 500 mã trạng thái. – maxisam