c# – 调试自托管的WebApi应用程序
作者:互联网
我有一个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 /,那么我的控制器操作就会被命中.
然而,
我有一个测试项目,SelfHosts这个api
using (var client = new HttpClient(Server))
{
var result = client.PostAsync(BaseAddress + "api/content/whatever"
var message = result.Content.ReadAsStringAsync().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; charset=utf-8
}}
无益,消息实际上是公正的
An error has occurred
有没有办法我可以调试自我托管的WebApi来找出导致此错误的原因?
建立
服务器只是一个来自基类的HttpServer,它拥有我自己托管的服务器,如下所示:
var httpConfig = new HttpSelfHostConfiguration(BaseAddress);
new ApiServiceConfiguration(httpConfig).Configure();
var server = new HttpSelfHostServer(httpConfig);
server.OpenAsync().Wait();
Server = server;
解决方法:
如果启用跟踪并添加Trace.Listeners.Add(新的ConsoleTraceListener()),则会收到更详细的错误消息.但是,您的问题很可能与您尝试序列化的对象无法序列化有关.
标签:c,asp-net,debugging,asp-net-web-api,self-hosting 来源: https://codeday.me/bug/20190624/1281553.html