没有连接,因为目标机器主动拒绝它127.0.0.1:8000内部异常

时间:2013-09-17 18:43:45

标签: c# wcf

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

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

2 个答案:

答案 0 :(得分:3)

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

看起来你希望它是8000,而不是8888.

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

答案 1 :(得分:0)

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

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