Step 1: - Open Visual Studio 2015 => Go to File Menu => New => Project...
Step 2: - In the Installed Templates list, select Visual C# => Web
Step 3: - Select ASP.Net Web Application (.NET Framework) from the Web list => Type WebApi in the Name box => Click OK
Step 4: - Select Empty template from ASP.NET Templates List and Checked Web API check box => Click OK
Step 5: - Project Structure
Step 6: - Right Click on Project Root folder in Solution Explorer => Add => Click on New Folder => Type folder name Repository
Step 7: - Right Click on Repository folder in Solution Explorer => Add => Click New Items... => Expand Visual C# from left pane => Select Code =>Select Interface from middle pane => Type ITest.cs in the Name box => Click Add
Step 8: - Add following code in ITest.cs file
namespace WebApi.Repository{
public interface ITest
{
string GetMessage();
}
Step 9: - Right Click on Repository folder in Solution Explorer => Add => Click New Items... => Expand Visual C# from left pane => Select Code =>Select Class from middle pane => Type Test.cs in the Name box => Click Add
Step 10: - Add following code in Test.cs file
Note:- Here Test.cs implement the ITest.cs interface
{
public class Test : ITest
{
public string GetMessage()
{
return "Welcome to Web Api.";
}
}
Step 11: - Right Click on Controllers folder => Add => Controller... => Select MVC 5 Controller - Empty => Click Add => Type HomeController in Controller name box => Click Add
Step 12: - Add following code in HomeController file
Note:- Here we have create parameter-less HomeController constructor to initialize the object of Test class to call the GetMessage().
using WebApi.Repository;
namespace WebApi.Controllers
{
public class HomeController : ApiController
{
private readonly ITest _test;
public HomeController()
{
_test = new Test();
}
[HttpGet]
public string Index()
{
return _test.GetMessage();
}
}
We have done the simple Web Api project without using Dependency injection(DI).
Launch the project => Enter URL in browser.
http://localhost:51896/api/home/index
Note:- Port defer from system to system. Check your Port.
Till here our project work as expected. Now implement the Dependency injection(DI) in it.
Step 13: - Open HomeController file and change HomeController constructor and inject the service.
using WebApi.Repository;
namespace WebApi.Controllers
{
public class HomeController : ApiController
{
private readonly ITest _test;
public HomeController(ITest test)
{
_test = test;
}
[HttpGet]
public string Index()
{
return _test.GetMessage();
}
}
Launch the project => Enter URL in browser.
http://localhost:51896/api/home/index
Note:- This time we will received an error “An error occurred when trying to create a controller of type 'HomeController'. Make sure that the controller has a parameterless public constructor.”
This error indicates that Web Api by default not support the parameterized constructor and DI. For that we need to use one of Unity library to fix this issue.
Step 14: - Open Solution Explorer => Right Click on References => Click on Manage NuGet Packages… => Search Unity.AspNet.WebApi => Choose Unity.AspNet.WebApi from list => Click to Install
Step 15: - Open Solution Explorer => Expand App_Start folder
Note:- Here 2 file added in when we install the Unity.AspNet.WebApi DLL that is UnityWebApiActivator.cs and UnityConfig.cs
Step 16: - Open UnityConfig.cs => Go to RegisterTypes function => Do the following changes
Launch the project => Enter URL in browser.
http://localhost:51896/api/home/index
Here we have successfully implemented the Dependency injection(DI) in our Web Api project.
To read setting from web.config file we need to add one more package Unity.Configuration in our solution.
Step 17 : - Open Solution Explorer => Right Click on References => Click on Manage NuGet Packages… => Search Unity.Configuration => Choose Unity.Configuration from list => Click to Install
Step 18: - Open Web.config file => Add following code in your configuration section
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="ITest" type="WebApi.Repository.ITest, WebApi" />
<alias alias="Test" type="WebApi.Repository.Test, WebApi" />
<containers>
<container>
<register type="ITest" mapTo="Test"/>
</container>
</containers>
Step 19: - Open UnityConfig.cs => Go to RegisterTypes function => Do the following changes
Launch the project => Enter URL in browser.
http://localhost:51896/api/home/index
ALL Done
No comments:
Post a Comment