【问题标题】:no connection could be made because the target machine actively refused it 127.0.0.1:8000 inner exception无法建立连接,因为目标机器主动拒绝它 127.0.0.1:8000 内部异常
【发布时间】:2013-09-17 18:43:45
【问题描述】:

我是 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);

我的 app.config 看起来像:

<?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>

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

【问题讨论】:

  • When I compile the web service code it runs without error FYI 编译 =/= "运行没有错误"
  • 我已经纠正了,你是对的,它确实可以编译,但显然不能没有错误地运行,谢谢!

标签: c# wcf


【解决方案1】:

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

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

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

【讨论】:

  • 我将 app.config 更改为引用端口 8000,因此它们都匹配,但得到了相同的结果:
  • 将其更改为 8000,现在得到:localhost:8000/ServiceModelSamples/Service 上没有可以接受消息的端点监听。这通常是由不正确的地址或 SOAP 操作引起的。有关更多详细信息,请参阅 InnerException(如果存在)。
  • 您将发布一个新问题!由于完全不同的原因,这是一个完全不同的错误!
【解决方案2】:

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

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

【讨论】:

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