I am trying to use a three-layer architecture in MVC MVC UI -> Services -> Objects
I use the asp.net built-in web development server for this, in which I configured port 4515 to run the user interface level, and I make an ajax call to access the webapi service at the service level, which is configured on port 35420.and when I make the call ajax, I get an error as shown below with status 500
System.Net.Sockets.SocketException: The connection could not be completed because the target machine actively rejected it 127.0.0.1:32450.
When I looked at IIS, only 4515 works, and service level 35420 is not running.
How to get rid of this problem? And I want my service level to work in parallel when my user interface layer is working? Is there a way that I can make a better decision for this?
And I can’t configure the same port number for these two projects using the integrated IIS Express development server for Visual Studio 2013?
Is it possible to make an integrated development server for visual studio 2013 in IIS Express to configure the same port number for these two projects?
Js function:
function AddProduct() {
var productmodel = {
ProductName: $('#ProductName').val(),
CreationDate: $('#CreationDate').val(),
ProuductSerialNumber: $('#ProuductSerialNumber').val(),
Descripiton: $('#Descripiton').val(),
CreatedBy: $('#CreatedBy').val(),
Price: $('#Price').val()
};
var form = $("#productFrm");
if (form.valid()) {
$.ajax({
url: 'Product/AddProduct',
type: 'POST',
data: JSON.stringify(productmodel),
contentType: "application/json;charset=utf-8",
beforeSend : function(xhr, opts){
$(".overlay").show();
$(".loading-img").show();
},
success: function (data) {
if (data.StatusCode === 204) {
alert('Product Created Succesfully');
}
else
{
alert('Something is wrong and server returned :' + data.StatusCode + ' and the reason is ' + data.ReasonPhrase);
}
},
error: function (x, y, z) {
alert(x + '\n' + y + '\n' + z);
},
complete : function() {
$(".overlay").hide();
$(".loading-img").hide();
}
});
}
}
GloboMart.Application.Web.UI project Configured in IIS Port 4515:
public class ProductController : Controller
{
private HttpClient _client;
private HttpResponseMessage _response;
public ProductController()
{
_client = new HttpClient();
_client.BaseAddress = new Uri("http://localhost:32450/");
}
public ActionResult Index()
{
return View();
}
[HttpPost]
public async Task<JsonResult> AddProduct(ProductViewModel ProductViewModel)
{
using (var client = _client)
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
StringContent content = new StringContent(JsonConvert.SerializeObject(ProductViewModel), Encoding.UTF8, "application/json");
_response = await client.PostAsync("api/Products/CreateProduct", content);
}
return Json(_response);
}
GloboMart.Services.WebApi project Confiugured in port http:
using GloboMart.Domain.Entities.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using GloboMart.Application.Web.UI.Models;
using GloboMart.Domain.Entities.Entities;
namespace GloboMart.Services.WebApi.Controllers
{
public class ProductsController : ApiController
{
private IProductRepository _repository;
public ProductsController(IProductRepository Repository)
{
_repository = Repository;
}
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
[HttpGet]
public List<Product> GetAllProducts()
{
var products= _repository.GetAll();
return products.ToList();
}
[HttpPost]
public void CreateProduct([FromBody] ProductViewModel ProductViewModel)
{
var Product=ConvertProductModelToProduct(ProductViewModel);
_repository.Add(Product);
}
public void Put(int id, [FromBody]string value)
{
}
public void Delete(int id)
{
}
private Product ConvertProductModelToProduct(ProductViewModel ProductViewModel)
{
`enter code here` var Product = new Product()
{
Name=ProductViewModel.ProductName,
SerialNumber=ProductViewModel.ProuductSerialNumber,
Description=ProductViewModel.Descripiton,
CreatedBy=ProductViewModel.CreatedBy,
CreationDate=Convert.ToDateTime(ProductViewModel.CreationDate),
Price=ProductViewModel.Price
};
return Product;
}
}
}