Asmx client with a WCF Service

Source: Internet
Author: User

Download Sample

This sample demonstrates how to create a service using Windows Communication Foundation (WCF) and then access the service from a non-WCF client, such as an asmx client.

Note:

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 implements a contract that defines a request-reply communication pattern. the contract is defined byIcalculatorInterface, which exposes math operations (Add,Subtract,Multiply, AndDivide). The asmx client makes synchronous requests to a math operation and the service replies with the result.

The Service implementsIcalculatorContract as defined in the following code.

Copy code

 
[Servicecontract (namespace = "http://Microsoft.ServiceModel.Samples"), xmlserializerformat] public interface icalculator {[operationcontract] Double add (double N1, double N2); [operationcontract] Double subtract (double N1, double N2); [operationcontract] Double multiply (double N1, double N2); [operationcontract] Double divide (double N1, double N2 );}

The datacontractserializer and xmlserializer map CLR types to an XML Representation. the datacontractserializer interprets some XML representations differently than xmlserializer. non-WCF proxy generators, such as wsdl.exe, generate a more usable interface when the xmlserializer is being used. the xmlserializerformatattribute is applied toIcalculatorInterface, to ensure that the xmlserializer is used for mapping CLR types to XML. The service implementation calculates and returns the appropriate result.

The service exposes a single endpoint for communicating with the service, defined using a configuration file (web. config ). the endpoint consists of an address, a binding, and a contract. the service exposes the endpoint at the base address provided by the Internet Information Services (IIS) host. theBindingAttribute is set to basichttpbinding that provides HTTP communications using soap 1.1, which is compliant with WS-I basicprofile 1.1 as shown in the following sample configuration.

Copy code

 
   
    
     
     
    
   

The asmx client communicates with the WCF Service using a typed proxy that is generated by the Web Services Description Language (WSDL) Utility (wsdl.exe ). the typed proxy is contained in the file generatedclient. CS. the WSDL utility retrieves metadata for the specified service and generates a typed proxy for use by a client to communicate. by default, the Framework does not expose any metadata. to expose the metadata required to generate the proxy, you must add a <servicemetadata> element and set itsHttpgetenabledAttributeTrueAs shown in the following configuration.

Copy code

<Behaviors> <servicebehaviors> <behavior name = "calculatorservicebehavior"> <! -- Setting httpgetenabled to true on the servicemetadata behavior exposes the Service's WSDL at <base address>? WSDL: http: // localhost/servicemodelsamples/service. SVC? WSDL --> <servicemetadata httpgetenabled = "true"/> <servicedebug includeexceptiondetailinfaults = "false"/> </behavior> </servicebehaviors> </behaviors>

Run the following command from a command prompt in the Client Directory to generate the typed proxy.

Copy code

 
WSDL/N: Microsoft. servicemodel. Samples/O: generatedclient. CS/urlkey: calculatorserviceaddress http: // localhost/servicemodelsamples/service. SVC? WSDL

By using the generated typed proxy, the client can access a given service endpoint by inserting the appropriate address. The client uses a configuration file (App. config) to specify the endpoint to communicate.

Copy code

 
<Appsettings> <add key = "calculatorserviceaddress" value = "http: // localhost/servicemodelsamples/service. SVC"/> </appsettings>

The client implementation constructs an instance of the typed proxy to begin communicating with the service.

Copy code

// Create a client to the calculatorservice. using (calculatorservice client = new calculatorservice () {// 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);} 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.

Copy code

 
Add (100, 15.99) = 115.99 subtract (145, 76.54) = 68.46 multiply (9, 81.25) = 731.25 divide (3.14285714285714) = press <enter> to terminate client.
To set up, build, and run the sample
    1. Ensure that you have completed MED 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.