Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stumped SelfHosting WebApi Services

Please help, I'm trying to self-host a web api.

When the same controller is hosted on a web project, and run on dev, via F5, everything works well.

However now I'm trying to self-host the same, I'm getting 411 and 404. 411, when I use Fiddler to connect, 404 when I'm attempting to connect via another library.

This is the console app that's supposed to host the service:

class Program
{        
    static int portNumber;



    static void Main(string[] args)
    {
        portNumber = 8089;

        var config = new HttpSelfHostConfiguration(
            string.Format("http://localhost:{0}", portNumber));

        config.Routes.MapHttpRoute(
                "API Default", "api/{controller}/{id}",
                new { id = RouteParameter.Optional });

        using (var server = new HttpSelfHostServer(config))
        {

            var test = new RetrieveGuidService().Execute(Unit.Instance);

            server.OpenAsync().Wait();

            Console.ReadLine();
        }
    }
}

This is what my controller looks like, it doesn't do anything it's just a test.

public class RetrieveGuidServiceController : ApiController
{
    public virtual Guid PostExecute(Unit request)
    {
        IQueryService<Unit,Guid> queryService = new RetrieveGuidService();
        return queryService.Execute(request);
    }
}

And here's how I'm attempting to access it via fiddler:

My Fiddler Pic

The same works when the service is hosted on a web project. I have followed this tutorial almost to the letter: asp.net WebApi self host tutorial which includes running nugget scripts, adding dependencies, etc.

What am I still missing?

like image 847
Alwyn Avatar asked Feb 17 '26 08:02

Alwyn


1 Answers

The 411 is because you didn't put the Content-Length header. Even if you are not sending content you need to include Content-Length: 0.

Regarding having the Controller in the correct assembly I have had inconsistent behaviour. In some projects it seems to work in other it doesn't. Not sure what I am doing wrong. I have a project here that does both Web Host and Self-Host with all the controllers in a different assembly and it works just fine.

like image 86
Darrel Miller Avatar answered Feb 20 '26 03:02

Darrel Miller