3

I have several resources that I'd like to expose using the WCF Web API. I've investigated the Web API using a Web host but our services all run as Windows Services in production so it's time for me to put the tests aside and verify that everything will work as we need it. I've looked as the sample app here: http://webapicontrib.codeplex.com/SourceControl/changeset/view/2d771a4d6f6f#Samples%2fSelfHosted%2fserver%2fProgram.cs but this does not work with the current version (preview 5) because the HttpConfigurableServiceHost class is not accessible from our code.

One of the most appealing aspects of the Web API is the simple startup using MapServiceRoute and the new WebApiConfiguration. I don't see, however, a way to define the base url and port for the services. Obviously, hosting the service in IIS eliminates this because we configure this information in IIS. How can I accomplish this when hosting in a Windows Service?

SonOfPirate
  • 5,642
  • 3
  • 41
  • 97

3 Answers3

3

It's actually pretty simple. In a nutshell you need to instantiate HttpSelfHostServer and HttpSelfHostConfiguration and then call server.OpenAsync().

public void Start()
{
    _server.OpenAsync();
}

public void Stop()
{
    _server.CloseAsync().Wait();
    _server.Dispose();
}

For an example on how to do this using Windows service project template and/or Topshelf library see my blog post: http://www.piotrwalat.net/hosting-web-api-in-windows-service/

Piotr Walat
  • 971
  • 7
  • 9
  • 1
    To be clear, this is a feature of ASP.NET MVC 4 and, therefore, available to the more recent and renamed ASP.NET Web API. The original post was referring to the WCF Web API (the predecessor to the ASP.NET versions). In that version, the SelfHost namespace did not exist. – SonOfPirate Jun 12 '12 at 11:57
2

The latest version just uses HttpServiceHost. http://webapicontrib.codeplex.com/SourceControl/changeset/view/ddc499585751#Samples%2fSelfHosted%2fserver%2fProgram.cs

Ping me on twitter if you continue to have problems.

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • Sorry, no access to twitter from the office. How do I add more services? Do I need to create a new host for each or is there a way to add them to the existing host? – SonOfPirate Oct 20 '11 at 12:06
  • @SonOfPirate Yes, you need to create a new HttpServiceHost for each ServiceContract. In fact, that is exactly what MapServiceRoute does under the covers. Personally, I'm not very happy that you have to do this, but it does appear that HttpServiceHost is relatively lightweight. – Darrel Miller Oct 20 '11 at 12:55
1

This is the basic code using a console app. A Windows Service uses the same basic approach except you use the start and stop methods to start and stop the service and don't need to block.

static void Main(string[] args)
{
    var host = new HttpServiceHost(typeof(PeopleService), "http://localhost:8080/people");

    host.Open();

    foreach (var ep in host.Description.Endpoints)
    {
        Console.WriteLine("Using {0} at {1}", ep.Binding.Name, ep.Address);
    }

    Console.ReadLine();

    host.Close();
}

See this blog post.

Maurice
  • 27,582
  • 5
  • 49
  • 62
  • Same question as above, how do I support more services? – SonOfPirate Oct 20 '11 at 12:08
  • You need to create a new HttpServiceHost per service type. – Maurice Oct 20 '11 at 12:21
  • It would be nice if I could mark both answers as correct but the site won't allow it. I appreciate your help and your responses were just as valuable but Darrel did respond first and the code in the link is a bit more complete so I marked his. – SonOfPirate Oct 20 '11 at 14:37