ASP.NET Web API内存中测试中的内部服务器错误

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异常时中断.代码在任何异常时都不会停止,也不能在响应中找到任何异常详细信息.

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

解决方法

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

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....