Sivakumar

Sivakumar

  • NA
  • 551
  • 211.4k

I am Implementing console application using web api

Sep 7 2015 1:37 AM
Hi,
 
I am Implementing Web API console application.
 
but when i debug get an error like this :
 
 
 
An unhandled exception of type 'System.AggregateException' occurred in mscorlib.dll

Additional information: One or more errors occurred.
 
This is my code :
 
Book.cs :
 
public class Book
{
public int Id { get; set; }

public string Name { get; set; }

public string Author { get; set; }

public decimal Rating { get; set; }
}
 
and BooksController.cs :
 
public class BooksController
{
Book[] books = new Book[]

{

new Book { Id = 1, Name = "Sagar", Author = "seventh", Rating = 5 },

new Book { Id = 2, Name = "Sachin", Author = "seventh", Rating = 4 },

new Book { Id = 3, Name = "Dravid", Author = "seventh", Rating = 3 },

new Book { Id = 4, Name = "Ramesh", Author = "seventh", Rating = 2 },

new Book { Id = 5, Name = "Ganguly", Author = "seventh", Rating = 1 },

new Book { Id = 6, Name = "Rahul", Author = "seventh", Rating = 6 },

};
public IEnumerable<Book> GetAllBooks()
{

return books;

}
public Book GetBookById(int id)
{

var book = books.FirstOrDefault((p) => p.Id == id);

if (book == null)
{

throw new HttpResponseException(HttpStatusCode.NotFound);

}

return book;

}



public IEnumerable<Book> GetBooksByStandard(string author)
{

return books.Where(p => string.Equals(p.Author, author,

StringComparison.OrdinalIgnoreCase));

}
}
}
and my program.cs is :
 
class Program
{
static void Main(string[] args)
{

var config = new HttpSelfHostConfiguration("http://localhost:8080");



//To determine which action to invoke, the framework uses a routing table. The Visual Studio project template for Web API creates a default route

config.Routes.MapHttpRoute(

"API Default", "api/{controller}/{id}",

new { id = RouteParameter.Optional });



using (HttpSelfHostServer server = new HttpSelfHostServer(config))
{

server.OpenAsync().Wait();



Console.WriteLine("Listening for HTTP requests.");

Console.WriteLine("(Please Run the ClientApp project to send requests).");

Console.WriteLine();

Console.WriteLine("You can hit Enter to quit.");

Console.ReadLine();

}

}

}
}
Please give me a solution for that..
Thank you