无法建立连接,因为目标计算机主动拒绝了它 127.0.0.1:8000 内部异常

本文关键字:异常 内部 8000 拒绝 连接 建立 因为 目标计算机 | 更新日期: 2023-09-27 18:31:52

我是WCF的新手,创建了一个包含2个项目的解决方案,一个是客户端应用程序,另一个是Web服务。当我编译 Web 服务代码时,它运行时没有错误:

namespace Microsoft.ServiceModel.Samples
{
class Program
{
    static void Main(string[] args)
    {
        // Step 1 of the address configuration procedure: Create a URI to serve as the base address.
        Uri baseAddress = new Uri("http://localhost:8888/ServiceModelSamples/Service");
        /* Step 2 of the hosting procedure: Create ServiceHost
         * Use the ServiceHost class to configure and expose a service for use by client applications when you are not using Internet Information Services (IIS) to expose a service.  
         * IIS interacts with a ServiceHost object on your behalf.             
         */
        ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);
        try
        {
            //WSHttpBinding is an interoperable binding that supports distributed transactions and secure, reliable sessions.
            WSHttpBinding ws = new WSHttpBinding();
            ws.Security.Mode = SecurityMode.Message; //Use SOAP message security
            ws.Security.Message.ClientCredentialType = MessageCredentialType.Windows; //Use windows authentication
            // Step 3 of the hosting procedure: Add a service endpoint.
            // Adds a service endpoint to the hosted service with a specified contract, binding, and endpoint address.
            //      The contract is the definition of what functionality the web service offers (i.e. its API)
            //      The binding specifies how the service communicates (protocols, transports, and message encoders)
            //      The address is the name of the endpoint being added to this service host
            selfHost.AddServiceEndpoint(
                typeof(ICalculator),
                ws, 
                "CalculatorService");
            // Step 4 of the hosting procedure: Enable metadata exchange.
            // Controls the publication of service metadata and associated information.
            //      HttpGetEnabled indicates whether to publish service metadata for retrieval using an HTTP/GET request.
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            //add the service metadata behavior to the list of service host behaviors
            selfHost.Description.Behaviors.Add(smb);
            // Step 5 of the hosting procedure: Start (and then stop) the service.
            selfHost.Open();
            Console.WriteLine("The service is ready.");
            Console.WriteLine("Press <ENTER> to terminate service.");
            Console.WriteLine();
            Console.ReadLine();
            // Close the ServiceHostBase to shutdown the service.
            selfHost.Close();
        }
        catch (CommunicationException ce)
        {
            Console.WriteLine("An exception occurred: {0}", ce.Message);
            selfHost.Abort();
        }
        Console.ReadLine();
    }
}

客户端,代码在 client.add 行上失败,并显示"没有终结点侦听可以接受消息http://localhost:8000/ServiceModelSamples/Service",内部例外是"无法建立连接,因为目标计算机主动拒绝了它 127.0.0.1:8000"

//Step 1: Create an endpoint address and an instance of the WCF Client.
CalculatorClient client = new CalculatorClient();
// Step 2: Call the service operations.
// Call the Add service operation.
double value1 = 100.00D;
double value2 = 15.99D;
double result = client.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

我的应用程序配置看起来像:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_ICalculator" />
            </wsHttpBinding>
        </bindings>
        <client>
                <endpoint address="http://localhost:8000/ServiceModelSamples/Service"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICalculator"
                contract="CalculatorServiceReference.ICalculator" name="WSHttpBinding_ICalculator">
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

专家可以指出我正确的方向吗? 我尝试了许多不同的端点地址,但没有运气。 提前感谢!

无法建立连接,因为目标计算机主动拒绝了它 127.0.0.1:8000 内部异常

当客户端尝试连接到端口 8000 时,您的服务器托管在 8888 上。更改服务器或客户端端口,使其匹配。

看起来您希望它是 8000,而不是 8888。

Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");

我测试了你的代码。我相信您需要像这样将服务添加到客户端 app.config 文件中的基址

终结点地址="http://localhost:8888/ServiceModelSamples/Service/CalculatorService"