WebService use. NET4 jquery invoke WCF

Source: Internet
Author: User
Keywords Jque here we are.
Tags ajax calls class configuration development jquery net note

In our previous development, for AJAX programs, the webservice was marked as [System.Web.Script.Services.ScriptService] via jquery, and then the backend WCF was invoked in webservice. This became the only reason for the existence of WebService: as a staging point for Ajax calls. If you want to discard WebService, just use WCF as a staging point.

In the. NET 3.5 era, jquery can invoke WCF, and I've written blogs before (for example, some of the problems that jquery calls WCF to note), but configuration is cumbersome.

As you go into the new era of. NET 4, the configuration of WCF does simplify a lot (for example, deploying WCF with IIS in. NET 4 is as simple as this), and the Ajax invocation of WCF is also simplified. So we decided to abandon WebService and enter WCF fully.

  

Now the program architecture of the blog Park has been based on WCF, so we pay more attention to WCF, share some of the articles may be relatively simple, but it is from our actual development encountered and resolved problems.

Share below. How to invoke WCF through jquery in NET4:

Description: WCF here is just a staging point for Ajax calls, which then invokes the local service layer interface or other WCF services in the background.

1. Add a class file (that is, normal class) to the App_Code, such as HelloService.cs.

2. Add a method to the HelloService.cs, plus some WCF settings, as shown in the following figure:

  

[Servciecontract] and [OperationContract] are standard settings, where the ' service interface ' and ' service implementation ' are written in a class, not a recommended practice. We do this here because we need an Ajax call station, the simpler the better.

The note here is aspnetcompatibilityrequirements, if this setting is not done, WCF does not go asp.net pipelines, asp.net context information can not be taken, such as: Do not add this setting, HttpContext.Current is null, the most commonly used scene is based on the HttpContext to obtain user login information, the user permissions to authenticate. corresponding to this setting, the Web.config should be added in the following figure:

  

* Note: When the above setting is added to the web.config, all WCF service implementations will have to set the Aspnetcompatibilityrequirements property.

3. Continue to add settings in Web.config, add Serviceactivations in System.servicemodel/servicehostingenvironment, as shown below:

  

Relativeaddress is the address name of the WCF service, which is the HelloService class name created previously, and factory is the key to support Ajax invocation, It's System.ServiceModel.Activation.WebScriptServiceHostFactory.

All right, three steps to fix the WCF end configuration. When you start the project in VS2010, you can access this WCF in the browser, such as: Http://localhost:3960/jQueryWcfDemo/HelloService.svc. If the following screen appears, the WCF side is working properly.

  

4. jquery Call WCF started ... The code is as follows:

  

The place to note is the background in the above figure, contenttype and data, and the difference between the WebService is explained in the comments.

Click the Submit button, "Hello, World" ... Full access to the WCF era, see the following figure:

  

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.