1

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

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

4

2 回答 2

3

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

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

Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");
于 2013-09-17T18:51:52.843 回答
0

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

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

于 2013-09-17T20:16:07.303 回答