Interoperating with ASMX Web Services
Collapse the table of content
Expand the table of content

Interoperating with ASMX Web Services

 

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

System_CAPS_noteNote

The setup 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 http://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 http://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="http://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. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

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


System_CAPS_importantImportant

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

<InstallDrive>:\WF_WCF_Samples

If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.

<InstallDrive>:\WF_WCF_Samples\WCF\Basic\Client\Interop\ASMX

Show:
© 2016 Microsoft