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ộ"?
Dấu vết ngăn xếp như thế nào? Đặt nó ở đây. – Aliostad
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ộ" –
'// cấu hình bổ sung' có nghĩa là gì? Bạn đã bỏ qua một số mã? – Aliostad