In our previous development, Ajax programs are marked
The WebService of system. Web. Script. Services. scriptservice.
. This is the only reason for the existence of WebService: as a transit station for Ajax calls. To discard WebService, you only need to use WCF as the transfer station.
In the. NET 3.5 era, jquery can call WCF. I have also written a blog (for example, some issues that need to be paid attention to when jquery calls WCF), but it is troublesome to configure it.
In the new era of. Net 4, the configuration of WCF is indeed much simplified (for example, deploying WCF with IIS in. Net 4 is so simple), and the Ajax call of WCF is also simplified. Therefore, we decided to abandon WebService and fully enter WCF.
Currently, the program architecture of the blog garden is based on WCF, so we pay special attention to it. The articles we share may be simple, but they are all from the problems we encountered and solved during actual development.
The following describes how to call WCF through jquery in. net4:
Note: here, the WCF is only a transfer station for Ajax calls. It then calls the local service layer interface or other background WCF services.
1. Add a class file (common class) in app_code, such as helloservice. CS.
2. Add a method to helloservice. CS and add some WCF settings, such:
[Servciecontract] and [operationcontract] are standard settings. Here, the 'service interface "and" service implementation "are written in a class, which are not recommended. We will do this here because we need an Ajax call transfer station. The simpler the better.
It is worth noting that aspnetcompatibilityrequirements does not take ASP. NET pipe if this setting is not performed.
The context information of ASP. NET cannot be obtained. For example, if this setting is not added, httpcontext. Current is null. The most common scenario is based on
Httpcontext obtains user logon information and verifies user permissions. Corresponding to this setting, in Web. config
<System. servicemodel> of must be added with <servicehostingenvironment
Aspnetcompatibilityenabled = "true">, such:
Note: After the preceding settings are added to Web. config, all WCF Service implementations must set the aspnetcompatibilityrequirements attribute.
3. Continue to add settings in Web. config and add serviceactivations in system. servicemodel/servicehostingenvironment, for example:
Relativeaddress is the address name of the WCF Service, and service is the helloservice class name created earlier. Factory is the key and supports
Ajax calls rely on system. servicemodel. Activation. webscriptservicehostfactory.
All right, configure the WCF end in three steps. Then, start the project in vs2010 and access the WCF in the browser, for example, http: // localhost: 3960/jquerywcfdemo/helloservice. SVC. If the following figure is displayed, it indicates that the WCF end works normally.
4. jquery starts calling WCF. The Code is as follows:
Note that the difference between contenttype and data is described in the annotations.
Click the submit button, "Hello, world"... to enter the WCF era. For details, see:
Link: http://www.cnblogs.com/dudu/archive/2011/01/19/1939094.html