-2

I'm trying to run a simple restfull server in a simple asp.net application without the mvc(following this tutorial : http://www.codeproject.com/Articles/769671/Web-API-without-MVC (which will trun into a online portal).

here is my class :

public class Foods
{
public string FoodName { get; set; }
public string Price { get; set; }
public string Type { get; set; }
public string Content { get; set; }
public Foods()
{
}
}

and here is my controller:

public class FoodController : ApiController
{
public List<Foods> _productList;

public List<Foods> GetProductList()
{
    _productList = new List<Foods>{
       new Foods{FoodName= "pizza",Content= "bread cheese",Type="Main",Price="100"},
       new Foods{FoodName= "rice",Content= "rice and ...",Type="Main",Price="100"}
    };
    return _productList;
}
 }

and here is my asp.net page code(it is simple nothing to show):

 public partial class _Default : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
  {

    var config = new HttpSelfHostConfiguration("http://localhost:8080/");
    config.Routes.MapHttpRoute(
        "API Default", "api/{controller}/{id}",
        new { id = System.Web.Http.RouteParameter.Optional });

    using (HttpSelfHostServer server = new HttpSelfHostServer(config))
    {
        server.OpenAsync().Wait();
    }
}
}

when i run it there is no error and the blank page is shown

and here is the client which is a simple c# form with a list box and a button:

 private void button1_Click(object sender, EventArgs e)
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:8080/");
        client.DefaultRequestHeaders.Accept.Add(
           new MediaTypeWithQualityHeaderValue("application/json"));
        try
        {
            HttpResponseMessage response = client.GetAsync("api/foods").Result;
            if (response.IsSuccessStatusCode)
            {
                // Parse the response body. Blocking!
                var foods = response.Content.ReadAsAsync<IEnumerable<Foods>>().Result;
                foreach (Foods food in foods)
                {
                    string foodinfo = food.FoodName + "   " + food.Content + "    " + food.Type + "    " + food.Price;
                    listBox1.Items.Add(foodinfo);
                }
            }
        }
        catch (Exception ex)
        {
            textBox1.Text = ex.ToString();
        }
    }

but when i run the client and click the button i get this error:

System.AggregateException: One or more errors occurred.

System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:8080

user3003613
  • 37
  • 13
  • This protected SO question may give you some ideas: [No connection could be made because the target machine actively refused it?](http://stackoverflow.com/questions/2972600/no-connection-could-be-made-because-the-target-machine-actively-refused-it). – djikay Jul 16 '14 at 15:09

1 Answers1

1

There could be one or more reason for it.

1) Make sure that the port 8080 is not consumed by other application, Try running on different port.

try client.BaseAddress = new Uri("http://localhost:9090/");

2) By default, listening at a particular HTTP address requires administrator privileges. When you run the application, therefore, you might get an error: "HTTP could not register URL http://+:8080/".To avoid this error, run Visual Studio with elevated administrator permissions.

3) Make sure that both projects are running parallelly. If not do it.

Go to Solution properties -> Common properties -> Startup Project and select Multiple startup projects

PrinceT
  • 459
  • 4
  • 19
  • by using a different port i get this error:Object reference not set to an instance of an object.and i have been running the visual studio with run as administrator – user3003613 Jul 16 '14 at 06:49