[英]Debugging a self hosted WebApi application
我有一个WebApi应用程序与以下控制器:
public class ContentController : ApiController
{
[HttpPost]
public HttpResponseMessage Post(string contentType)
{
//do stuff
return new HttpResponseMessage(HttpStatusCode.OK);
}
}
路线看起来像这样
routes.MapHttpRoute("content",
"api/content/{contentType}",
new { controller = "Content", contentType = RouteParameter.Optional });
当我在IIS / cassini中托管服务时,如果我按照预期POST到api/content/whatever
那么我的控制器操作就会被命中。
然而,
我有一个测试项目,SelfHosts这个api
using (var client = new HttpClient(Server))
{
var result = client.PostAsync(BaseAddress + "api/content/whatever"
var message = result.Content.ReadAsStringAsync().Result;
}
如果我调试单元测试,并进入它, 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]],标题:{Content-Type:application / json; charset = 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;
如果启用跟踪并添加Trace.Listeners.Add(new ConsoleTraceListener())
,则会收到更详细的错误消息。 但是,您的问题很可能与您尝试序列化的对象无法序列化有关。
添加到Startup
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
其中config
是HttpConfiguration
类型。
这一行代码为我解决了同样的问题 - 现在我可以看到所有内部异常的细节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.