2012-05-22 26 views
5

Tôi nhận được "lỗi máy chủ nội bộ" (mã trạng thái 500) khi kiểm tra bộ điều khiển ASP.NET Web API trong in-memory test.Lỗi máy chủ nội bộ trong thử nghiệm ASP.NET Web API trong bộ nhớ

[TestFixture] 
public class ValuesControllerTest 
{ 
    private HttpResponseMessage response; 

    [TestFixtureSetUp] 
    public void Given() 
    { 
     var config = new HttpConfiguration 
     { 
      IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always 
     }; 

     config.Routes.MapHttpRoute(
      name: "DefaultApi", 
      routeTemplate: "api/{controller}/{id}", 
      defaults: new { controller = typeof(ValuesController).Name.Replace("Controller", string.Empty), id = RouteParameter.Optional } 
     ); 

     //This method will cause internal server error but NOT throw any exceptions 
     //Remove this call and the test will be green 
     ScanAssemblies(); 

     var server = new HttpServer(config); 
     var client = new HttpClient(server); 
     response = client.GetAsync("http://something/api/values/5").Result; 
     //Here response has status code 500 

    } 

    private void ScanAssemblies() 
    { 
     PluginScanner.Scan(".\\", IsApiController); 
    } 

    private bool IsApiController(Type type) 
    { 
     return typeof (ApiController).IsAssignableFrom(type); 
    } 

    [Test] 
    public void Can_GET_api_values_5() 
    { 
     Assert.IsTrue(response.IsSuccessStatusCode); 
    } 
} 

public static class PluginScanner 
{ 
    public static IEnumerable<Type> Scan(string directoryToScan, Func<Type, bool> filter) 
    { 
     var result = new List<Type>(); 
     var dir = new DirectoryInfo(directoryToScan); 

     if (!dir.Exists) return result; 

     foreach (var file in dir.EnumerateFiles("*.dll")) 
     { 
      result.AddRange(from type in Assembly.LoadFile(file.FullName).GetTypes() 
          where filter(type) 
          select type); 
     } 
     return result; 
    } 
} 

Tôi đã định cấu hình Visual Studio để ngắt khi có bất kỳ ngoại lệ .Net nào được ném. Mã không được dừng lại ở bất kỳ ngoại lệ nào cũng như tôi không thể tìm thấy bất kỳ chi tiết ngoại lệ nào trong phản hồi.

Tôi nên làm gì để xem nguyên nhân gây ra "lỗi máy chủ nội bộ"?

+0

Dấu vết ngăn xếp như thế nào? Đặt nó ở đây. – Aliostad

+0

Vâng, đó là vấn đề. Tôi không nhận được dấu vết ngăn xếp. Chỉ một câu trả lời cho biết "Lỗi Máy chủ Nội bộ" –

+0

'// cấu hình bổ sung' có nghĩa là gì? Bạn đã bỏ qua một số mã? – Aliostad

Trả lời

9

Trường hợp ngoại lệ là trong Response.Content

if (Response != null && Response.IsSuccessStatusCode == false) 
{ 
    var result = Response.Content.ReadAsStringAsync().Result; 
    Console.Out.WriteLine("Http operation unsuccessfull"); 
    Console.Out.WriteLine(string.Format("Status: '{0}'", Response.StatusCode)); 
    Console.Out.WriteLine(string.Format("Reason: '{0}'", Response.ReasonPhrase)); 
    Console.Out.WriteLine(result); 
} 
+1

Như được đề xuất trong câu trả lời khác, bạn có thể cần đặt chính sách chi tiết lỗi var config = new HttpConfiguration {IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always}; –

4

Bạn cần phải thêm một tuyến đường để nó trông giống như sau:

 var config = new HttpConfiguration() 
     { 
      IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always 
     }; 

     config.Routes.MapHttpRoute(
      name: "default", 
      routeTemplate: "api/{controller}/{id}", 
      defaults: new { controller = "Home", id = RouteParameter.Optional }); 

     var server = new HttpServer(config); 
     var client = new HttpClient(server); 

     HttpResponseMessage response = client.GetAsync("http://somedomain/api/product").Result; 

Btw, trong các bit mới nhất bạn nhận được một 404 Not Found như bạn mong đợi.

Henrik

+0

Tôi có allready. Trong thực tế, tôi có khá nhiều cấu hình. Các tuyến, bộ lọc, thùng chứa IoC, v.v. Vấn đề của tôi là một cái gì đó xảy ra trong đường ống gây ra một lỗi nội bộ được gửi trở lại nhưng không có thông tin về nó. –

+0

@Martin Nilsson - Henrik là kiến ​​trúc sư WebAPI. Tôi sẽ nghe anh ta. – RickAndMSFT

0

Có vẻ như bạn có thể đã tìm thấy câu trả lời của bạn, nhưng điều đó đã không được khá nó cho tôi vì vậy tôi muốn để thêm video này cho những người khác có vấn đề của tôi.

Để bắt đầu, có vẻ như đó là vấn đề với các trình định dạng MVC 4 mới. Đặt bất kỳ cờ chính sách lỗi nào sẽ không hoạt động (IncludeErrorDetailPolicy, CustomErrors, vv), các trình định dạng này sẽ bỏ qua chúng và chỉ trả lại và trống "lỗi máy chủ nội bộ" 500.

Tôi tìm thấy điều này bằng cách quá tải các định dạng và kiểm tra phản ứng của họ đối với các lỗi:

public class XmlMediaTypeFormatterWrapper : XmlMediaTypeFormatter 
{ 
    public override Task WriteToStreamAsync(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, TransportContext transportContext) 
    { 
     var ret = base.WriteToStreamAsync(type, value, stream, contentHeaders, transportContext); 
     if (null != ret.Exception) 
      // This means there was an error and ret.Exception has all the error message data you would expect, but once you return below, all you get is a blank 500 error... 

     return ret; 
    } 
} 

để bây giờ tôi đang sử dụng Xml và giấy gói định dạng Json mà chỉ đơn giản tìm kiếm ret.Exception và nắm bắt nó vì vậy tôi ít nhất có dữ liệu nếu một 500 xảy ra. Tôi không thể thực sự tìm thấy một cách thanh lịch để làm cho các lỗi thực sự hiển thị trong phản ứng html kể từ khi Task.Exception đã được thiết lập và NÊN này được tất cả các yêu cầu để vượt qua ngoại lệ cùng.

Các vấn đề liên quan