Selfhost with WebApi

123 views
Skip to first unread message

Dan Dorey

unread,
Jan 27, 2015, 2:02:00 PM1/27/15
to nin...@googlegroups.com
Hi,

I'm trying to get Ninject to work with a project that is using a self hosted WebApi.

I've installed the nuget package Microsoft ASP.NET Web API 2.2 Self Host (Microsoft.AspNet.WebApi.SelfHost) along with Ninject.Web.Common.Selfhost.

The console app seems to load up correctly, but I only seem to get a connection error when hitting my URL (http://localhost:8081/api/hello). I have a similar test app that is not using Ninject that works correctly.


Am I missing something to get this going? I originally tried using the package Microsoft.AspNet.WebApi.OwinSelfHost but could not get it functioning because of various errors. If this is what I need to be using, I can revisit this.


public class HelloController : ApiController
    {
        public string Get()
        {
            return "Hello, world!";
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var webApiConfiguration = new HttpSelfHostConfiguration("http://localhost:8081");
            webApiConfiguration.Routes.MapHttpRoute(
                        name: "DefaultApi",
                        routeTemplate: "api/{controller}/{id}",
                        defaults: new { id = RouteParameter.Optional });
            var mSelfHost = new NinjectSelfHostBootstrapper(CreateKernel, webApiConfiguration);
            mSelfHost.Start();
            Console.ReadLine();
        }
        private static IKernel CreateKernel()
        {
            var mKernel = new StandardKernel();
            mKernel.Load(Assembly.GetExecutingAssembly());
            return mKernel;
        }
    }


Here are the packages I have installed:
<packages>
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.SelfHost" version="5.2.2" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" />
  <package id="Ninject" version="3.2.2.0" targetFramework="net45" />
  <package id="Ninject.Web.Common" version="3.2.3.0" targetFramework="net45" />
  <package id="Ninject.Web.Common.Selfhost" version="3.2.3.0" targetFramework="net45" />
</packages>



This e-mail (including any attachments) is for the sole use of the intended recipient(s) and may contain information that is confidential and/or protected by legal privilege. Any unauthorized review, use, copy, disclosure or distribution of this e-mail is strictly prohibited. If you are not the intended recipient, please notify Mitel immediately and destroy all copies of this e-mail.  Mitel does not accept any liability for breach of security, error or virus that may result from the transmission of this message.
NinjectWinApiTest.zip

Dan Dorey

unread,
Jan 28, 2015, 3:13:42 PM1/28/15
to nin...@googlegroups.com
This was caused by the order I was adding the nuget packages. When I added ninject first, it automatically added the appropriate webapi packages and everything worked as expected.

Michael

unread,
Jan 28, 2015, 4:00:46 PM1/28/15
to nin...@googlegroups.com


On Wednesday, January 28, 2015 at 2:13:42 PM UTC-6, Dan Dorey wrote:
This was caused by the order I was adding the nuget packages. When I added ninject first, it automatically added the appropriate webapi packages and everything worked as expected.

I could be wrong, haven't verified it myself, but it sounds like possible there is a Nuget package version that is mismatched, version range or other, etc. Probably could verify there is a package that needs to get updated in the process, whichever one got added first. Maintainers really should pay closer attention to these sorts of dependencies, but I find from experience that they don't all do this.
Reply all
Reply to author
Forward
0 new messages