Comparison between WCF and Asp.net Web Service

Source: Internet
Author: User
Tags configuration settings

First, let's briefly introduce what is WCF, and then compare it with Asp.net web service.

Windows Communication Foundation (WCF) is used to build service-oriented applications.Program. With WCF, data can be sent as asynchronous messages from one service endpoint to another service endpoint. A service endpoint can be a part of a continuously available service hosted by IIS or a service hosted by an application. An endpoint can be a service client that requests data from a service endpoint. A message can be a single character or word sent in XML format to a complex binary data stream. Although such applications can be developed before the emergence of WCF, the emergence of WCF makes the development of such applications easier.

Asp.net web service usually depends on xmlserializer.. NET Framework.. NET Framework.

Disadvantages of using xmlserializer to serialize or deserialize the Data Type of. NET Framework:

1. Only fields and attributes marked as public can be serialized.

2. Only a set of classes that implement the ienumerable and icollection interfaces can be serialized.

3. classes that implement the idictionary interface cannot be serialized, such as hashtable.

WCF uses datacontractattribute and datamemberattribute to mark the. NET Framework types that can be serialized.

[Datacontract] public class item {[datamember] Public String ID; [datamember] public decimal qty; [datamember] public decimal price ;}

Datacontractattribute can be applied to classes or struct, and datamemberattribute can be applied to fields and attributes marked as public or private.

Differences between datacontractserializer and xmlserializer:

1. datacontractserializer does not control the representation of data types in. NET Framework in XML, so the performance is higher than that of xmlserializer.

2. xmlserializer cannot determine the fields or attributes to be serialized, while datacontractserializer can use datamemberattribute to specify the fields or attributes to be serialized.

3. datacontractserializer can serialize classes that implement the idictionary interface.

4. Because datacontractserializer can access non-public members of an object, it is required to run in full trust mode during deserialization, while xmlserializer does not.

 

Service Development

When developing Asp.net web service, you must add the WebService attribute to the class and add webmethodattribute to the class method.

 
[WebService] public class webservice1: system. Web. Services. WebService {[webmethod] Public String helloworld () {return "Hello World ";}}

A WCF Service provides one or more WCF endpoints. Each endpoint includes an address, binding, and contract ). When developing a WCF Service, you usually add servicecontractattribute and operationcontractattribute to the interface to define the service contract (contract ):

 
[Servicecontract] public interface iservice1 {[operationcontract] void dowork ();}

Servicecontractattribute specifies that the interface defines a WCF Service Contract. operationcontractattribute indicates which method of the interface is defined as an operation in the service contract. After the service contract is defined, a class is required to implement it:

 
Public class service1: iservice1 {public void dowork () {// do some works }}

The class that implements the service contract is a service in WCF. Configure the service address and binding type in the configuration file.

 
<? XML version = "1.0" encoding = "UTF-8"?> <Configuration> <system. servicemodel> <services> <service name = "service1"> <endpoint address = "service1" binding = "basichttpbinding" Contract = "iservice1"/> </service> </services> </system. servicemodel> </configuration>

The binding specifies how to communicate with the endpoint. The bindings supported by WCF include:

Basichttpbinding, wshttpbinding, wsdualhttpbinding, wsfederationbinding, nettcpbinding, netnamedpipebinding, netmsmqbinding, msmqintegrationbinding, and netpeertcpbinding. Basichttpbinding includes the protocols supported by Asp.net Web Services.

Service bearer

After Asp.net Web Service is compiled, an assembly and a service file with the extension. asmx are generated. This file containsCodeAnd the information of the assembly in which it is located. Copy the service file to the root directory of the Asp.net program hosted by IIS, and copy the assembly to the \ bin directory under the root directory of the program. The service can be accessed through the URL of the service file.

The WCF Service can be hosted by IIS, Windows Process activation Service (was), or. NET applications. Steps carried in IIS or was:

1. Compile the service.

2. Copy the service file with the extension. SVC to a virtual directory and copy the assembly to the \ bin folder under the virtual directory.

3. Copy the Web. config configuration file to the virtual directory.

Client development

The Asp.net web service client uses the command line tool WSDL. EXE to generate client code.

WCF uses servicemodel metadata utility tool(svcutil.exe) to generate client code.

Message Protocol

The header of the SOAP message in Asp.net web service can be customized.

WCF uses messagecontractattribute, messageheaderattribute, and messagebodymemberattribute to describe the structure of the SOAP message in the service.

Metadata

When the client requests metadata from the Asp.net web service through http get, ASP. NET generates the WSDL and sends it to the client. The generated WSDL can be customized by creating a class that inherits from servicedescriptionformatextension.

The client can send the request information defined by the WS-metadataexchange standard to WCF to receive the returned WSDL. The WSDL generated by WCF can also be customized. WCF can also be configured to not generate the WSDL and provide a fixed URL for the WSDL file.

Exception Handling

Exceptions not handled in the Asp.net web service will be returned to the client as soap errors.

Exceptions not handled in the WCF Service are not returned to the client as soap errors. During debugging, you can use configuration settings to return unhandled exceptions to the client.

Security

The security settings of Asp.net Web services are the same as those of other IIS applications.

Therefore, the security settings of WCF are independent. However, security settings that can be applied to Asp.net web services can also be applied to WCF.

Related Article

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.