I am following the self hosting tutorial. I have created a simple controller that returns a string. When I debug into the code, I get into the Get() method and the string is returned. However, the client always receives a 500 status code.
Is there some tracing I can turn on to see what is going on inside to cause this to happen... it is quite annoying...
I gues this question could also be answered like this... How tightly is the new ASP.NET Web API tied to WCF? Is it using WCF underneath and so the normal WCF tracing stuff works like it always has?
Since you are using SelfHost, you can use System.Net's tracing. However the traces generated by this is not so readable immediately. Instead you could use the tool called Http Fiddler which is very easy to use. Can you show us how your response looks like(from
Fiddler)? Btw, if you use WebHost, then System.Net tracing will not work.
I'm looking for server side tracing. Fiddler just gives me the result. I need to know what is going on inside the server.
Fiddler output:
HTTP/1.1 500 Internal Server Error
Content-Length: 0
Server: Microsoft-HTTPAPI/2.0
Date: Mon, 20 Feb 2012 02:00:07 GMT
EDIT: below is the System.Net trace... It shows the request coming in, but then never returns the content. Below is my setup... no different than the tutorial. I've also included my setup code.
var baseAddress = "http://localhost:8080/";
var config = new HttpSelfHostConfiguration(baseAddress);
config.Routes.MapHttpRoute(
name: "default",
routeTemplate: "api/{controller}/{id}",
defaults: new { controller = "Home", id = RouteParameter.Optional });
Self host code was in previous message. I'm trying to convey that there is nothing different about my code from the tutorial published here. So, there must be something going on internally with my system and I'm just trying to figure out how to diagnose
the issue.
var baseAddress = "http://localhost:8080/";
var config = new HttpSelfHostConfiguration(baseAddress);
config.Routes.MapHttpRoute(
name: "default",
routeTemplate: "api/{controller}/{id}",
defaults: new { controller = "Home", id = RouteParameter.Optional });
to see if the response you get includes any exception details? If you still get a 500 with an empty body then it's very possible that there was an issue during response serialization. You could try attaching a debugger to your server to see where any exceptions
are being thrown.
Craig, this is what I have that your code looks like and that should be fine:
class Program
{
static void Main(string[] args)
{
var baseAddress = "http://localhost:8080/";
HttpSelfHostServer server = null;
try
{
var config = new HttpSelfHostConfiguration(baseAddress);
config.Routes.MapHttpRoute(
name: "default",
routeTemplate: "api/{controller}/{id}",
defaults: new { controller = "Home", id = RouteParameter.Optional });
server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();
Console.WriteLine("Hit ENTER to exit");
Console.ReadLine();
}
finally
{
if (server != null)
{
server.CloseAsync().Wait();
}
}
}
}
public class HomeController : ApiController
{
public string Get()
{
return "Hello World";
}
}
If this doesn't work then something strange is going on. One thing to try is turning on exceptions and run it in debug (see under the Debug/Exceptions menu and then check "CLR exceptions"). That should give you what the exception is.
Yes, I have turned on exceptions and set the include errors to always. It simply is breaking somewhere inside and not throwing. I'll pull out ilspy and see if I can track it down.
As you mentioned, if it is erroring rendering the response, wouldn't that throw an exception? Perhaps I'll start messing with the media type formatters and see if something odd is going on in there.
So, I'm on a different computer and still am having no luck. I made a simple one file program as pasted below. It finds the controller and executes the Get method, but still returns a 500. The only common thing is that I pulled the bits down from Nuget...
Is it possible there is a nuget package problem?
Ok, I got it. Apparently, System.Json is a required assembly. It is not, however, apart of the nuget package dependencies. I'd suggest you update the package immediately to include the nuget package JsonValue as a required dependency.
The stack traces were pointing at System.Net.Http.FormattingUtilities as the culprit for all my issues. It obviously references System.Json and since it didn't exist on my system, it was blowing up.
Thanks for helping me track this down.
Marked as answer by craiggwilson on Feb 20, 2012 07:08 PM
craiggwilson
Member
10 Points
6 Posts
500 Errors
Feb 19, 2012 10:57 PM|LINK
I am following the self hosting tutorial. I have created a simple controller that returns a string. When I debug into the code, I get into the Get() method and the string is returned. However, the client always receives a 500 status code.
Is there some tracing I can turn on to see what is going on inside to cause this to happen... it is quite annoying...
I gues this question could also be answered like this... How tightly is the new ASP.NET Web API tied to WCF? Is it using WCF underneath and so the normal WCF tracing stuff works like it always has?
Kiran Challa
Participant
1460 Points
285 Posts
Microsoft
Re: 500 Errors
Feb 20, 2012 12:53 AM|LINK
Kiran Challa
craiggwilson
Member
10 Points
6 Posts
Re: 500 Errors
Feb 20, 2012 01:01 AM|LINK
I'm looking for server side tracing. Fiddler just gives me the result. I need to know what is going on inside the server.
Fiddler output:
HTTP/1.1 500 Internal Server Error
Content-Length: 0
Server: Microsoft-HTTPAPI/2.0
Date: Mon, 20 Feb 2012 02:00:07 GMT
EDIT: below is the System.Net trace... It shows the request coming in, but then never returns the content. Below is my setup... no different than the tutorial. I've also included my setup code.
var baseAddress = "http://localhost:8080/";
var config = new HttpSelfHostConfiguration(baseAddress);
config.Routes.MapHttpRoute(
name: "default",
routeTemplate: "api/{controller}/{id}",
defaults: new { controller = "Home", id = RouteParameter.Optional });
_server = new HttpSelfHostServer(config);
_server.OpenAsync().Wait();
System.Net Information: 0 : [6576] Current OS installation type is 'Client'.
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::HttpListener()
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpListener#2256335::HttpListener()
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::Prefixes_get()
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::AddPrefix(uriPrefix:http://+:8080/)
System.Net.Sockets Verbose: 0 : [2612] DNS::GetHostEntry()
System.Net.Sockets Verbose: 0 : [2612] DNS::GetHostByName()
System.Net.Sockets Verbose: 0 : [2612] Exiting DNS::GetHostByName() -> IPHostEntry#35588587
System.Net.Sockets Verbose: 0 : [2612] Exiting DNS::GetHostEntry() -> IPHostEntry#35588587
System.Net.HttpListener Information: 0 : [2612] ServiceNameStore#45814213::Add() adding default SPNs 'HTTP/CraigWilson-PC' from prefix 'http://+:8080/'
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpListener#2256335::AddPrefix() -> prefix:http://+:8080/
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::Start()
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpListener#2256335::Start()
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext()
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext(IAsyncResult#45763068)
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext()
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext(IAsyncResult#40392858)
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext()
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext(IAsyncResult#13391695)
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext()
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext(IAsyncResult#63950773)
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext()
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext(IAsyncResult#3944845)
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext()
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext(IAsyncResult#11555583)
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext()
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext(IAsyncResult#5376733)
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext()
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext(IAsyncResult#27686112)
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext()
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext(IAsyncResult#21360616)
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext()
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext(IAsyncResult#28272190)
System.Net.HttpListener Information: 0 : [2612] HttpListenerContext#15790037::.ctor(httpListener#2256335 requestBlob=43163804)
System.Net.HttpListener Information: 0 : [2612] HttpListenerRequest#47341188::.ctor(httpContext#15790037 memoryBlob# 43163804)
System.Net.HttpListener Information: 0 : [2612] Associating HttpListenerRequest#47341188 with HttpListenerContext#15790037
System.Net.HttpListener Information: 0 : [2612] HttpListenerRequest#47341188::.ctor(httpContext#15790037 RequestUri:http://localhost:8080/api Content-Length:0 HTTP Method:GET)
System.Net.HttpListener Information: 0 : [2612] HttpListenerRequest#47341188::.ctor(HttpListenerRequest Headers:
Accept : application/json
Host : localhost:8080
User-Agent : Fiddler
)
System.Net.HttpListener Warning: 0 : [2612] HttpListener#2256335::HandleAuthentication() - Received a request with an unmatched or no authentication scheme. AuthenticationSchemes:Anonymous, Authorization:<null>.
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::EndGetContext(IAsyncResult#45763068)
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpListener#2256335::EndGetContext() -> HttpListenerContext#15790037 RequestTraceIdentifier#00000000-0000-0000-0200-0080030000fd
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext()
System.Net.HttpListener Verbose: 0 : [2612] HttpListener#2256335::BeginGetContext(IAsyncResult#30966680)
System.Net.HttpListener Verbose: 0 : [2612] HttpListenerContext#15790037::Response()
System.Net.HttpListener Information: 0 : [2612] HttpListenerResponse#26679708::.ctor()
System.Net.HttpListener Information: 0 : [2612] Associating HttpListenerResponse#26679708 with HttpListenerContext#15790037
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpListenerContext#15790037::Response()
System.Net.HttpListener Verbose: 0 : [2612] HttpListenerContext#15790037::Response()
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpListenerContext#15790037::Response()
System.Net.HttpListener Verbose: 0 : [2612] HttpListenerRequest#47341188::InputStream_get()
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpListenerRequest#47341188::InputStream_get()
System.Net.HttpListener Verbose: 0 : [2612] HttpResponseStream#49906006::Close()
System.Net.HttpListener Verbose: 0 : [2612] HttpListenerContext#15790037::Response()
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpListenerContext#15790037::Response()
System.Net.HttpListener Verbose: 0 : [2612] HttpListenerContext#15790037::Response()
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpListenerContext#15790037::Response()
System.Net.HttpListener Verbose: 0 : [2612] HttpListenerContext#15790037::Response()
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpListenerContext#15790037::Response()
System.Net.HttpListener Verbose: 0 : [2612] HttpListenerContext#15790037::Response()
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpListenerContext#15790037::Response()
System.Net.HttpListener Verbose: 0 : [2612] HttpListenerContext#15790037::Response()
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpListenerContext#15790037::Response()
System.Net.HttpListener Verbose: 0 : [2612] HttpListenerContext#15790037::Response()
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpListenerContext#15790037::Response()
System.Net.HttpListener Verbose: 0 : [2612] HttpListenerContext#15790037::Response()
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpListenerContext#15790037::Response()
System.Net.HttpListener Information: 0 : [2612] HttpListenerResponse#26679708::.ctor(HttpListenerResponse Headers:
Content-Length : 0
)
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpResponseStream#49906006::Dispose()
System.Net.HttpListener Verbose: 0 : [2612] HttpListenerContext#15790037::Response()
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpListenerContext#15790037::Response()
System.Net.HttpListener Verbose: 0 : [2612] HttpListenerResponse#26679708::Close()
System.Net.HttpListener Verbose: 0 : [2612] HttpResponseStream#49906006::Close()
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpResponseStream#49906006::Close()
System.Net.HttpListener Verbose: 0 : [2612] HttpListenerContext#15790037::Close()()
System.Net.HttpListener Verbose: 0 : [2612] HttpListenerResponse#26679708::Close()
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpListenerResponse#26679708::Close()
System.Net.HttpListener Verbose: 0 : [2612] HttpListenerRequest#47341188::Close()
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpListenerRequest#47341188::Close()
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpListenerContext#15790037::Close()
System.Net.HttpListener Verbose: 0 : [2612] Exiting HttpListenerResponse#26679708::Close()
Henrik Fryst...
Member
96 Points
13 Posts
Microsoft
Re: 500 Errors
Feb 20, 2012 01:59 AM|LINK
waht does your selfhost code and controller look like?
craiggwilson
Member
10 Points
6 Posts
Re: 500 Errors
Feb 20, 2012 02:06 AM|LINK
Self host code was in previous message. I'm trying to convey that there is nothing different about my code from the tutorial published here. So, there must be something going on internally with my system and I'm just trying to figure out how to diagnose the issue.
var baseAddress = "http://localhost:8080/";
var config = new HttpSelfHostConfiguration(baseAddress);
config.Routes.MapHttpRoute(
name: "default",
routeTemplate: "api/{controller}/{id}",
defaults: new { controller = "Home", id = RouteParameter.Optional });
_server = new HttpSelfHostServer(config);
_server.OpenAsync().Wait();
Controller:
marcind
Contributor
3344 Points
609 Posts
Microsoft
Re: 500 Errors
Feb 20, 2012 03:00 AM|LINK
Can you try setting
to see if the response you get includes any exception details? If you still get a 500 with an empty body then it's very possible that there was an issue during response serialization. You could try attaching a debugger to your server to see where any exceptions are being thrown.
ASP.NET Team
@marcind
Blog
Henrik Fryst...
Member
96 Points
13 Posts
Microsoft
Re: 500 Errors
Feb 20, 2012 04:37 AM|LINK
Craig, this is what I have that your code looks like and that should be fine:
class Program { static void Main(string[] args) { var baseAddress = "http://localhost:8080/"; HttpSelfHostServer server = null; try { var config = new HttpSelfHostConfiguration(baseAddress); config.Routes.MapHttpRoute( name: "default", routeTemplate: "api/{controller}/{id}", defaults: new { controller = "Home", id = RouteParameter.Optional }); server = new HttpSelfHostServer(config); server.OpenAsync().Wait(); Console.WriteLine("Hit ENTER to exit"); Console.ReadLine(); } finally { if (server != null) { server.CloseAsync().Wait(); } } } } public class HomeController : ApiController { public string Get() { return "Hello World"; } }If this doesn't work then something strange is going on. One thing to try is turning on exceptions and run it in debug (see under the Debug/Exceptions menu and then check "CLR exceptions"). That should give you what the exception is.
Thanks,
Henrik
craiggwilson
Member
10 Points
6 Posts
Re: 500 Errors
Feb 20, 2012 10:37 AM|LINK
Yes, I have turned on exceptions and set the include errors to always. It simply is breaking somewhere inside and not throwing. I'll pull out ilspy and see if I can track it down.
As you mentioned, if it is erroring rendering the response, wouldn't that throw an exception? Perhaps I'll start messing with the media type formatters and see if something odd is going on in there.
Thanks for your help...
craiggwilson
Member
10 Points
6 Posts
Re: 500 Errors
Feb 20, 2012 06:54 PM|LINK
So, I'm on a different computer and still am having no luck. I made a simple one file program as pasted below. It finds the controller and executes the Get method, but still returns a 500. The only common thing is that I pulled the bits down from Nuget... Is it possible there is a nuget package problem?
craiggwilson
Member
10 Points
6 Posts
Re: 500 Errors
Feb 20, 2012 07:08 PM|LINK
Ok, I got it. Apparently, System.Json is a required assembly. It is not, however, apart of the nuget package dependencies. I'd suggest you update the package immediately to include the nuget package JsonValue as a required dependency.
The stack traces were pointing at System.Net.Http.FormattingUtilities as the culprit for all my issues. It obviously references System.Json and since it didn't exist on my system, it was blowing up.
Thanks for helping me track this down.