Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should OWIN self host app using Ninject OWINHost need system.web?

I'm trying to create a Windows service with OWIN self hosted WebAPI with Ninject . I got it to work but I had to add a reference to system.web, which seems wrong. Without a reference to system.web I got these compile errors:

The type 'System.Web.Routing.RouteCollection' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

The type 'System.Web.Routing.Route' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

The errors showed up after I added Ninject according to this article Setting up a OWIN WebApi application

I also had to constrain the Microsoft.Owin to version 2.1.0 for Ninject to work. During the service startup Ninject is looking for Owin 2.1.0. If you get latest on Microsoft.Owin it will roll to 3.0.

The three main NuGet packages I'm using are:

Microsoft.AspNet.WebApi.OwinSelfHost

Ninject.Web.Common.OwinHost

Ninject.Web.WebApi.OwinHost

Here's all my packages (notice the constrain on Microsoft.Owin)

  <package id="Microsoft.AspNet.WebApi" version="5.2.2" targetFramework="net45" />
  <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.Owin" version="5.2.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.OwinSelfHost" version="5.2.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.2" targetFramework="net45" />
  <package id="Microsoft.Owin" version="2.1.0" targetFramework="net45" allowedVersions="(,2.1]" />
  <package id="Microsoft.Owin.Host.HttpListener" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.Owin.Hosting" version="2.0.2" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
  <package id="Ninject" version="3.2.2.0" targetFramework="net45" />
  <package id="Ninject.Extensions.ContextPreservation" version="3.2.0.0" targetFramework="net45" />
  <package id="Ninject.Extensions.NamedScope" version="3.2.0.0" targetFramework="net45" />
  <package id="Ninject.Web.Common" version="3.2.2.0" targetFramework="net45" />
  <package id="Ninject.Web.Common.OwinHost" version="3.2.2.0" targetFramework="net45" />
  <package id="Ninject.Web.WebApi" version="3.2.1.0" targetFramework="net45" />
  <package id="Ninject.Web.WebApi.OwinHost" version="3.2.1.0" targetFramework="net45" />
  <package id="Owin" version="1.0" targetFramework="net45" />

Here's what the Program.cs of the windows service looks like

using Microsoft.Owin.Hosting;
using Ninject;
using Ninject.Web.Common.OwinHost;
using Ninject.Web.WebApi.OwinHost;
using Owin;
using System.Reflection;
using System.ServiceProcess;
using System.Web.Http;


namespace ServiceExample
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new Service1() 
            };


            using (WebApp.Start<Startup>("http://localhost:12345"))
            {
                ServiceBase.Run(ServicesToRun);  
            }
        }



        public class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                ConfigureWebAPI(app);

            }

            private void ConfigureWebAPI(IAppBuilder app)
            {
                var config = new HttpConfiguration();
                config.Routes.MapHttpRoute(
                    "DefaultApi",
                    "api/{controller}/{id}",
                    new {id = RouteParameter.Optional});

                app.UseNinjectMiddleware(CreateKernel).UseNinjectWebApi(config);
            }


            private static StandardKernel CreateKernel()
            {
                var kernel = new StandardKernel();
                kernel.Load(Assembly.GetExecutingAssembly());
                return kernel;
            }
        }
    }
}
like image 435
HungryPipo Avatar asked Sep 05 '14 15:09

HungryPipo


2 Answers

Try to remove System.Web.Http.WebHost.dll from references.

references list with System.Web.Http.WebHost.dll


I had the same problem when I wanted to have an API project as class library and separate host projects (one using console and other IIS) as described in this great blog post

The problem was that when I executed Install-Package Microsoft.AspNet.WebApi to install ASP.NET Web API it also added reference to System.Web.Http.WebHost.dll and the result packages.json was:

<packages>
  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
</packages>

so the solution was to remove reference to System.Web.Http.WebHost.dll and delete Microsoft.AspNet.WebApi and Microsoft.AspNet.WebApi.WebHost from packages.json so it won't install again when some will do nuget restore/reinstall:

<packages>
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
</packages>

And this is my packages.json from working class library ASP.NET Web API project:

<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net452" />  
<package id="Microsoft.Owin" version="3.0.1" targetFramework="net452" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
<package id="Owin" version="1.0" targetFramework="net452" />
like image 74
Mariusz Pawelski Avatar answered Nov 13 '22 22:11

Mariusz Pawelski


I came across with the same problem as you, but without usign Ninject OwinHost, and indeed I think that the problem is not with Ninject but the Routing.

The problem is this code:

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

that uses System.Web.Routing wich is contained on System.Web namespace (Check here).

So, the alternative to this that I've found is using rooting with attributes. So you can replace the code above by this:

// Web API routes
config.MapHttpAttributeRoutes();

If you are not familiar with it, check this tutorial.

Using this approach you can delete the System.Web reference of your project.

like image 45
Xavier Egea Avatar answered Nov 14 '22 00:11

Xavier Egea