1
votes

I am new to WCF and have created one solution with 2 projects, one is the client app, the other is the web service. When I compile the web service code it runs without error:

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

On the client side, the code fails on the client.add line with "There was no endpoint listening at http://localhost:8000/ServiceModelSamples/Service that could accept the message", with the inner exception of "no connection could be made because the target machine actively refused it 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);

My app.config looks like:

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

Can an expert point me in the right direction? I have tried many different endpoint addresses, but no luck. Thanks in advance!

2
When I compile the web service code it runs without error FYI compiling =/= "runs without error"tnw
I stand corrected, you are right it does compile, but obviously doesn't run without error, thanks!user2483529

2 Answers

3
votes

Your server is being hosted on 8888 while your client is attempting to connect to port 8000. Change the server or client port so that they match.

It looks like you want it to be 8000, not 8888.

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

I tested your code. I believe you need to add the service to your base address in your client app.config file like this

endpoint address="http://localhost:8888/ServiceModelSamples/Service/CalculatorService"