简体   繁体   English

调试自托管的WebApi应用程序

[英]Debugging a self hosted WebApi application

I have a WebApi application with the following controllor: 我有一个WebApi应用程序与以下控制器:

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

The route looks like this 路线看起来像这样

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

When I host the service in IIS / cassini, if I POST to api/content/whatever then as expected, my controller action is hit. 当我在IIS / cassini中托管服务时,如果我按照预期POST到api/content/whatever那么我的控制器操作就会被命中。

However, 然而,

I've got a test project, that SelfHosts this api 我有一个测试项目,SelfHosts这个api

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

    var message = result.Content.ReadAsStringAsync().Result;
}

If I debug the unit test, and step into it, result is: 如果我调试单元测试,并进入它, 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; {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]],标题:{Content-Type:application / json; charset=utf-8 }} charset = utf-8}}

Unhelpfully, message is literally just 无益, message实际上是公正的

An error has occurred 发生了错误

Is there a way I can debug my self hosted WebApi to find out what is causing this error? 有没有办法我可以调试自我托管的WebApi来找出导致此错误的原因?

Set up 建立

Server is just an HttpServer from a base class, that holds my self hosted server, new'd up like so: Server只是一个来自基类的HttpServer ,它拥有我自己托管的服务器,如下所示:

var httpConfig = new HttpSelfHostConfiguration(BaseAddress);

new ApiServiceConfiguration(httpConfig).Configure();

var server = new HttpSelfHostServer(httpConfig);
server.OpenAsync().Wait();
Server = server;

If you enable tracing and add a Trace.Listeners.Add(new ConsoleTraceListener()) then you will get more detailed error messages. 如果启用跟踪并添加Trace.Listeners.Add(new ConsoleTraceListener()) ,则会收到更详细的错误消息。 However, your problem is most likely related to the fact that the object that you are trying to serialize is failing to serialize. 但是,您的问题很可能与您尝试序列化的对象无法序列化有关。

Added to Startup 添加到Startup

config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

where config is of HttpConfiguration type. 其中configHttpConfiguration类型。

This single line of code solved the same issue for me - now I can see all the inner exception's details. 这一行代码为我解决了同样的问题 - 现在我可以看到所有内部异常的细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM