How do you access the Configuration in SelfHost so as to access the DependencyResolver to in turn get a service from Ninject.
In a typical Web Api you use GlobalConfiguration.Configuration.DependencyResolver.
In SelfHost GlobalConfiguration is not accessible.
Essentially using Ninject I need to get an instance of a service for injection but need to access the configuration. Again this is relevant to SelfHost NOT IIS Hosted which you can use the above as I indicated.
Basically the question in short is: How do you access GlobalConfiguration in Web Api Self Host or how do your get a reference in a Self Host Web Api to the Configuration.
When you create your self-hosted API, you create an instance of HttpSelfHostConfiguration, which inherits from HttpConfiguration, and it's this object that is the equivalent of GlobalConfiguration.Configuration in an ASP.NET-hosted scenario
eg.
// Create your self-host configuration
var
configuration = newHttpSelfHostConfiguration(uriBuilder.Uri);
configuration.Routes.MapHttpRoute(
name: "Default Route",
routeTemplate: "api/{controller}/{id}",
defaults: new
{ id = RouteParameter.Optional
}
);
// this configuration object can be used to specify your dependency resolver:
configuration.DependencyResolver = new
MyNinjectWebApiDependencyResolver(kernel);
var server = newHttpSelfHostServer(configuration);
// Start the server etc...
Note: (apologies if you already know this bit - I didn't and it took me ages to find the solution) The resolver you construct above is not the same as the resolver you might have created for MVC3 - it implements a different IDependencyResolver
interface! See here for a sample resolver you can use to pass in above:
http://www.strathweb.com/2012/05/using-ninject-with-the-latest-asp-net-web-api-source/
chazelton
Member
102 Points
53 Posts
Self Host Web Api And Ninect
Sep 03, 2012 08:05 PM|LINK
How do you access the Configuration in SelfHost so as to access the DependencyResolver to in turn get a service from Ninject.
In a typical Web Api you use GlobalConfiguration.Configuration.DependencyResolver.
In SelfHost GlobalConfiguration is not accessible.
Essentially using Ninject I need to get an instance of a service for injection but need to access the configuration. Again this is relevant to SelfHost NOT IIS Hosted which you can use the above as I indicated.
Thanks.
chazelton
Member
102 Points
53 Posts
Re: Self Host Web Api And Ninect
Sep 03, 2012 10:59 PM|LINK
BUMP
Basically the question in short is: How do you access GlobalConfiguration in Web Api Self Host or how do your get a reference in a Self Host Web Api to the Configuration.
sicnarf
Member
2 Points
1 Post
Re: Self Host Web Api And Ninect
Nov 27, 2012 11:12 AM|LINK
I came up against this same problem:
When you create your self-hosted API, you create an instance of HttpSelfHostConfiguration, which inherits from HttpConfiguration, and it's this object that is the equivalent of GlobalConfiguration.Configuration in an ASP.NET-hosted scenario
eg.
// Create your self-host configuration
var configuration = new HttpSelfHostConfiguration(uriBuilder.Uri);
configuration.Routes.MapHttpRoute(
name: "Default Route",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// this configuration object can be used to specify your dependency resolver:
configuration.DependencyResolver = new MyNinjectWebApiDependencyResolver(kernel);
var server = new HttpSelfHostServer(configuration);
// Start the server etc...
Note: (apologies if you already know this bit - I didn't and it took me ages to find the solution) The resolver you construct above is not the same as the resolver you might have created for MVC3 - it implements a different IDependencyResolver interface! See here for a sample resolver you can use to pass in above: http://www.strathweb.com/2012/05/using-ninject-with-the-latest-asp-net-web-api-source/