【问题标题】:No connection could be made because the target machine actively refused it 127.0.0.1:32450无法建立连接,因为目标机器主动拒绝它 127.0.0.1:32450
【发布时间】:2017-06-06 21:25:15
【问题描述】:

我正在尝试在 MVC 中使用 3 层架构 MVC UI-->服务-->实体

我正在使用内置的 asp.net Web 开发服务器,在该服务器中我配置了端口 4515 来运行 UI 层,并且我正在进行 ajax 调用以访问配置在端口 35420 的服务层中的 webapi 服务。当我进行 ajax 调用时,出现如下错误,状态为 500

System.Net.Sockets.SocketException:无法建立连接,因为目标机器主动拒绝它 127.0.0.1:32450。

当我查看 IIS express 时,只有 4515 正在运行,而 35420 服务层没有运行。

如何解决这个问题?当我的 UI 层运行时,我希望我的服务层自动并行运行?有什么方法可以为此完成更好的解决方案吗?

而且我无法使用 Visual Studio 2013 开发服务器内置的 IIS Express 为上述两个项目配置相同的端口号?

是否可以在 IIS Express 内置的 Visual Studio 2013 开发服务器中为上述两个项目配置相同的端口号?

js函数:

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){
                //show loading gif
                $(".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() {
                //remove loading gif
                $(".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/");
        }
        // GET: Product
        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://localhost:32450/


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;
        }

        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5
        [HttpGet]
        public List<Product> GetAllProducts()
        {
            var products= _repository.GetAll();

            return products.ToList();

        }

        // POST api/values

        [HttpPost]
        public void CreateProduct([FromBody] ProductViewModel ProductViewModel)
        {
            var Product=ConvertProductModelToProduct(ProductViewModel);
            _repository.Add(Product);
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        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;
        }
    }
}

【问题讨论】:

  • 你有多个项目,所有项目都在调试时启动:stackoverflow.com/questions/3697092/… ?
  • 我认为端口号(35420 和 32450)的不一致是拼写错误,而不是您尝试连接到错误的端口?

标签: c# asp.net entity-framework asp.net-web-api


【解决方案1】:

3 层架构并不意味着您必须拥有 3 个单独托管的层。

您可以在 Visual Studio 中将所有层放在一个解决方案中。这样,当您运行您的网站时,您可以将其作为一个托管解决方案一次性运行。通常这是通过一个 Web/MVC 项目、一个应用程序逻辑项目和一个数据访问项目(您称之为实体)来完成的。您应该能够在 Application Logic 项目的 Web/MVC 项目中引入引用。然后,您可以将数据访问的引用拉入应用程序逻辑(反之亦然)。

【讨论】:

    【解决方案2】:

    拥有这样工作的应用程序是完全正常的:一个端口上的 UI 和不同端口上的 webapi。

    你有多种方法来处理这个问题。

    1. UI 和 WebApi 都是作为单独项目的同一解决方案的一部分。您知道您可以将一个项目设置为默认项目,因此当您运行解决方案时,一个项目会自动运行。您可以右键单击解决方案,转到属性,然后选择通用属性,然后选择然后选择多个启动项目并选择您想要的两个项目,甚至超过 2 个。然后,当您运行解决方案时,两者都将运行并可用。

    2. UI 和 WebAPI 在 2 个不同的解决方案中。您所要做的就是先启动 APi,然后启动 UI,它们都将在各自的端口上运行并能够相互通信。

    无论您选择哪种解决方案,您都可以独立运行和调试每一层。

    您还可以让您的生活更轻松,并将每个项目配置为在适当的 IIS 而不是 IIS express 中运行,这样您就不会得到那些疯狂的端口号,它基本上简化了您的生活,而且它更容易,因为在某些时候你需要在适当的 IIS 中部署这些东西,你只需复制你在 dev 中已有的东西

    【讨论】:

      猜你喜欢
      • 2015-04-05
      • 2012-03-30
      • 2012-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-14
      • 2018-07-12
      • 1970-01-01
      相关资源
      最近更新 更多