Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Self hosting Web Api service into Windows Forms

I am trying to self host a Web Api service inside a windows forms application using the code below

namespace MascoteAquarium.Desktop
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            var config = new HttpSelfHostConfiguration("http://localhost:8080");
            config.Routes.MapHttpRoute(
                "DefaultApi", "api/{controller}/id", new { id = RouteParameter.Optional });

            using (HttpSelfHostServer server = new HttpSelfHostServer(config))
            {
                server.OpenAsync().Wait();
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmMainMenu());
        }
    }
}

When I try

http://localhost:8080/api/*(some-controller)* 

I receive a NullReferenceException at System.Web.Http.SelfHost.HttpSelfHostServer.ProcessRequestContext(ChannelContext channelContext, RequestContext requestContext)

Someone know what is happening? Is it possible to self host inside a Win Forms app?

like image 205
Andrey Avatar asked Jan 13 '13 15:01

Andrey


People also ask

Can we use Web API in Webforms?

Although ASP.NET Web API is packaged with ASP.NET MVC, it is easy to add Web API to a traditional ASP.NET Web Forms application. To use Web API in a Web Forms application, there are two main steps: Add a Web API controller that derives from the ApiController class. Add a route table to the Application_Start method.

Can Web API be hosted in windows service?

You can host a Web API as separate process than ASP.NET. It means you can host a Web API in console application or windows service or OWIN or any other process that is managed by . NET framework.

Can Web API be self hosted?

This tutorial shows how to host a web API inside a console application. ASP.NET Web API does not require IIS. You can self-host a web API in your own host process. New applications should use OWIN to self-host Web API.


2 Answers

The problem is that the HttpSelfHostServer object gets lost just before Application.Run(...), which contains the main event loop that keeps your program running. The using statement makes sure that Dispose method gets called for the object, in this case server, thus making it unavailable for answering requests, resulting in the NullReferenceException you are experiencing.

To fix the exception, your code should look like this:

...
using (HttpSelfHostServer server = new HttpSelfHostServer(config))
{
    server.OpenAsync().Wait();
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new frmMainMenu());
}
...
like image 91
niofis Avatar answered Sep 22 '22 10:09

niofis


  1. You need to run the WinForms application (or VS if you run the WinForm app from the debugger) with elevated privileges (as Admin), otherwise the self-host will not be allowed to open a port.

  2. Make sure no other application is running on port 8080 already

like image 30
Filip W Avatar answered Sep 21 '22 10:09

Filip W