locked
WebAPI OWIN Self-Hosted Problem RRS feed

  • Question

  • User1569366455 posted

    Hi,

    I'm trying to develop a WebAPI that will be self-hosted using OWIN, that will be running within a Windows service.  For those interested, we chose this deployment for easier installation at our customers sites.  This is the first time I'm trying to implement a self-hosted solution.

    I was getting the following type of error:

    {"Message":"No HTTP resource was found that matches the request URI 'http://localhost:9000/api/HelloWorld'.","MessageDetail":"No type was found that matches the controller named 'HelloWorld'."}

    So, I thought lets make a simpler project with a simple console application.  But, I'm getting the same error.

    I'm currently using Visual Studio 2017, .Net Framework 4.6.2, Microsoft.AspNet.WebApi.OwinSelfHost(and dependent NuGet packages).

    Here's the code, that I'm using for testing:

    using Microsoft.Owin.Hosting;
    using Owin;
    using System;
    using System.Web.Http;
    
    namespace FiloptoServer
    {
        class Program
        {
            public static void Main(string[] args)
            {
                WebApp.Start<StartOwin>("http://localhost:9000");
                Console.ReadLine();
            }
    
            public class StartOwin
            {
                public void Configuration(IAppBuilder appBuilder)
                {
                    var config = new HttpConfiguration();
                    config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                    );
                    appBuilder.UseWebApi(config);
                }
            }
    
            public class HelloWorldController : ApiController
            {
                public string Get()
                {
                    return "Hello, World!";
                }
            }
        }
    }
    

    Any help would be appreciated.

    Monday, June 26, 2017 11:09 PM

Answers

  • User1569366455 posted

    Thanks for the information.

    I noticed what my error was.  I had the ApiController class declared inside the program class.  Once it was outside the program class, it worked as expected.  It still didn't help me with the Windows Service project, which was still giving me that error and the ApiController class was defined in it's own .cs file.  Anyway, I did find the TopShelf library that did permit me to transform the console application into a Windows Service application.  So, now I have a self-hosted Owin Web API running inside a Windows service, which was what I wanted.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 27, 2017 7:29 PM

All replies

  • User991499041 posted

    Hi DanielRail,

    You can modify like this.

    string baseAddress = "http://localhost:9000/";
    
    // Start OWIN host 
    using (WebApp.Start<StartOwin>(url: baseAddress))
    {
    	// Create HttpCient and make a request to api/values 
    	HttpClient client = new HttpClient();
    
    	var response = client.GetAsync(baseAddress + "api/HelloWorld").Result;
    
    	Console.WriteLine(response);
    	Console.WriteLine(response.Content.ReadAsStringAsync().Result);
    	Console.ReadLine();
    }

    Regards,

    zxj

    Tuesday, June 27, 2017 5:52 AM
  • User1569366455 posted

    Thanks for the information.

    I noticed what my error was.  I had the ApiController class declared inside the program class.  Once it was outside the program class, it worked as expected.  It still didn't help me with the Windows Service project, which was still giving me that error and the ApiController class was defined in it's own .cs file.  Anyway, I did find the TopShelf library that did permit me to transform the console application into a Windows Service application.  So, now I have a self-hosted Owin Web API running inside a Windows service, which was what I wanted.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 27, 2017 7:29 PM