我在in-memory test測試的ASP.NET Web API控制器時,得到一個「內部服務器錯誤」(狀態代碼500)。內部服務器錯誤內存測試
[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;
}
}
我已經配置的Visual Studio時,任何.NET拋出異常打破。代碼不會在任何異常情況下停止,也不能在響應中找到任何異常詳細信息。
我應該怎麼做,看看是什麼導致了「內部服務器錯誤」?
什麼是堆棧跟蹤什麼樣的?把這個放在這裏。 – Aliostad
那麼,這就是問題所在。我沒有得到堆棧跟蹤。只有一個迴應說「內部服務器錯誤」 –
「//附加配置」是什麼意思?你有沒有遺漏一些代碼? – Aliostad