调试自托管 WebApi 应用程序

Ale*_*lex 5 c# asp.net debugging self-hosting asp.net-web-api

我有一个带有以下控制器的 WebApi 应用程序:

public class ContentController : ApiController
{
    [HttpPost]
    public HttpResponseMessage Post(string contentType)
    {
        //do stuff
        return new HttpResponseMessage(HttpStatusCode.OK);
    }
}
Run Code Online (Sandbox Code Playgroud)

路线是这样的

routes.MapHttpRoute("content", 
    "api/content/{contentType}", 
    new { controller = "Content", contentType = RouteParameter.Optional });
Run Code Online (Sandbox Code Playgroud)

当我在 IIS / cassini 中托管服务时,如果我api/content/whatever按预期POST 到那时,我的控制器操作就会被命中。

然而,

我有一个测试项目,SelfHosts 这个 api

using (var client = new HttpClient(Server))
{
    var result = client.PostAsync(BaseAddress + "api/content/whatever"

    var message = result.Content.ReadAsStringAsync().Result;
}
Run Code Online (Sandbox Code Playgroud)

如果我调试单元测试并进入它,result则是:

{StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.ObjectContent`1[[System.Web.Http.HttpError, System.Web.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]], Headers: { Content-Type: application/json; 字符集=utf-8 }}

无益,message实际上只是

发生了错误

有没有办法调试我自己托管的 WebApi 以找出导致此错误的原因?

设置

Server只是HttpServer来自基类的一个,它包含我的自托管服务器,就像这样:

var httpConfig = new HttpSelfHostConfiguration(BaseAddress);

new ApiServiceConfiguration(httpConfig).Configure();

var server = new HttpSelfHostServer(httpConfig);
server.OpenAsync().Wait();
Server = server;
Run Code Online (Sandbox Code Playgroud)

Dar*_*ler 2

如果您启用跟踪并添加,Trace.Listeners.Add(new ConsoleTraceListener())那么您将收到更详细的错误消息。但是,您的问题很可能与您尝试序列化的对象无法序列化有关。