Abandon WebService calling WCF with jquery in. net4

Source: Internet
Author: User

In our previous developmentProgramAll WebServices marked as system. Web. Script. Services. scriptservice through jquery call, and then the backend WCF is called in WebService. 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 Park is based on WCF, so we pay special attention to and shareArticleSome of them may be relatively simple, but they all come 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 if this setting is not performed. net pipeline, Asp.. net, for example, httpcontext. current is null. The most common scenario is to obtain user logon information based on httpcontext and verify user permissions. Corresponding to this setting, <servicehostingenvironment aspnetcompatibilityenabled = "true"> must be added to <system. servicemodel> of Web. config, for example:

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 the service is the helloservice class name created earlier. Factory is the key. It supports Ajax calling and relies 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,CodeAs 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

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.