ASMX Client with a WCF Service
2008-05-04 16:58
357 查看
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 by the ICalculator interface, which exposes math operations (Add, Subtract, Multiply, and Divide). The ASMX client makes synchronous requests to a math operation and the service replies with the result. The service implements an ICalculator contract as defined in the following code.
Copy Code
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 to the ICalculator interface, 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. The binding attribute 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 its httpGetEnabled attribute to True as shown in the following configuration.
Copy Code
Run the following command from a command prompt in the client directory to generate the typed proxy.
Copy Code
By using the generated typed proxy, the client can access a given service endpoint by configuring the appropriate address. The client uses a configuration file (App.config) to specify the endpoint to communicate with.
Copy Code
The client implementation constructs an instance of the typed proxy to begin communicating with the service.
Copy Code
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
To set up, build, and run the sample
Ensure that you have performed the One-Time Set Up Procedure for the Windows Communication Foundation Samples.
To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.
To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.
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 by the ICalculator interface, which exposes math operations (Add, Subtract, Multiply, and Divide). The ASMX client makes synchronous requests to a math operation and the service replies with the result. The service implements an ICalculator contract 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 to the ICalculator interface, 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. The binding attribute 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
<services> <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior"> <!-- This endpoint is exposed at the base address provided by the host: http://localhost/servicemodelsamples/service.svc. --> <endpoint address="" binding="basicHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator" /> </service> </services>
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 its httpGetEnabled attribute to True as 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 configuring the appropriate address. The client uses a configuration file (App.config) to specify the endpoint to communicate with.
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(22,7) = 3.14285714285714 Press <ENTER> to terminate client.
To set up, build, and run the sample
Ensure that you have performed the One-Time Set Up Procedure for the Windows Communication Foundation Samples.
To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.
To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.
相关文章推荐
- RestFul WCF JSON Service with client and on Mozilla Firefox –REST Client
- Pushing Data to a Silverlight Client with a WCF Duplex Service
- Things that will impact concurrency & capacity behavior of WCF service (with simoultaneous client requests/connections)
- 【转载】RestFul WCF JSON Service with client and on Mozilla Firefox –REST Client
- Pushing Data to a Silverlight Client with a WCF Duplex Service
- Invoke WCF service from Java Client with Authentication (X.509 Certificate) Java 客户端调用WCF服务 需要安全验证
- Generate WCF Client using svcutil with nettcpbinding
- Deploying OpenFire for IM (instant message) service (TCP/IP service) with database MySQL , client Spark on linux部署OpenFire IM 消息中间件服务
- WCF 4.0 service consumed in Silverlight 4.0 with cross domain
- WCF Data Service 的.NET Client 的不支持原生类型服务操作的解决方法
- WCF Data Service 的.NET Client 的不支持原生类型服务操作的解决方法
- WCF Data Service 的.NET Client 的不支持原生类型服务操作的解决方法
- WCF Data Service 的.NET Client 的不支持原生类型服务操作的解决方法
- WCF Data Service 的.NET Client 的不支持原生类型服务操作的解决方法
- If WCF Service side and Client side config is different?!
- PHP calling .NET – Running WCF service with basicHttpBinding
- [转]Consuming a OData Service in a Client Application (WCF Data Services)
- The service ‘xxx’ configured for WCF is not registered with the Autofac container
- WCF 4.0 service consumed in Silverlight 4.0 with cross domain
- WCF中Client捕获Service异常