Share via


Interoperating with ASMX Web Services

Download sample

This sample demonstrates how to integrate a Windows Communication Foundation (WCF) client application with an existing ASMX Web service.

Note

The WCF samples may already be installed on your machine. Check for the following (default) directory before continuing.

<InstallDrive>:\Samples\WCFWFCardspace

If this directory does not exist, click the download sample link at the top of this page. Note that this downloads and installs all of the WF, WCF, and CardSpace samples. The sample is located in the following directory.

<InstallDrive>:\Samples\WCFWFCardSpace\WCF\Basic\Service\Client\Interop\ASMX

Note

The set-up procedure and build instructions for this sample are located at the end of this topic.

This sample consists of a client console program (.exe) and a service library (.dll) hosted by Internet Information Services (IIS). The service is an ASMX Web Service that implements a contract that defines a request-reply communication pattern. The service exposes math operations (Add, Subtract, Multiply, and Divide). The client makes synchronous requests to a math operation and the service replies with the result. Client activity is visible in the console window.

The ASMX Web service implementation shown in the following sample code calculates and returns the appropriate result.

[WebService(Namespace="http://Microsoft.ServiceModel.Samples")]
public class CalculatorService : System.Web.Services.WebService
    {
        [WebMethod]
        public double Add(double n1, double n2)
        {
            return n1 + n2;
        }
        [WebMethod]
        public double Subtract(double n1, double n2)
        {
            return n1 - n2;
        }
        [WebMethod]
        public double Multiply(double n1, double n2)
        {
            return n1 * n2;
        }
        [WebMethod]
        public double Divide(double n1, double n2)
        {
            return n1 / n2;
        }
    }

As configured, the service can be accessed at https://localhost/servicemodelsamples/service.asmx by a client on the same machine. For clients on remote machines to access the service, a qualified domain name must be specified instead of localhost.

Communication is done through a client generated by the ServiceModel Metadata Utility Tool (Svcutil.exe). The client is contained in the file generatedClient.cs. The ASMX service must be available to generate the proxy code, because it is used to retrieve the updated metadata. Run the following command from a command prompt in the client directory to generate the typed proxy.

svcutil.exe /n:http://Microsoft.ServiceModel.Samples,Microsoft.ServiceModel.Samples https://localhost/servicemodelsamples/service.svc?wsdl /out:generatedClient.cs

By using the generated client, you can access a service endpoint by configuring the appropriate address and binding. Like the service, the client uses a configuration file (App.config) to specify the endpoint to communicate with. The client endpoint configuration consists of an absolute address for the service endpoint, the binding, and the contract, as shown in the following sample configuration.

<client>
   <endpoint 
      address="https://localhost/ServiceModelSamples/service.asmx" 
      binding="basicHttpBinding" 
      contract="Microsoft.ServiceModel.Samples.CalculatorServiceSoap" />
</client>

The client implementation constructs an instance of the generated client. The generated client can then be used to communicate with the service.

// Create a client.
CalculatorServiceSoapClient client = new CalculatorServiceSoapClient();

// 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);

// Call the Subtract service operation.
value1 = 145.00D;
value2 = 76.54D;
result = client.Subtract(value1, value2);
Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);

// Call the Multiply service operation.
value1 = 9.00D;
value2 = 81.25D;
result = client.Multiply(value1, value2);
Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);

// Call the Divide service operation.
value1 = 22.00D;
value2 = 7.00D;
result = client.Divide(value1, value2);
Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);

//Closing the client gracefully closes the connection and cleans up resources.
client.Close();

Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate client.");
Console.ReadLine();

When you run the sample, the operation requests and responses are displayed in the client console window. Press ENTER in the client window to shut down the client.

Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714

Press <ENTER> to terminate client.

To set up, build, and run the sample

  1. Ensure that you have performed the One-Time Set Up Procedure for the Windows Communication Foundation Samples.

  2. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.

© 2007 Microsoft Corporation. All rights reserved.