tldr; Get the code from my Github repository here: https://github.com/devdaves/ExampleWebApi
Start a new ASP.NET Web Application
Select the Empty template. Don't check any of the core references (Web Forms, MVC or Web API).
Once the project is ready to go, open the package manager console and run the following commands to install the necessary nuget packages.
- install-package Microsoft.AspNet.WebApi.Owin
- install-package Microsoft.Owin.Host.SystemWeb
- install-package Castle.Windsor
Open the web.config and add the following code:
In the Solution Explorer create a Startup class at the root of the site with the following code:
In the Solution Explorer create a Models folder and inside the Models folder create a Contact class with the following code:
In the Solution Explorer create a Controllers folder and inside the Controllers folder create a Web API controller called ContactsController with the following code:
At this point you have enough to run the application and should be able to go to http://localhost:####/contacts in your browser and see the contact returned.
Now its time to add Windsor. In the Solution Explorer add a new folder called Windsor. Create a new class called WindsorDependencyScope with the following code:
Add a new class to the Windsor folder called WindsorHttpDependencyResolver with the following code:
In the Windsor folder create a new folder called Installers. Create a new class in the Installers folder called ControllerInstaller with the following code:
Create another new class in the Windsor/Installers folder called DefaultInstaller with the following code:
Now we need to make the final changes to the Startup class we created in the root of the site. Make it look like this:
Now that Windsor is setup lets add a contact repository and add that as a dependency to the controller so we can see Windsor in action.
In the Solution Explorer add a folder called Repository. In the Repository folder add a new interface called IContactsRepository with the following code:
Add a class called ContactsRepository to the Repository folder with the following code:
Now lets go add the dependency to the ContactsController, make the ContactsController code look like this:
That should be it. Running the site now everything should work.