We have seen earlier creating WEBAPI selecting
WEBAPI project type, also we can create the WEBAPI using Console application.
Soon after listening the word console application we
will get a doubt that how the URL of the service is exposed with a generated
Executable file (.EXE).
Here we are going to host
the application as a Self-Hosting
1. Create new project
2. Under Visual C# select Console
application as a project type.
3. Click
on Tools -> NuGet Package Manager - > Package Manager Console.
Install
OwinselfHost package for self hosting (internet connection mandatory)
PM> Install-Package Microsoft.AspNet.WebApi.OwinSelfHost
4. If
the API is also used to consume as a cross origin, we should install CORS packages (internet connection
mandatory)
PM> Install-Package Microsoft.AspNet.WebApi.Cors
5. Add
new class to the project and name it to Startup.cs
[Name should be same for self-Hosting]
Place the below code in
Startup.cs
using System;
using Owin;
using System.Web.Http;
using System.Net.Http;
namespace firstMicroService
{
public class Startup
{
public void Configuration(IAppBuilder appBuilder)
{
HttpConfiguration CONFIG = new HttpConfiguration();
CONFIG.EnableCors();
CONFIG.Routes.MapHttpRoute(
name: "createUserApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
appBuilder.UseWebApi(CONFIG);
}
}
}
6. Create
new class with [EmployeeController.cs] which inherits
from ApiController
Place the code as shown
below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;
namespace firstMicroService
{
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Salary { get; set; }
}
[EnableCors(origins:"*", headers:"*", methods:"*")]
public class EmployeeController : ApiController
{
Employee[] employees = new Employee[]
{
new Employee{Id=0,Name="Morris",Salary="151110"},
new Employee{Id=1,Name="John", Salary="120000"},
new Employee{Id=2,Name="Chris",Salary="140000"},
new Employee{Id=3,Name="Siraj", Salary="90000"}
};
public IEnumerable<Employee> Get()
{
return employees.ToList();
}
public Employee Get(int Id)
{
try
{
return employees[Id];
}
catch (Exception)
{
return new Employee();
}
}
}
}
7. Open
Program.cs file and replace with below code.
using Microsoft.Owin.Hosting;
using System;
namespace firstMicroService
{
class Program
{
static void Main(string[] args)
{
string domainAddress = "Http://20.201.36.49/";
using (WebApp.Start(url: domainAddress))
{
Console.WriteLine("Service Hosted " + domainAddress);
System.Threading.Thread.Sleep(-1);
}
}
}
}
9. Run
the application and keep the console open.
10. Service
is written only for GET method, to test the service,
{given Domain address}/{api}/{Given
controllername}/{optional id}
In my case am testing
the service with POSTMAN REST Client,
http://20.201.36.49/api/Employee/1
Testing Service.
8 Comments
When I need a piece of advice, I will find your posts and read them.I love putting together useful information, this particular publish has got me personally a lot more info!
ReplyDeletefree game Toon Cup 2017 Game Break The Cup play Battle for the Galaxy
Great Article
ReplyDeleteDot Net Based Projects for Final Year Students
FInal Year Project Centers in Chennai
JavaScript Training in Chennai
Dot Net Training in Chennai
CORS doesn't work in this example
ReplyDeleteIn this article you shared lots of interesting information. Thanks a lot.
ReplyDeletePlacement Training in Chennai
best training and placement institute in chennai
Placement Training in Porur
soft skills training in chennai
soft skills training institutes in chennai
clinical sas training in chennai
SAS Training in Chennai
Affordable hostings at hostingsuits.com/
ReplyDeleteI knew that I'd never have to go back to the soul sucking corporate life again and I knew I could achieve that by helping others find their confidence again! That was one of my main motivating factors. Well that and working from home in my pjs
ReplyDeletekizi Games free
Jogo para menina
free online friv Games
not work in net core 3.1
ReplyDeleteThis content is written very well. Your use of formatting when making your points makes your observations very clear and easy to understand. Thank you. sellfy
ReplyDelete