8

在内存测试中测试 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 异常时中断。代码不会因任何异常而停止,我也无法在响应中找到任何异常详细信息。

我应该怎么做才能查看导致“内部服务器错误”的原因?

4

3 回答 3

12

例外是在 Response.Content

if (Response != null && Response.IsSuccessStatusCode == false)
{
    var result = Response.Content.ReadAsStringAsync().Result;
    Console.Out.WriteLine("Http operation unsuccessful");
    Console.Out.WriteLine(string.Format("Status: '{0}'", Response.StatusCode));
    Console.Out.WriteLine(string.Format("Reason: '{0}'", Response.ReasonPhrase));
    Console.Out.WriteLine(result);
}
于 2012-05-23T10:53:29.237 回答
4

您需要添加一条路线,使其看起来像这样:

        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;

顺便说一句,在最新的位中,您会得到 404 Not Found,如您所料。

亨里克

于 2012-05-22T21:36:44.967 回答
1

听起来您可能已经找到了答案,但这对我来说并不完全如此,所以我想为其他有我的问题的人添加这个。

首先,这似乎是新的 MVC 4 格式化程序的问题。设置任何错误策略标志都不起作用(IncludeErrorDetailPolicy、CustomErrors 等),这些格式化程序会忽略它们,只是返回并清空“内部服务器错误”500。

我最终通过重载格式化程序并检查它们的响应是否有错误发现了这一点:

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;
    } 
}

现在我正在使用 Xml 和 Json 格式化程序包装器,它们只是查找 ret.Exception 并捕获它,因此如果发生 500,我至少有数据。我真的找不到一种优雅的方法来使错误实际显示在 html 响应中,因为 Task.Exception 已经设置,这应该是传递异常所需的全部。

于 2012-08-14T19:45:56.517 回答